manual trace the following code int numbers [5][3] = { {1, 2, 5} , {2, 4, 3}, {1
ID: 3821008 • Letter: M
Question
manual trace the following code
int numbers [5][3] = { {1, 2, 5} , {2, 4, 3}, {1, 9, 8} , {2, 6, 5}, {7, 4, 2}};
int counts [9] = {0};
int digit = 1; //keeps track of numbers from 1 through 9
while (digit <= 9)
{
for (int row = 0; row < 5; row = row + 1)
for (int col = 0; col < 3; col = col + 1)
//count the number of times the digit appears in the numbers array
if (numbers[row] [col} == digit)
counts[digit - 1] = counts[digit - 1] + 1;
//end if
//end for
//end for
digit = digit + 1; //look for next digit
}//end while
//display counts
for (int x = 0; x < 9; x = x + 1)
cout << "The number " << x + 1 << " appears "
<< counts [x] << " time(s) ." << endl;
// end for
Explanation / Answer
The Modified code is as follows :
#include <iostream>
using namespace std;
int main()
{
int numbers [5][3] = { {1, 2, 5} , {2, 4, 3}, {1, 9, 8} , {2, 6, 5}, {7, 4, 2}};
int counts [9] = {0};
int digit = 1; //keeps track of numbers from 1 through 9
while (digit <= 9)
{
for (int row = 0; row < 5; row = row + 1)
for (int col = 0; col < 3; col = col + 1)
//count the number of times the digit appears in the numbers array
if (numbers[row] [col] == digit)
counts[digit - 1] = counts[digit - 1] + 1;
//end if
//end for
//end for
digit = digit + 1; //look for next digit
}//end while
//display counts
for (int x = 0; x < 9; x = x + 1)
cout << "The number " << x + 1 << " appears "
<< counts [x] << " time(s) ." << endl;
// end for
}
NOTE:
The Mistake you have done is mistake as Brace mismatch .
The Output is as follows:
The number 1 appears 2 time(s) .
The number 2 appears 4 time(s) .
The number 3 appears 1 time(s) .
The number 4 appears 2 time(s) .
The number 5 appears 2 time(s) .
The number 6 appears 1 time(s) .
The number 7 appears 1 time(s) .
The number 8 appears 1 time(s) .
The number 9 appears 1 time(s)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.