joining array elements of same value and find the count


Hello,

I want to enter heights of the students like
175, 167, 160, 164, 183, 187, 188, 179, 176, 175,
169, 175, 176, 178, 165, 160, 173, 165, 187, 178

and finally output should be:

height number of pupils
160 2
164 1
165 2
167 1
... ...
... ...
188 1

I don't know which concept to use. Is it possible to use arrays for this.

I have tried the following code:

#include<iostream>

using namespace std;
int main()
{

int numberofstudents = 0;
int n;
int heights[10];

cout<<"Enter the number of students:"<<endl;
cin>>n;
cout<<"Enter the heights of students:";
for(int i=0;i<n;i++)
{
cin>>heights[i];
}
cout<<" heights";
cout<<"\t";
cout<<"number of pupils:";
cout<<"\n";

for(int i=0;i<n;i++)
{

cout<<heights[i];
cout<<"\t";
cout<<numberofstudents;
cout<<"\n";
}
system("pause");
return 0;

}

But here I can't make any use of numberofpupils variables.

Any valuable suggestions and code are greatly appreciated.
I think the best solution is to use standard container std::map.
For example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include	<iostream>
#include	<map>
#include	<algorithm>
#include	<utility>

int main()
{
	const int a[] = { 175, 167, 160, 164, 183, 187, 188, 179, 176, 175,
		          169, 175, 176, 178, 165, 160, 173, 165, 187, 178 };
	std::map<int, int> m;

	for ( auto it = std::begin(  a ); it != std::end( a ); ++it )
	{
		++m[*it];
	}

	std::for_each( m.begin(), m.end(),
 	                     []( std::pair<const int, int> x )
	                     { std::cout << x.first << ' ' << x.second << std::endl; } );
	return 0;
}


If your compiler supports the for construction based on range then you can use it instead of the loop and the algorithm.
Last edited on
From best to worst :

1. Use a map.
1
2
for(i = ...) height_to_count[heights[i]]++;
for(it = ...) cout << it->first << " " << it->second << '\n';
Best because requires no effort. Unless you don't know maps.

2. Sort the array.
1
2
3
4
5
6
std::sort(heights);
int i, j;
for(i = 0; i < size_of_heights; i++) {
   for(j = 1; heights[i] == heights[i+j]; j++);
   std::cout << heights[i] << " " << j;
}
Good because fast, unless you don't know a decent sorting algorithm and can't use std::sort.

3. The silly way: iterate through the array and pick the lowest value min. Than iterate through it again and count all elements with that value. Then do it again ignoring all elements lower or equal to previous min. I'm not going to write as it is long and ugly. It requires no knowledge of anything though.
Hi

If you are a beginner than try an easier algorithm

1- Copy the int heights[10] after you have read into another array ,e.g. int B[10]
2- create a function which takes a int pointer, a int size, and a int to be looked for, e. g. int count(int* a, int n, int t)

3- create another function which set the elements which have been already counted to -1

4- use the two function together in a loop

a simple example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// first function
int count(int* a, int n , int t){
	int c = 0 ;
	for( int i = 0 ; i < n ; i++)
		if( a[i] == t )
			c++;
	return c;
}

// second function

void clean(int* a, int n , int t){
	for( int i = 0 ; i < n ; i++)
		if( a[i] == t )
			a[i] = -1 ;
	
}


in your main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int B[10];
for(int i = 0 ; i < 10 ;i++)
 B[i] = heights[i] ;
//  you may first sort your array than, but you dont have to :-), only if it is a must from your teacher :-)

//and compute things like

for(int i = 0 ; i < 10 ;i++){
		if(B[i] != -1 ){
			cout<< B[i] << " \t" << count(B , 10 , B[i] )<<endl;
			clean(B, 10, B[i] );
		}
	
	}




hope it helps
Last edited on
Topic archived. No new replies allowed.