Thought Question 2 (1 Point): Type in the Program

Thought Question 2 (1 Point): Type in the Program

Thought Question 2 (1 point): Type in the program. Build and execute the project. How does the output compare with what you expected? Explain why you got the values that were output.

Answer :

The output

myScores[0] = -858993460

myScores[1] = -858993460

myScores[2] = -858993460

myScores[3] = -858993460

myScores[4] = -858993460

Press any key to continue . . .

Explanation

The output came out exactly as I expected.

The output shows some junk integer values.

In this small program, when the integer array myScores is declared, it creates a new empty array which can contain integer data values. Hence the array exists but its contents are empty… which in case of C++, junk integer values are stored. Without storing any integer data in the array, the program is asked to display the contents. Therefore we see some junk integer data displayed.

ThoughtQuestion 4 (1 point): Now build and run the project. How did the output compare with what you predicted? Explain why the values that you saw were generated.

Answer

The output

myScores[0] = 5

myScores[1] = 0

myScores[2] = 0

myScores[3] = 0

myScores[4] = 0

Press any key to continue . . .

I expected the output to display myScores[0] = 5 and to display junk integer data values for the rest of the array contents.

The content of the first index is exactly as I predicted, i.e. myScores[0] = 5, but for the rest of the contents we have 0 instead of junk integer data values.

Explaination

When the program executed the line int myScores[5] = {5}; then the program stored the value 5 in myScores[0] , then it searched for the values for the other indexes. But since there was none specified, the C++ program assumed that the other values were zero and initialized the other contents of the array as 0. That’s why we have the output as shown.

The line

int myScores[5] = {5};

is similar to saying

int myScores[5] = {5,0,0,0,0};

Thought Question 6 (1 point): Now compile and run the program. How does the output compare to what you predicted? Explain why the values that you see were output.

Answer

The ouput

myScores[0] = 5

myScores[1] = 0

myScores[2] = 0

myScores[3] = 0

myScores[4] = 0

myScores[5] = 6

Press any key to continue . . .

I expected the output to display similar to the problem above with myScores[0] = 5 and zero integer values to the rest of the array contents. But at the end I expected an “memory out of bound” error since the array SIZE is declared as 5 only. However, since there was no error I expected to see a junk integer value since there were no array element myScores[5] declared nor initialized.

The content of the indices 0 – 4 is exactly as I predicted , but at the end I was surprised to see the value 6.

Explaination

When a C++ program is executing, it does not notice when data storage results in an array index that is out of bounds. The data is taken and placed in the next available memory even if it does not belong to the array. Generally the next available memory is sequential to the array allocation.

So in this program, C++ took the value 6 and store in the next sequential memory location and assumed it as an extension of the array. Therefore we see that myScores[5] = 6 although it did not exist at first.

Thought Question 7 (4 points): In the space below, write down

(a) What went wrong with this program, and

(b) Fix the code so that it works correctly and paste a copy of your code below.

Answer

The output

89-

78-

56-T

93-N

100-N

78-

84-

67-

91-N

43-T

Press any key to continue . . .

(a) Explaination

In this program,

char grades[ ] = {'A', 'B', 'C', 'D', 'E'};

int scores[5]={0};

were declared and allocated memory space with the initial data values as

grades = A, B, C, D, E and scores = 0, 0, 0, 0, 0

These two memories are fixed.

However, in the .txt file there are 10 scores. So when the scores were read one after the other till the end of the file, C++ stored the entire ten grades in a sequential memory in continuation with the scores integer array. So the scores array was extended to hold all the ten values of the .txt file instead of only 5.

When this happened, it clashed with the storage address of the other array, char grades[ ]. Therefore it so happened that both the grades array and the end of scores array were pointing to the same address location. And the data values of the grades array got overwritten with the new integer values of the scores array. So when the grades array was displayed, it displayed the integer scores after converting it into char values.

(b) Explanation

This program can be fixed by simply changing the line

int scores[5]={0};

to

int scores[10]={0};

By doing this, the storage address of the scores array is defined and fixed. Thus the memory address clash.

The output

89-B

78-C

56-E

93-A

100-A

78-C

84-B

67-D

91-A

43-E

Press any key to continue . . .