Preparation
Create a working directory and
place the files necessary for this lab in that directory. The files can
be downloaded by right-clicking on the links below, selecting "Save Link
As", then finding your working directory and saving there. The files:
There is no need to print these
files. The first two (five.cpp and sum.cpp) are included below.
You'll need the others to do the worksheet, but if you are doing this early,
you could open the files now and try to figure them out.
Five.cpp
Examine the code in five.cpp
below. The program extracts five values from a standard input stream to
set the elements of an array. Then it displays the values of the
array. Although the program is correct for its task, the actual implementation
can be improved.
#include <iostream>
#include <string>
#include <conio>
#include <iomanip>
using namespace std;
const int ListSize = 5;
int main() {
int Number[ListSize];
int i;
// load Number from cin
cout << "Enter a list of " << ListSize
<< " elements" << endl;
for (i = 0; i < 5; ++i) {
cout << "Number: ";
cin >> Number[i];
}
// display contents of Number to cout
cout << "\n" << "The entered list is:
" << endl;
for (i = 0; i < ListSize; ++i) {
cout << "Number[" <<
setw(2) << i << "]: " << Number[i] << endl;
}
getch();
return 0;
} |
five.cpp
|
Now go through the following
steps:
-
Create a new project and open
(or create) five.cpp.
-
Modify the program so that it
extracts and lists eight numbers.
-
Run your program after it has
been modified.
-
Were you required to make an
additional modification to get the program to work?
-
Should this modification have
been necessary or should the original designer/implementer have written
the program differently?
Now, modify it some more:
-
Modify the program so that it
displays the minimum value in the list before displaying the values in
the entire list.
-
The minimum value should be
determined after you have read in the array elements. You will need to
have a variable that will contain the smallest value, initialized with
the first array value. You will need a loop that iterates through the array,
replacing the value in the variable if a smaller value is found.
-
Run your program to make sure
that it works.
Save and print your source code,
you'll be turning it in with your worksheet.
Sum.cpp
One of the most common errors
when using arrays is trying to use an invalid array index. C++ will not
stop you from overrunning the end of the array, and the results are generally
unpredictable. Therefore, the burden falls upon the programmer so s/he
must ensure that the array is always defined large enough to hold the necessary
information and never allows an invalid index to be used.
Examine the program given
in the figure below. This program initializes two arrays A and B and produces
a third array whose values are the sum of A and B.
What is the problem you see
with the program? (it's subtle, so look closely!)
#include <iostream>
#include <string>
#include <conio>
#include <iomanip>
using namespace std;
const int ListSize = 10;
int main() {
int A[ListSize] = { 30, 21, 9, 28, 29, 12, 4, 23, 4,
25 };
int B[ListSize] = { 54, 54, 82, 85, 91, 28, 32, 56,
61, 3 };
int C[ListSize];
int i;
for (i = 1; i <= ListSize; ++i) {
C[i] = A[i] + B[i];
}
cout << " A: [";
for (i = 0; i <= ListSize; ++i) {
cout << setw(4) << A[i]
<< " ";
}
cout << "]\n" << endl;
cout << " B: [";
for(i = 0; i <= ListSize; ++i) {
cout << setw(4) << B[i]
<< " ";
}
cout << "]\n" << endl;
cout << "A + B: [";
for (i = 0; i <= ListSize; ++i) {
cout << setw(4) << C[i]
<< " ";
}
cout << "]\n" << endl;
getch();
return 0;
} |
sum.cpp
|
ANSWER: In C++
when using a for loop to iterate through an array you usually want the
loop indices to range from 0 to n-1, where n is the size of the array.
This design corresponds to the fact that C++ numbers array indices from
0 to n-1. Hence, the typical for loop initializes its index object to zero
and repeats the loop as long as the index is less than the size of the
list.
Here's what to do:
-
Create a new project and copy
& paste the text from sum.cpp into a text edit window.
-
Correct the errors in the program.
-
Using the debugger, step through
the program and watch the values of A[0], B[0], and C[0].
-
What happens as the program
is running through the for loops?
-
These values are not the values
you want to watch. You really want to watch the current values being considered,
which you can do by editing the watches and replacing the 0's with i. Step
through the program again, but this time watch A[i], B[i],
and C[i] .
-
It is also possible to watch
an entire array at one time. Add watches of A, B and C
and again step through the program. This should give you a very nice
picture of what is going on (unfortunately, this is only useful when the
arrays are a manageable size).
Take a look at the worksheet
now (from your TA), you'll be using these skills to do similar problems.
|