Programming Assignment: P2

  1. Read the following sample programs to find out the output. Then run the program on computer and compare the output you found manually with the output from computer to make sure to understand the basic C++ output.
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
    	cout << fixed << showpoint;
    
    	cout << 4 + 3 * 5 << endl;
    	cout << (4 + 3) * 5 << endl;
    	cout << 4 * 5 % 3 + 2 << endl;
    	cout << (4 * (5 / 3) + 2) << endl;
    
    	return 0;
    }
    

    // Program Format demonstrates the use of output format.
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main ()
    {
      const int  INT_NUMBER = 1066;
      const float  FLT_NUMBER = 3.14159;
      
      float  fltValue;
      int    intValue;
    
      cout  << fixed  << showpoint;
    
      intValue = INT_NUMBER + FLT_NUMBER;
      fltValue = float(INT_NUMBER) + FLT_NUMBER;
      
      cout << "01234567890123456789" << endl;
      cout << INT_NUMBER  << endl;
      cout << intValue << endl;
      cout << setw(10) << intValue;
      cout << setw(10) << intValue  << intValue /10  << endl;
      cout << setw(10) << fltValue  << endl;
      cout << setprecision(10)  << fltValue  << endl;
      cout << setw(10)  << setprecision(3)  << fltValue  << endl;
      cout << fltValue << endl;
      cout << intValue << setw(3) << intValue << setw(7) << intValue << endl;
      
      return 0;
    }
    
  2. Write a C++ program to find the solutions to a quadratic equation:



    using the formula to find the two roots of the equation:



    a, b, and c are the constants of the quadratic equation. You program will ask the user to enter the values for the three constants from keyboard: a, b, and c. Your program will check if there is any real solution to the equation (i.e. b^2-4ac > 0). If yes, your program calculates the solutions to the equation and prints them out on screen. Otherwise, print out a message saying "there is no real solution to the equation." Test your program with the following two cases: (1) a = 1, b=-1, and c = -1 (2) a = 2, b = 2, and c = 1
  3. String processing. Write a program to break an address into city, state abbreviation, and zip code. For example, when a user enters an address from keyboard: Fort Worth, TX 76015 your program breaks it into three parts and print them on screen in format like City=Fort Worth, State=TX, Zipcode=76105 Test your program with these examples: (1) Dallas, TX 76206 (2)Little Elm Town, AK 45306 (3)Fort Worth, TX 76015 You might use getline function to read an address string into a string variable. For example:
    int main ()
    {
      string address;
      
      cout << "Enter an address: " << endl;
      getline(cin, address);
      
      //Break the value in variable address into three parts using string functions such as find and substr
      //...
      
      //Print the three parts on screen
      
      return 0;
    }
    
  4. At the top section of your program you need to use C++ comments to include your name, class name, programming assignment name, and date the program was written.
  5. Your programs should be properly formatted using indentation and empty lines (spaces). Use "camel" notation for variable naming.
  6. Upon completion, save all programs and the required output into a single text file using notepad (or notepad++). The file should be named in this format: P2_YourName.txt, for example, P2_JohnDoe.txt. Then submit to the Blackboard (Bb) as attachment by the deadline. The submission link on the Bb will be closed automatically after the deadline. Assignments that fail to follow the instructions will NOT be graded.