Note
You can do this lab using the graphic environment (Konqueror+Kwrite)
or in a terminal using vi as you wish. You can write a Makefile
if you feel it will save you time in the lab.
Set up
Login. Change directory to cs201.
Make a new directory lab04 and
change directory to it.
Perform all the exercises in this lab under this directory.
Fixing ifs, whiles and conditions
Download this program
[ largest.cpp ]
, compile it:
The following should find a mistake I planted in the code:
g++ -o largest largest.cppTry to figure out what I have deleted from it.
Fix it, compile, and test it by running this command
./largestinputting some numbers, and (on a blank line) after the data send a End-Of_Transmission signal: hold down the Control key and tapping the letter D.
Make sure it handles missing data and a series of numbers.
Otherwise fix it!
Modifying the program
Make a copy of largest.cpp called smallest.cpp
cp largest.cpp smallest.cpp(or use the GUI Konqueror)
Change smallest.cpp so that it outputs the smallest of the input numbers rather than the largest.
If...
Create a new file called "grade.cpp"
containing this code (do not use copy/paste!):
/* Programmers: Kay Zemoudeh & Dick Botting
Program Name: grade.cpp
This program translates a percentage grade into a letter grade
or else reports an error.
*/
#include <iostream>
#include <string>
using namespace std;
int main()
{
double percentage;
cout <<"Enter a percentage (0..100): ";
cin >> percentage;
cout << "Letter grade is ";
if (percentage > 100)
{
cout << "Percentage too large";
return 2; /* signals error #2 to operating system*/
}
else if (percentage >=95)
cout << "A";
else if (percentage >= 85)
cout << "B";
else if (percentage >=75)
cout << "C";
else if (percentage >=65)
cout << "D";
else if (percentage > 0)
cout << "F";
else
{
cout << "Invalid Percentage: " << percentage << endl;
return 1;/* to signal an error to the operating system*/
}
cout << endl;
return 0;
} // mainDon't forget to save it.
Check your file
If you can spot any errors, correct them.
Compile and run the program
Make sure there are no syntax, semantic, or logic errors.
Here is a suitable command
c++ -o grade grade.cppAre any compile errors output? If not go to the next step.
Test your grading program
Try every grade in turn... and some bad data. Does the
program do the right thing? If not, figure out why and
fixit.
Programming Exercise
Modify the program grade.cpp
to also handle letter grades followed by + or -. make the
code match the grading in the syllabus
[ ../../syllabus.html#grading ]
(Hint: Try to minimize your changes.)
(Hint: Start by writing some examples of what you want by hand. Then it often helps to draw a picture of how a program behaves or to describe the inputs and what happens to them. Then work on the source code. )
First fix the syntax errors.
Then test with as many test inputs that you can think of. Make sure your program is error free.
Getting this far by the end of the lab gets you an A for the lab.
To earn credit
Either show the lab instructor the programs
or print them and hand the printouts in.
| Who | When | What |
|---|---|---|
| Kay Zemoudeh | 12/9/1997 | Author |
| Dick Botting | 04/21/2005 | Improved |
| Dick Botting | 01/22/2008 | Rewrote |
. . . . . . . . . ( end of section CSci201 Laboratory 04 -- decisions and loops) <<Contents | End>>
Abreviations