


























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



























Brief pseudo code compute value
Detailed pseudo code identify formula(s) needed define and initialize variables do calculation and store result
Example 1 //compute area of a circle: ฯr^2 double pi=3.14159, radius=12, area; area = piradiusradius;
Example 2 //compute volume of a sphere: 4/3ฯr^3 double pi=3.14159, radius=12, volume; volume = 4/3.0piradiusradiusradius;
Example 3 //compute averageScore: total/ note: this was before we knew how to typecast so to avoid integer division quantity is defined as a double double averageScore, quantity=3.0; int score1=73, score2=92, score3=88; averageScore = (score1+score2+score3)/quantity;
Example 4 //compute tablesNeeded: attendees/seatsPerTable + int tablesNeeded, attendees=53, seatsPerTable=8; tablesNeeded = attendees / seatsPerTable+1;
Example 5 //compute seatsLeft: of // seatsPerTable โ remainder of attendees/seatsPerTable int seatsLeft, attendees=53, seatsPerTable=8; seatsLeft = seatsPerTable - attendees % seatsPerTable;
Example 6 //compute cardsLeft: remainder of cards/players int cardsLeft, cards=52, players=6; cardsLeft = cards % players;
Examples: An example that uses the addition or the subtaction operator. An example that uses the multiplication operator. An example that uses integer division. An example that uses real division. An example that uses the modulus operator. Note: We have not yet learned how to get values from a user. For any values you need in these examples define a variable and give it an initial value.
Brief pseudo code get value
Detailed pseudo code define variables needed print input prompt get value from user and store value
Example 1 //get age int age; cout<<"Enter your age: โ; cin>>age;
Example 2 //get wage double wage; cout<<"Enter your wage: "; cin>>wage;
Example 3 //get gender char gender; cout<<"Enter your gender (m or f): "; cin>>gender;
Examples: One that gets an integer One that gets a double One that gets a character
Example 4 //get x and y values of two points // compute slope of a line: (y 2 -y 1 )/(x 2 -x 1 ) int x1, y1, x2, y2; double slope; cout<<"Enter the x-value of the first point: "; cin>>x1; cout<<"Enter the y-value of the first point: "; cin>>y1; cout<<"Enter the x-value of the second point: "; cin>>x2; cout<<"Enter the y-value of the second point: "; cin>>y2; slope = (y2-y1)/static_cast
Examples: Include the following four examples: Compute the average of three integers. Compute the area of a circle. Compute the volume of a sphere. An additional example that uses parenthesis to change the order of operations Note: Just because you use parenthesis does not mean it changes the order in which the operations will happen. (They might happen in the same order without the parenthesis.)
Brief pseudo code get string
Detailed pseudo code define constant for the size of the char array define char array to hold string determine if the string may contain whitespace print input prompt get string from user store string
Example 1 //get title const int SIZE = 12; char title[SIZE]; cout<<"What is your official title? (president, secretary, etc.) "; cin>>title;
Example 2 //get address const int SIZE = 120; char address[SIZE]; cout<<"Enter your address: "; cin.getline(address,SIZE);
Example 3 //get age, name const int SIZE = 40; int age; char name[SIZE]; cout<<"Enter your age: "; cin>>age; cin.ignore(); cout<<"Enter your name: "; cin.getline(name,SIZE);
Examples: One example that gets a string that contains no whitespace. One example that gets a string that may contain whitespace. One example that needs the ignore function to work correctly.
Brief pseudo code print value to file
Detailed pseudo code define file stream open file print label to file print value to file
Example 1 //assign elevation = 5438 //print elevation to file int elevation = 5438; ofstream fout("mountain.txt"); fout<<"The elevation of the park is "<<elevation<<endl;
//contents of mountain.txt //The elevation of the park is 5438
Example 2 //assign volume = 543. //print volume to file ofstream fout("experiment.txt"); double volume = 543.7264; fout<<setprecision(2)<<fixed; fout<<"The volume is "<<volume<<endl;
//contents of experiment.txt //The volume is 543.
Example 3 //assign youthCost=4.59, adultCost=6.79, oversizedCost=8. //print cost of youth, adult, and oversized T-shirts ofstream fout("store.txt"); double youthCost=4.59, adultCost=6.79, oversizedCost=8.29; //print column labels fout<<setw(18)<<left<<"Item" <<right<<setw(8)<<"Youth" <<setw(8)<<"Adult"<<setw(8)<<"Over"<<endl; fout<<setw(18)<<left<<"T-shirts" <<right<<setw(8)<< youthCost <<setw(8)<< adultCost <<setw(8)<< oversizedCost<<endl;
/*contents of store.txt Item Youth Adult Over T-shirts 4.59 6.79 8. */
Examples:
One example that prints an integer to a file One example that prints a double to a file. Use formatting to print the number with 2 digits after the decimal place. Make sure your example value has more than 2 digits after the decimal place. One example that uses fields to print a string followed by at least three numbers in a row. For the output label print column headings on a line or two above the three values. Have the string be left-justified and the numbers be right justified. This is a common format for a table. In each example add a comment that shows what the file contains after your code is executed
Examples: One example that reads three numbers from a file One example that reads a combination of numbers and strings from a file In your examples include getting an integer, a real number, a string without whitespace, and a string with whitespace. For each example include a comment that shows exactly what is contained in the file
Brief pseudo code if condition then
if condition then else
if condition then else if condition then ...
if condition then else if condition then ... else
Detailed pseudo code determine condition (or conditions) get value(s) used in the condition(s) write if condition list what is to be done when condition is true list what is to be done when condition is false (optional)
Example 1 //get appleCount //if appleCount is less than 4 //then // print "Add apples to the grocery list" int appleCount; cout<<"How many apples are left in the drawer? "; cin>>appleCount; if(appleCount<4) cout<<"Add apples to the grocery list\n"; Example 2 //get age //if age >= 65 //then // dinnerCost=8. //else // dinnerCost=12. int age; cout<<"Enter age: ";
Examples: Do at least four examples One example is to have an if statement without an else part One example is to have an if and an else part One example is to have an if, one or more else if parts and no trailing else part One example is to have an if, one or more else if parts, and a trailing else part
In some of our previous examples we defined and initialized variables to specific numbers. For the value(s) that are part of a condition, the value must be acquired from a user, from a file, or from a computation based on external input.
When writing your examples be careful to consider which variables need to be initialized before the condition, which variables need to be assigned inside the conditions, and which need to be gathered from a user or from a file.
If you have a trailing else then you can guarantee that at least one part will be executed. so you can assign a variable a value inside each part of the condition statement and know that it will have been given a value. But if you don't have a trailing else then you need to prepare your variable for the possibility that none of the conditional parts will be executed.
C++ conditional statements are very similar to English conditional statements. Since that is the case sometimes the pseudo code for these instructions looks almost the same as the code. Be careful to distinguish between them. Make the pseudo code different from the actual code.
Brief pseudo code get value check value is in range then list what to do when value is in range
get value check value is in range then list what to do when value is in range else list what to do when value is NOT in range
get value check value is out of range then list what to do when value is out of range
get value check value is out of range then list what to do when value is out of range else list what to do when value is NOT out of range
Detailed pseudo code determine range condition (or conditions) get value(s) used in the condition(s) write range condition list what to do when value is in range list what to do when value is not in range (optional)
Code Example 1 //get score //check score is in range: 0-100 (inclusive) //then // add score to total //else // print "Score is not in range, it will not be counted." int score, total=0; cout<<"Enter a score: "; cin>>score;
if(score>=0 && score<=100) total += score; else cout<<"Score is not in range, it will not be counted.";
Brief pseudo code open file
Detailed pseudo code define ifstream variable open ifstream variable test to see if file opened correctly if file did not open correctly then print error message close program
Example 1 //open elevations.in ifstream fin("elevations.in"); if(!fin){ cout<<"File Open Error, program is shutting down.....\n"; return 1; }
Example 2 //get filename from user //open filename entered const int SIZE=64; char filename[SIZE]; cout<<"Enter the name of the input file: "; cin>>filename;
ifstream fin(filename); if(!fin){ cout<<"File Open Error, program is shutting down.....\n"; return 1; }
Do the following two examples:
Brief pseudo code validate input
Detailed pseudo code specify what is valid input get input write while loop with a condition that is true when the input is incorrect print error message print detailed input prompt get value end while loop
Example 1 //get length //validate length is positive and non-zero int length; cout<<"Enter the length of the pole: "; cin>>length; while(length<=0){ cout<<"Error. Length must be greater than zero.\n"; cout<<"Please enter the length of the pole: "; cin>>length; }
Example 2 //get score //validate score is between 0-100, inclusive int score; cout<<"Enter the score: "; cin>>score; while(score<0 || score>100){ cout<<"Error. Score must be between 1-100.\n"; cout<<"Enter the score: "; cin>>score; }
Example 3 //get denominator //validate denominator is not equal to 0 int denominator; cout<<"Enter the denominator: "; cin>>denominator; while(denominator==0){ cout<<"Error. Cannot divide by zero.\n"; cout<<"Enter the denominator: "; cin>>denominator; }
Do the following three examples: