CSP125 LABORATORY 3: Expressions and Operators
Objective
This
laboratory provides you with your first opportunity to decompose a problem into
manageable pieces and solve it. You will be doing a few things that are new
to you. If you have any questions or
problems, just ask your laboratory instructor for help.
Key
Concepts
·
Expression evaluation
·
Simple input & output
·
Hand checking code
·
Expressing mathematical
equations in C++
·
Using the procedures in the
introductory laboratory handout, create the working directory LAB02 in your
CSP125 directory.
·
Many of the activities that
are performed in the laboratory can be done in groups but you should work the
exercises yourself.
GETTING
STARTED
You
will need the following files, available electronically either by
downloading them from the following links, or by copying them on
turing from ~goldwasser/csp125/labs/lab03/
2.1
SOLVING
YOUR A, B, C’S
Examine
the program below. Next to the insertion statements write what you expect the
output to be.
int
main() {
// Object definitions and initializations
int a;
int b;
int c;
int d;
//assign some values to our variables
//this is called initialization!
a = 3;
b = 12;
c = 6;
d = 1;
// Now calculate the results
d = d * a;
c = c + (2 * a)
d = d - (b / c);
c = c * c;
b = b / 2;
//
Finally display the results
cout << "a: " << a << endl;
cout << "b: " << b << endl;
cout << "c: " << c << endl;
cout << "d: " << d << endl;
// Exit indicating a lack of errors
return 0;
}
Get
the c++ file simpmath.cpp in the files for this lab. It should contain the
preceding program along with several additional comments and output statements.
·
Compile and run the program
to observe the output.
·
Did you get the same answers
for your manual calculations as you did from the computer program? If there are
differences, try to figure out why. If you cannot determine the reason for the
differences, ask a laboratory instructor for help.
·
Allowing a user to supply
input values is a better technique than hardcoding the values because it makes
the program more general. Now you will modify simpmath.cpp to extract user
input from the standard input stream. First, delete the hard-coded
initialization of a, b, c, and d.
·
Add a prompt that tells the
users of the program what you want them to do. In this case, you want to prompt
the user to supply a value for object a. Then add a statement to do the extraction. Your code might look like the following:
cout
<< "Enter value for object a: ";
cin
>> a;
It
is important to prompt the user for each object separately. So add lines of
code to the program that prompt the user to enter values for objects b, c, and
d. Be sure to store the user’s keyboard input in the appropriate object. Save
the program.
Compile
and run your improved program. Be sure to save the program before each
compilation and run. If you get an error message that you cannot figure out,
ask a laboratory instructor for help.
After
developing a program or modifying an existing one, a key question is, Does the
program run correctly? One way is to hand check the program. Hand checking a program involves computing the
results by hand for some input and making sure the results agree with what the
computer outputs for the same input. You can hand check your modified program
by using as inputs the values that were used to initialize the integer objects
a, b, c, and d in the original program. Run your program and enter the values
that were used to initialize the objects a, b, c, and d in the original
program. Did you get the same results?
Once
the program is working, SAVE it – ***
you will need to upload it later ***
OPERATION
ORDER IS IMPORTANT
Now
let’s consider a slightly more challenging problem—writing the general solution
to an algebraic problem. Suppose you have a simple problem that you wish to
solve. You should take the following steps to write a program to solve the
problem:
This
process seems relatively straightforward, so let’s give it a try. For the next
part of the lab, you are going to write a program that solves some mathematical
equations
OPERATION
ORDER IS IMPORTANT
You
should work independently on the next several steps.
Determine
the types of the objects. For these equations, you can use the type float
because three of the equations contain a division operation. If you used
integer objects, the result of the division operation would be truncated, which
would produce erroneous results.
Determine
how many inputs and outputs there will be. Will you need any temporary space to
store partial computations? Or do you want to try computing the larger problem
with one huge equation?
List
the definitions of all the objects you will need for input, output, and
temporary computations in the area provided below. We do the first declaration
for you.
double Result1;
Write
the solution for each of the following equations as it will need to appear in
C++ code in order to be correct. Pay particular attention to the order of
operations in each problem. Be sure to use parentheses ( ) as needed to enforce
the correct computation of each problem. We do the first one for you. Does your
solution agree? If not, review your answers for it and the other equations.
Result1 = (2 * a * a) + (4 * a) -
29;
On
a piece of paper Write the C++ solution to equation #2, #3 and #4
Once
you have determined that your answers are correct, open the file compute.cpp.
Use this file as a basis for computing the above equations.
Complete the program in that file. Save your work often. In
particular, always save it before you do a compile and run. Once it is working
correctly save it, *** you will need to upload it later ***
Compounding!
Legend has it that in 1626 Peter Minuit purchased Manhattan for $24 in barter. Did he make a good investment? To answer this question, modify the compound interest program compound.cpp to begin with a principal of $24 and calculate the amount on deposit if that money had been invested with interest rates of 2%, 3%, 5%, and 7% to observe the wonders of compound interest. Rather than hardcoding the percentages in you should change the program to accept the percentage as input from the user – you need to prompt the user correctly then take in the value entered. Write down the totals for each percentage and add them in the comments at the end of file compound.cpp – save your work. You will need to upload this file.
FINISHING
UP
You
will need to upload the programs
you have written – please ensure you upload
the correct files. You have
3 programs to upload.
simpmath.cpp
compute.cpp
compound.cpp
Thank
your instructor and TA for their help!