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 formulas to find the two roots of the equation:



    where 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 are any real solutions to the equation (that is, check if the discriminant b2-4ac ≥ 0). If so, 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.
    
    //Skeleton program
    int main()
    {
    	//Variable declarations
    	int a, b, c;
    	double discriminant;
    	double x1, x2;
    	
    	cout << "Enter three constants: a, b, and c for a quadratic equation: " << endl;
    	cin >> a >> b >> c;
    
    	//Calculate discriminant
    	
    	//Check if the equation has real roots
    	if(discriminant > 0)
    	{
    		//find solutions (or roots) and print them out
    	}
    	else
    	{
    		cout << "There are no real solutions to the equation." << endl;
    	}
    	
    	return 0;
    }
    
    
    //Sample output:
    //Case 1:
    
    Enter three constants: a, b, and c for a quadratic equation: 
    1 -1 -1
    The quadratic equation is x^2-x-1=0
    x1 = 1.618
    x2 = -0.618
    
    
    //Case 2:
    
    Enter three constants: a, b, and c for a quadratic equation: 
    2 2 1
    The quadratic equation is 2x^2+2x+1=0
    There are no real solutions to the equation.
    
    
    
  3. String Processing. Write a program to break an address into city, state abbreviation, and zip code. An address consists of a city followed by a comma, a two-letter abbreviation of a state and a 5-digit zipcode. Note that there is a space between each part.

    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) Arlington, TX 76010 (2)Little Bighorn Town, MT 59022 (3)Fort Worth, TX 76015 You might use getline function to read an address string into a string variable. For example:
    #include <iostream>
    #include <string>
    using namespace std;
    
    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;
    }
    
    //Sample output for three test cases:
    Enter an address: 
    Arlington, TX 76010
    City = Arlington
    State = TX
    Zipcode = 76010
    
    Enter an address: 
    Little Bighorn Town, MT 59022
    City = Little Bighorn Town
    State = MT
    Zipcode = 59022
    
    Enter an address: 
    Fort Worth, TX 76105
    City = Fort Worth
    State = TX
    Zipcode = 76105
    
  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 Canvas 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.