Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys
i'm trying to do 2 functions program
first one is to enter 10 elements of the array 2nd is to search for the repeated element in the array butt i had a little problem
this is what i tried...




C++
#include <iostream>
using namespace std;
void inputnumbers();
void sfr(int a[],int z);
int main(){
cout << "Enter 10 numbers" << endl;
inputnumbers();
	return 0;
}
void inputnumbers(){
	int z=10;
	int a[z]={};
	for (int i=0;i < 10 ; i++){
		cin >> a[i];
		
	}
	sfr(a,z);
	
}
void sfr(int a[],int z){
	int rep=0;
	for (int i=0;i<z;i++){
		for (int j=0;j<10;j++){
		if (a[i]==a[j])
		{rep++;}
		cout <<"repeated numbers are " << rep <<endl;
		}
	};
	;
}


What I have tried:

i wanted the output to look like this
C++
repeted elements are 2 for 2 times 

something like this the important thing is to give me the amount of repeating for each element..
Posted
Updated 2-Apr-16 23:45pm

1 solution

The way I would do this is simple: sort the array.
When you do that, all the repeated elements are next to each other and very, very easy to spot in a single pass...
1, 3, 17, 17, 21, 22, 24, 24, 37, 42
All you need to do is check if the current value is the same as the last value and if it is, it's repeated.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900