hi all this is an algo of finding the no. of times the key occurs in an array..

#include<iostream>
using namespace std;

int main()
{
	int a[5]={1,2,2,5,6};
	int max=2;
	int counter=0;
	

	for(int i=0;i<5;i++)
	{
		if(max==a[i])
		 counter++;			
	

	}

	cout<<"the element "<<max<<" appears "<<counter <<" no. of times";
	return 0;
}

on this pattern i want to find an element which occurs the largest no. of times in da array??

i m stuck in it..dont know how to do
i m sending my code..

#include<iostream>
using namespace std;

int main()
{
	int array[5]={1,2,2,5,6};

	int counter=0;
	int max;
	
	for(int j=0;j<5;j++)
	{

	for(int i=0;i<5;i++)
	{
		if(array[j]==array[i])
		{
			max=array[j];
		 counter++;	
		break;
		}
	

	}
	}

	cout<<"the element which the largest no of time is : "<<max;
	return 0;
}

Recommended Answers

All 7 Replies

please sumbody reply to dis

I would create a structure that contains a value and the count for that value

struct data
{
    int value;
    int frequency;
};

Now make an array of those structures and initialize everythig to 0. Then begin the loop you wrote on line 11. For each value in the array list search the array of structures for the value. If there isn't one in the structure array add one. If there is an element in the structure array for the value in the aray array then just increment the frequency counter. When the loop finishes you have the information you need.

still confused n not picking up ur point.

You need to design some system to count the number of times each value appears in the array. If you do this with pencil & paper it will become clear how to do it. First start out with a clean sheet of paper, nothing on it. Now write column headings, one called Value and a little but to the right of that name it Count.

Now look at the first number in the array. Its a 1. Is there a 1 on that piece of paper? No because this is the first number, to under the Value column write a 1 and under the Count column write a 1 (first count).

Look at the second number. Its a 2, so is there a 2 under the Value column on the paper? No, so under the Value colum add another line with the Value of 2 and 1 in the Count colunm.

Look at the third number. Its another 2. There is alread a 2 under the Value column so increment is count from 1 to 2.

Do that for each of the other values in the array. After you are done you can easily see which Value has the greatest Count.

The above is the same concept you will use in your program. You could use two arrays of integers or one array of structures, whichever is easiest for you.

is it now better???

#include<iostream>

using namespace std;

int main()
{
	int c=0;
	int z=0;

	int array[5]={2,2,5,5,5};
	int value[5];
	int count[5];

	int counter=0;

count[z]=1;
	
	for(int j=0;j<5;j++)
	{

	for(int i=0;i<5;i++)
	{
		if(array[j]==array[i])
		{
			value[c]=array[j];
			
			c++;
			count[z]++;
			z++;
			
		break;

		}
		else 
			value[c]=array[j];
	
	

	}
	}

	cout<<"the element which is repeated the largest no of time is : "<<value[c]<<endl<<count[z];
	return 0;
}

>>is it now better???
Don't ask me but ask yourself. Does it work right? No, then its not better.

lines 11 and 12: you need to initialize the arrays to 0. Easy to do like this: int value[5] = {0}; Now intialize the other one just like that.

line 23: should be testing value and array if(array[j]==value[i]) delete lines 27 and 29 because they serve no useful purpose.

There are at least a couple other problems.

hi thanks a lot.. i m done with it..
actually i was not doing it properly the thing was i was storing the
new values in the arrays which was a bit complexed..
which i did was i take to variables value n counter initialized on zero..
n check the first element of array n counts its frequency if it is greater then the
counter value then swap the values n so on.. thanks a lot buddy-:) :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.