Parallel arrays- count occurrence of input numbers

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  #include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>

using namespace std;

void sort (int [], int);

int main(int argc, char** argv) {
int const elements=10;
int list[elements];	
int num[elements];
int count, x;

for (int x=0; x<elements; ++x)
{cout<<"Enter a test score: ";
cin>>list[x];
}



	
sort (list, elements);
cout<<"Sorted list \n";
for (int x=0; x<elements; ++x)	
	{cout<<list[x]<<endl;
	}
	
if (list[x]==list[x+1])
{++count;
	}	
else 
{cout<<list[x]<<count;
}
	
	system("PAUSE");
	return EXIT_SUCCESS;
}
void sort (int list[], int elements)
{int temp;
bool swapmade=true;
int lastindex=elements-1;

while (swapmade)
{swapmade=false;
	for (int x=0; x<lastindex; ++x)	
		{if (list[x]>list[x+1])
		{temp=list[x];
		list[x]=list[x+1];
		list[x+1]=temp;
		swapmade=true;
		}
	}
	--lastindex;
		}	
}
Last edited on
bump
You don't have the lines 30-35 inside any loop. What should the value of x be?

The count should be initialized/reset every time when a new value appears.

Note that the x's on lines 16 and 26 mask the outer scope x (line 14).
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.
Last edited on
Topic archived. No new replies allowed.