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

#include<iostream>
using namespace std;
int main()
{
	int numbers[101]={0},i,num;

	cout<<"enter a number: ";
    cin>>num;

	while(num!=-999)
    {
		numbers[num]++;
		//cout<<numbers[num]<<" "<<endl; >>this appears to be a counter
        cout<<"enter a number: ";
        cin>>num;
     }

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

	for(i=0;i<101;i++)
   
		if(numbers[i]!=0)
      
			cout<<i<<"\t\t"<<numbers[i]<<endl;

	system("pause");
return 0;
}

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.