Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i dont know why the program is crashing when ever i run it
C++
#include <iostream>
void input(int a[],int size);
void search(int a[],int size);
void print(int a[],int&count,int&i);
using namespace std;
int main(){
	int count,i;
	int size=5,array[size];
	cout << "Enter 5 numbers :" << endl;
	input(array,size);
	search(array,size);
	for (int j=0;j<size ;j++){
		print(array,count,i);
	}
	
	return 0;
}
void input(int a[],int size){
	for (int i=0;i<size;i++){
		cin >> a[i];
	}
}
void search(int a[],int size){
	int count=0;
	for (int i=0;i<size;i++){
	for (int j=0;j<i;j++){
		if ( a[i]==a[j] ){
			count=1;
			count++;
		
		}
	}
	
	
}}
void print(int a[],int&count,int&i){
	cout << "The element " << a[i] <<" is repeated for " << count << " times " << endl;
}


What I have tried:

the program is keeps on crashing
Posted
Updated 18-Apr-16 1:58am

In the following loop:


C++
for (int j=0;j<size ;j++){
	print(array,count,i);
}

You use j as the loop counter and the uninitialized i as the index for the print function...

 
Share this answer
 
v2
Probably you meant something like:
C++
  #include <iostream>

void input(int a[],int size);
int repetitions(int a[], int size, int value);
void print(int a[], int size, int i);

using namespace std;

int main()
{
  int size=5, array[size];
  cout << "Enter 5 numbers :" << endl;
  input(array,size);
  for (int j=0;j<size ;j++)
  {
    print(array, size,j);
  }

  return 0;
}
void input(int a[],int size)
{
  for (int i=0;i<size;i++)
  {
    cin >> a[i];
  }
}

int repetitions(int a[], int size, int value)
{
  int count = 0;
  for (int n=0; n<size; ++n)
  {
    if ( a[n] == value) count++;
  }
  return count;
}

void print(int a[], int size, int i)
{
  cout << "The element " << a[i] <<" is repeated for " << repetitions(a,size,a[i]) << " times " << endl;
}



or

C++
 #include <iostream>
 #include <array>
 #include <algorithm>
 using namespace std;

int main()
{
  const int SIZE = 5;
  array<int,SIZE> ar;

  cout << "please enter 5 numbers " << endl;

  for (auto & a : ar)
    cin >> a;

  for (auto a : ar)
    cout << "item " << a << " is repeated for " << count( ar.begin(), ar.end(), a) << " times" << endl;
}
 
Share this answer
 
v4
Comments
Member 12173667 18-Apr-16 10:47am    
thats a good way ,, but it keeps repeating..
for example :
the element 5 is repeated for 3 times
the element 5 is repeated for 3 times
the element 5 is repeated for 3 times
the element 1 is repeated for 1 times
the element 1 is repeated for 2 times

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