Hello all

I was looking into some textbook solution (preparing for my exam)

and I saw this program:

Question Details:
write a program that reads in a set of positive integers, representing test scores for a class, and outputs how many times a particular number appears in the list. you may assume that the data set has at most 100 numbers and -999 marks the end of the output data. the numbers must be output in increasing order. for example, for the data:

55 80 78 92 95 55 78 53 92 65 78 95 85 92 85 95 95

the output is:

test score count
53 1
55 2
65 1
78 3
80 1
85 2

using namespace std;
int main()
	int numbers[101]={0},i,num;

	cout<<"enter a number: ";

		//cout<<numbers[num]<<" "<<endl; >>this appears to be a counter
        cout<<"enter a number: ";

	cout<<"test score\tcount\n";


return 0;

how does it work ?
what does "i" represent ?

7 Years
Discussion Span
Last Post by vmanes

When you want to understand what a piece of code like this does, or how it does it, it's sometimes useful to get out pencil and paper. Write down each of the variable names, and beside them keep track of their values as the program executes. Walk through it by hand.

So, you start with an array of 101 elements, all initialized to 0. (You don't need to write out all 101 0's, just use the first 10 or so as an example.)

Assume some number input to num. How does that interact with the array? As you enter other values, how does the array change?

Once the -999 is entered, what does the next loop do? If you're getting ready for any sort of exam, that should be evident.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.