Docsity

Modifying a C++ Program to Find Minimum and Maximum Values, Study notes of Computer Science

Toggle sidebar
bg1
CS 149D
Recitation, March 20, 2006
Change an Existing C++ Program
Objective: Make some simple program changes.
The examples in class were not very easy to use since a user had to guess at what to do to
use the program. Change the program findminArrays.cpp to make it easier to use.
1. Add cout statements to prompt users for input. Remember, a cout statement can have the
form:
cout << “some text string as appropriate” << endl;
or
cout << “some text string” << variable1 << endl;
or
cout << “some text” << variable1 << “more text” << variable2 << endl;
Any mixture of variables and text string can be included, but you must place the “<<”
symbols between them.
Also modify the cout statement that displays the minimum value so that it also labels the
number it prints. So if minimum were -5, rather than just displaing the number, the
program should display:
minimum: -5
2. Make the findminArrays.cpp more useful by having the program ask the user how
many numbers they intend to enter.
a. Assume they will always enter fewer than 100, so you need to change the size of the
array accordingly.
b. You will need to introduce a new variable to store this size. This variable should be
used in the for loops to determine home many times the loop executes.
c. You need to correct the comments in the program so that they describe your new
program.
d. You need to add a line in the first set of comments to include your name as
programmer, such as:
// Programmer: <your name>
e. Add code to the program so that it also find the maximal value. You will need a new
variable for this. You will also need to add new if statements, similar to those for
minimum, to find the maximal value. You will also need to add a cout statement to display
the maximal value. Similar to 1 above, include a label with the number.
3. After you have your program working, make sure your name is included as a comment,
print it, and give it to your lab instructor.

Partial preview of the text

Download Modifying a C++ Program to Find Minimum and Maximum Values and more Study notes Computer Science in PDF only on Docsity!

CS 149D

Recitation, March 20, 2006

Change an Existing C++ Program

Objective: Make some simple program changes.

The examples in class were not very easy to use since a user had to guess at what to do to use the program. Change the program findminArrays.cpp to make it easier to use.

  1. Add cout statements to prompt users for input. Remember, a cout statement can have the form: cout << “some text string as appropriate” << endl; or cout << “some text string” << variable1 << endl; or cout << “some text” << variable1 << “more text” << variable2 << endl;

Any mixture of variables and text string can be included, but you must place the “<<” symbols between them.

Also modify the cout statement that displays the minimum value so that it also labels the number it prints. So if minimum were -5, rather than just displaing the number, the program should display:

minimum: -

  1. Make the findminArrays.cpp more useful by having the program ask the user how many numbers they intend to enter.

a. Assume they will always enter fewer than 100, so you need to change the size of the array accordingly.

b. You will need to introduce a new variable to store this size. This variable should be used in the for loops to determine home many times the loop executes.

c. You need to correct the comments in the program so that they describe your new program.

d. You need to add a line in the first set of comments to include your name as programmer, such as:

// Programmer:

e. Add code to the program so that it also find the maximal value. You will need a new variable for this. You will also need to add new if statements, similar to those for minimum, to find the maximal value. You will also need to add a cout statement to display the maximal value. Similar to 1 above, include a label with the number.

  1. After you have your program working, make sure your name is included as a comment, print it, and give it to your lab instructor.