I'm working on another code. I'm trying to create a program that when you enter a set number of test scores, it will sort them and display them in descending order. This works perfectly, however it must also count how many times each number is repeated and display the count. This is the part that does not work and I'm stumped. If anyone can advise me on this, that would be great.
Thank you for the reply! So I made a few changes and now it outputs the count of each number - but the numbers don't make sense! The portion I changed now looks like this:
1 2 3 4 5 6 7 8 9
for (x=0; x<elements; ++x)
{
if (list[x]==list[x+1])
{++count;
}
else
{cout<<list[x]<<setw(10)<<" "<<count<<endl;
}
}
And here is the output, which clearly counted too many occurrences of "40" and "50":
Enter a test score: 20
Enter a test score: 40
Enter a test score: 50
Enter a test score: 20
Enter a test score: 40
Enter a test score: 50
Enter a test score: 20
Enter a test score: 20
Enter a test score: 20
Enter a test score: 40
Sorted list
20
20
20
20
20
40
40
40
50
50
20 4
40 6
50 7
Press any key to continue . . .
I think it's adding their occurrence onto the count of the first number instead of starting fresh. How do I fix this?
The indentation of your code could be more intuitive:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// what is the value of 'count' now?
for ( x=0; x<elements; ++x )
{
if ( list[x] == list[x+1] )
// on last iteration x==elements-1, so
// you will read list[elements] here.
// An out-of-range error
{
++count;
}
else
{
cout << list[x] << setw(10) << " " << count << endl;
// the value will change. What should the 'count' become here?
}
}
There is probably an extra twist, but lets first see how you treat the points above.