Sample assignment and algorithm

 

Algorithm for simple math operations
Declare all the variables as a float
Ask user to enter first number to be added.
Ask user to enter second number to be added.
Read the two numbers.
Ask the user to type in the possible answer.
Use manipulators to get the desired output.
Perform the computation to get the sum of the two numbers.
Display the result

Good algorithm example
Here is a good example of an algorithm

 

Ask user to enter first number to be subtracted.
Ask user to enter second number to be subtracted.
Read the two numbers.
Ask the user to type in the possible answer.
Use manipulators to get the desired output.
Perform the computation to get the difference of the two numbers.
Display the result.

Ask user to enter first number to be multiplied.
Ask user to enter second number to be multiplied.
Read the two numbers.
Ask the user to type in the possible answer.
Use manipulators to get the desired output.
Perform the computation to get the product of the two numbers.
Display the result.

Ask user to enter first number to be divided.
Ask user to enter second number to be divided.
Read the two numbers.
Ask the user to type in the possible answer.
Use manipulators to get the desired output.
Perform the computation to get the quotient of the two numbers.
Display the result
Display a table that consists of the numbers added, subtracted, multiplied and divided together with the result.

Good programming style with comments
Great style example, notice how easy it is to read.

Also, the exercise we did, where you had to copy the program, showed the proper style that you are to follow

 

// Filename: Assign2.CPP
//
//  CS161 
//
// Program to help children  with simple math.
// Prompt child for two values after printing
// a title message.          
#include <iostream.h>       // for input/output
#include <iomanip.h>        // for formatting           
int main()
{          
   int addnum1;             // first addition number
	int addnum2;             // second addition number
	int addans;              // addition answer
	int her_ans;             // user's answer
  	int subnum1;             // first subtraction number
  	int subnum2;             // second subtraction number
  	int subans;              // subtraction answer
	float multnum1;          // first multiplication number
	float multnum2;          // second multiplication number
	float multans;           // multiplication answer
	float divnum1;           // first division number
	float divnum2;           // second division number
	float divans;            // division answer          
//*************************************************************************
// Addition section
//*************************************************************************

          
	 cout << "\n\n*** Math Practice"
	      << " ADDITION"                       // Header for Math 
			<< " ***\n\n\n";          
	 cout << "What is the first number?  ";	 // prompt for number
	 cin  >> addnum1;							       // read in number
	 cout << "What is the second number? "; 	 // prompt for number
	 cin  >> addnum2;							       // read in number
          
	 addans = addnum1 + addnum2;				    // Compute answer and give
	 cout << "What do you think"				    // user a chance to think.
			<< " the answer is?  ";
	 cin >> her_ans;								    // Nothing is done with this
          
	 cout << "\n" << addnum1 << " plus "       // Print answer and equation
			<< addnum2 << " is "
			<< addans << "\n\n Hope you"         // and an encouraging note
			<< " got it right!";          
//*************************************************************************
// Subtraction section
//*************************************************************************
          
	cout << "\n\n***********************"
		  << "***************************";          
	cout << "\n\n*** Math Practice"            // Header for Math
        << " SUBTRACTION"
		  << " ***\n\n\n";          
	cout << "What is the first number?  ";	    // prompt for number
	cin  >> subnum1;							       // read in number
	cout << "What is the second number? "; 	 // prompt for number
	cin  >> subnum2;							       // read in number
          
	subans = subnum1 - subnum2;					 // Compute answer and give
															 // user a chance to think
	cout << "What do you think"
		 << " the answer is?  ";
	cin >> her_ans;							    	 // Nothing is done with her_ans
                                              
          
	cout << "\n" << subnum1 << " minus "       // Print answer and equation
		  << subnum2 << " is "
		  << subans << "\n\n Hope you"          // and an encouraging note
		  << " got it right!";
          
//*************************************************************************
// Multiplication section
//*************************************************************************
          
	cout << "\n\n***********************"
		  << "***************************";          
	cout << "\n\n***"
		 << "Math Practice MULTIPLICATION"      // Header for Math
		 << " ***\n\n\n";          
	cout << "What is the first number?  ";		 // prompt for number
	cin  >> multnum1;							       // read in number
	cout << "What is the second number? "; 	 // prompt for number
	cin  >> multnum2;							       // read in number
          
	multans = multnum1 * multnum2;			    // Compute answer and give
	cout << "What do you think"					 // user a chance to think.
		  << " the answer is?  ";
	cin >> her_ans;							   	 // Nothing is done with this
          
	cout << "\n" << multnum1 << " times "      // Print answer and equation
		  << multnum2 << " is "
		  << multans << "\n\n Hope you"         // and an encouraging note
		  << " got it right!";
          
//*************************************************************************
// Division section
//*************************************************************************
          
	cout << "\n\n***********************"
		  << "***************************";          
	cout << "\n\n***"
		 << "Math Practice DIVISION"     		// Header for Math
		  << " ***\n\n\n";          
	cout << "What is the first number?  ";		// prompt for number
	cin  >> divnum1;							      // read in number
	cout << "What is the second number? ";    // prompt for number
	cin  >> divnum2;						      	// read in number
          
	divans = divnum1 / divnum2;					// Compute answer and give
	cout << "What do you think"					// user a chance to think.
		  << " the answer is?  ";
	cin >> her_ans;							   	// Nothing is done with this
          
	cout << "\n" << divnum1
		 << " divided by "       			      // Print answer and equation
		 << divnum2 << " is "
		 << divans << "\n\n Hope you"         	// and an encouraging note
		 << " got it right!" <<endl;
          
//*************************************************************************
// Table Format section
//*************************************************************************          
																 
	cout << "____________"                  // Begin drawing table
	     << "_____________" 					 // Draw top of table box
		  << "_____________"
		  << "_____________"
		  << "_____________"
		  << "___________\n";
          
	cout.setf(ios::fixed, ios::floatfield);	 // Set up floating point
															 //  output format          
	//cout.setf(ios::showpoint);               // decided not to use
                                              // this statement          
	cout  << "|" << setw(19) << "| "           // Draw one row of vertical
			<< setw(18) << "|"                   // lines.
			<< setw(19) << "|"                   // Use setw() for spacing
			<< setw(20)
			<< setprecision(2)<< "|\n";          
	cout << "|" << setw(5) << addnum1          // Enter equation for
			<< setw(2) << "  +"                  // Addition
			<< setw(5) << addnum2
			<< setw(2) <<" = " ;
          
	cout << " | " << setw(5) << subnum1        // Enter equation for
			<< setw(2) << " - "                  // Subtraction
			<< setw(5) << subnum2
			<< setw(2) << " = ";
          
	cout << " | " << setw(5) << multnum1       // Enter equation for
			<< setw(2) << " * "                  // Multiplication
			<< setw(5) << multnum2
			<< setw(2) << " = ";
          
	cout << " | "
			<< setw(5) << divnum1                // Enter equation for
			<< setw(2) << " / "                  // Division
			<< setw(5) << divnum2
			<< setw(2) << " = "
			<< setw(2) << " |\n" ;          
	cout << "|" <<setw(10) << addans            // Enter one row of vertical
		  << setw(8) << "|"                      // lines for table
		  << setw(10) << subans                  // with answers formatted
		  << setw(9) << "|"                      // below the equation.
		  << setprecision(2)
		  << setw(10) << multans
		  << setw(9) << "|"
		  << setw(10) << divans
		  << setw(10) << "|\n";          
	cout << "|_________________|"
		  << "__________________|"
		  << "__________________|"         	  // Enter bottom line of
		  << "__________________|\n";              // table

          
	return 0;
}      
 

 

Back to CS 161 Homepage
This page was last modified June 20, 1999
wmorales@pcc.edu