Chapter 11
ONE-DIMENSIONAL ARRAYS
EXERCISE ANSWERS
Exam Preparation Exercises
2. False
3. a.
float floatArray[24];b.
int intArray[500];c.
double doubleArray[50];d.
char charArray[10];4.
const int CLASS_SIZE = 30;float quizAvg[CLASS_SIZE];
6. a.
for (cIndex = BLUE; cIndex <= BLACK; cIndex = Colors(cIndex + 1))count[cIndex] = 0;
b.
for (rIndex = 0; rIndex < MAX_LENGTH; rIndex++)rainbow[rIndex] = WHITE;
c.
int counter = 0;for (rIndex = 0; rIndex < MAX_LENGTH; rIndex++)
if (rainbow[rIndex] == GREEN)
counter++;
d.
cout << count[BLUE] << endl;e.
int sum = 0;for (cIndex = BLUE; cIndex <= BLACK; cIndex = Colors(cIndex + 1))
sum = sum + count[cIndex];
8. The For statement incorrectly causes
10.
sample [0] [1] [2] [3] [4] [5] [6] [7]------------------------------------------------
| 1 | 1 | 1 | 1 | -1 | -1 | -1 | -1 |
------------------------------------------------
11.
sample [0] [1] [2] [3] [4] [5] [6] [7]------------------------------------------------
| 0 | 101 | 2 | 103 | 4 | 105 | 6 | 107 |
------------------------------------------------
Programming Warm-Up Exercises
2.
void SetFailing( /* inout */ Boolean failing[],/* in */ const int score[],
/* in */ int length )
// Precondition:
// length <= MAX_STUD
// && score[0..length-1] are assigned
// Postcondition:
// For all i, where 0 <= i <= length-1,
// IF score[i] < 60, THEN failing[i] == TRUE
{
int index; // Loop control and index variable
for (index = 0; index < length; index++)
// Invariant (prior to test):
// For all i, where 0 <= i <= index-1,
// IF score[i] < 60, THEN failing[i] == TRUE
// && 0 <= index <= length
if (score[index] < 60)
failing[index] = TRUE;
}
4.
int PassTally( /* in */ const Boolean passing[],/* in */ int length )
// Precondition:
// length <= MAX_STUD
// && passing[0..length-1] are assigned
// Postcondition:
// Function value == count of TRUE values in passing[0..length-1]
{
int index; // Loop control and index variable
int count = 0; // Count of TRUE values
for (index = 0; index < length; index++)
// Invariant (prior to test):
// count == count of TRUE values in passing[0..index-1]
// && 0 <= index <= length
if (passing[index])
count++;
return count;
}
5.
Boolean Error( /* in */ const Boolean passing[],/* in */ const Boolean failing[],
/* in */ int length )
// Precondition:
// length <= MAX_STUD
// && passing[0..length-1] are assigned
// && failing[0..length-1] are assigned
// Postcondition:
// Function value == TRUE, if there is some i (0 <= i <= length-1)
// such that passing[i] == failing[i]
// == FALSE, otherwise
{
int index = 0; // Loop control and index variable
while (index < length && passing[index] != failing[index])
// Invariant (prior to test):
// For all i, where 0 <= i <= index-1,
// passing[i] != failing[i]
// && 0 <= index <= length
index++;
return (index < length);
}
7.
int NumberGreater( /* in */ int grade,/* in */ const int score[],
/* in */ int length )
// Precondition:
// length <= MAX_STUD
// && score[0..length-1] are assigned && grade is assigned
// Postcondition:
// Function value == count of values in score[0..length-1]
// that are >= grade
{
int index; // Loop control and index variable
int count = 0; // Count of values that are >= grade
for (index = 0; index < length; index++)
// Invariant (prior to test):
// count == count of values in score[0..index-1]
// that are >= grade
// && 0 <= index <= length
if (score[index] >= grade)
count++;
return count;
}
8.
void Reverse( /* inout */ int score[],/* in */ int length )
// Precondition:
// length <= MAX_STUD
// && score[0..length-1] are assigned
// Postcondition:
// score contains the same values as score@entry,
// rearranged into reverse order
{
int index; // Loop control and index variable
int tempScore; // Used for swapping
int halfLength = length / 2; // Upper limit for array index
for (index = 0; index < halfLength; index++)
{
// Invariant (prior to test):
// For all i, where 0 <= i <= index-1,
// score[i] and score[length - (i + 1)] have
// been swapped
// && 0 <= index <= halfLength
tempScore = score[index];
score[index] = score[length - (index + 1)];
score[length - (index + 1)] = tempScore;
}
}
Note that if you run the loop from 0 to
9. Change the
CompareLists function to the following:void CompareLists(
/* inout */ Boolean& allOK, // True if lists match
/* in */ const int firstList[], // 1st list of numbers
/* in */ int length, // Length of 1st list
/* inout */ ifstream& dataFile ) // Input file
// Precondition:
// allOK is assigned
// && length <= MAX_NUMBER
// && firstList[0..length-1] are assigned
// Postcondition:
// Values from the second list have been read from the input file
// && IF the lists are of equal length and all values match
// allOK == allOK@entry
// ELSE
// allOK == FALSE
// && The positions and contents of mismatches have been printed
// && IF one list is longer than the other
// A message indicating the longer has been printed
{
int counter; // Loop control and index variable
int number; // Variable used for reading
counter = 0;
dataFile >> number;
while (counter < length && dataFile)
{
// Invariant (prior to test):
// - Same as in book -
if (number != firstList[counter])
{
allOK = FALSE;
cout << "Position " << counter << ": "
<< setw(4) << firstList[counter] << " != "
<< setw(4) << number << endl;
}
counter++;
dataFile >> number;
}
if (counter < length)
{
// Assert: EOF occurred before first list was finished
allOK = FALSE;
cout << "First list is longer. Comparison stopped." << endl;
}
else if (counter == length && dataFile)
{
// Assert: First list has finished but EOF has not occurred
allOK = FALSE;
cout << "Second list is longer. Comparison stopped." << endl;
}
}
ANSWERS TO QUESTIONS
True/False
Multiple Choice
Fill-In
35. structured
36. index (index expression)
37. array (one-dimensional array)
38. elements
39. out-of-bounds
40. base address
41. length
42. subarray
43. parallel
44. semantic content
45.
float alpha[100];46.
char letterGrade[25];47.
FlagColors flagArray[51];48.
int gradeCount[7];49.
charArray[5] = 'X';50.
cout << waveLength[YELLOW];or
cout << waveLength[2];