hi .

plz I need you to explain for me how i can write a function that return the mod of array with n value .
i think mod is that sample mode is it ?
i am try to solve .
but my code print the number taht is repeted not mode

#include<iostream.h>
void main()
{
	int array[12]={8,9,6,7,5,7,3,5,2,5,8,9};
	int sentinel=array[0];
	cout<<sentinel;
	for ( int i=1;i<12;i++)
	{
		if (sentinel<array[i])
		sentinel=array[i];
	}
	sentinel++;
	cout<<sentinel;
	cout<<":::\n";
	for (  i=1;i<12;i++)
	{
		if (array[i]!=sentinel)
		{
		//	int count =1;
			int value=array[i];
			array[i]=sentinel;
			int j=i+1;
			while(j<12)
			{
				if (array[j]==value)
				{
					i++;
				array[j]=sentinel;
				}
				j++;
			}
		 
				cout <<value<<"\t"<<"\n";
		}
	}
}

i just need The most frequent number ?
in that way i am understant the question >
what is the mod ?

What is the range of numbers in your data set?

Make an array the size of your largest element. If you have 0-9, make an array of ints,"counts", that is size 10, and initialize each element to 0.

If you encounter a number, increment that digits place in the array (so if you encounter a 3, increment counts[3]. Then find the maximum of counts, that's your mode.

The mod is the remainder of a division, as in 11 mod 3 = 2 or 11/3 = 3 rem 2 As for the most repeating number, you should try nested for loops to go through the array (which might even be of a variable length).

Like this:

#include<iostream>

using namespace std;

int main()
{

	int numbers[12]={8,9,6,7,5,7,3,5,2,5,8,9};
	int qMax = 0; //Quantity of times the maximum repeating number appears
	int temp, compare, max; //temp: stores the quantity of times the compared
                                //number repeats itself
                                //
                                //compare: number to be compared
                                //
                                //max: number that repeats the most times in the 
                                //array

	for (int i=0;i<12;i++)
	{
		temp = 0;
		compare = numbers[i];

		for (int j=0;j<12;j++)
		{
			if (numbers[j] == compare)
			{
				temp++;
			}
		}

		if (temp > qMax)
		{
			max = compare;
			qMax = temp;
		}
	}

	cout<<"The most repeating number: "<<max<<endl;
	system("PAUSE");

	return 0;
}

Edited 5 Years Ago by Nichito: n/a

Comments
good

Sort them first.

8,9,6,7,5,7,3,5,2,5,8,9 --> 2, 3, 5, 5, 5, 6, 7, 7, 8, 8, 9, 9

Now that the list is sorted, you can take advantage of that fact, so there's no need for a counts[] array since you're only dealing with one number at a time and can discard counts of incorrect modes as you go rather than wait till the end. By the time I'm done with the 5's, I know I don't need 2's or 3's count, so I don't keep track of them. I can do this because since the list is sorted I can guarantee that a 2 or 3 will never show up again.

That's another solution, though I think the sorting process might make the procedure a little longer and resource consuming

Sort them first.

8,9,6,7,5,7,3,5,2,5,8,9 --> 2, 3, 5, 5, 5, 6, 7, 7, 8, 8, 9, 9

Now that the list is sorted, you can take advantage of that fact, so there's no need for a counts[] array since you're only dealing with one number at a time and can discard counts of incorrect modes as you go rather than wait till the end. By the time I'm done with the 5's, I know I don't need 2's or 3's count, so I don't keep track of them. I can do this because since the list is sorted I can guarantee that a 2 or 3 will never show up again.

how ?
are you mean you should to sort then which element has more repetion ?
see
if we have 3 repeted 4 times and 6 repeted 4 times then there is no mode .
we can writed with a function
1 - function that sorted using bubble , or anyway to sort
2- function to chick if there two element have equale repetion time there is no mode .

. its write what iam understod ?

i think its take a long time .
because we can solved without sorted .

>> because we can solved without sorted .

Yes, you can. You were given two ways of doing it WITHOUT sorting.


>> i think its take a long time

I think it'll be faster and easier to sort them first.


Whether you write a function or just put the code all in main is up to you. I'm saying that if you sort first, you can avoid the new counts[] arrays that jonsca recommended, and if you sort first, you can use Nichito's method as a guide, except there will only be ONE loop rather than a nested loop.

Hence your "Big-O" time goes from about "n-squared", which is about what it is for Nichito's method, to "nlogn" for mine (basically the time it takes to do just the sort).

I also think it's easier to implement if you sort first, since there is less to keep track of.

Hence your "Big-O" time goes from about "n-squared", which is about what it is for Nichito's method, to "nlogn" for mine (basically the time it takes to do just the sort).

But you're forgetting the 'time' it takes to do the sort itself. You need to add that to your 'nlogn'+n-squared

I agree with jonsca. Once through one array, once through the second.

>> But you're forgetting the 'time' it takes to do the sort itself. You need to add that to your 'nlogn'+n-squared

PM sent to all but the OP containing my code. I'd be interested in your thoughts.

Sort = nlogn
Once through the array = n

Total time = nlogn + n.

The "n" drops out, so "nlogn".

ybb .. i am understand your idea . its now vey clear

i will solving it now .
as I want to do with tow function sort .and check func

i will come back :)

Edited 5 Years Ago by programing: n/a

i didn't write a full code ,
because my compiler Broken
if any one can corrected if i have errors
i will converted to function
i think just about idea.

// func sort
#include <iostream>
using namespace std; 
void main()
{
	int A[50],temp,z=0,n_elements;
	bool swap;
	cout<<"How many elements ? ";
	cin>>n_elements;
	cout<<"Enter the elements:-\n";
	for(int i=0;i<n_elements;i++) 
	   cin>>A[i];
	//sorting 
	do
	{
		swap=false;
		for(int i=0;i<n_elements-1;i++)
		{
			if(A[i]>A[i+1])
			{
				//swap
				temp=A[i+1];
				A[i+1]=A[i];
				A[i]=temp;
				swap = true ;
			}
		}
	}
	while (swap);
	for( i=0;i<n_elements;i++)
		cout<<A[i]<<"  ";
}

>>

check
	for (int i=0;i<n_elements;i++)
	{		
temp = 0;		
compare = numbers[i]; 
		for (int j=0;j<n_elements;j++)
		{			
if (numbers[j] == compare)		
	{	
			temp++;	
		}		} 	
	if (temp > qMax)		{		
	max = compare;			
qMax = temp;	
else if ( max=temp)
cout << "there is no mode ";
	}	} 
	cout<<"The most repeating number: "<<max<<endl;
	system("PAUSE"); 	return 0;

i didn't write a full code ,
because my compiler Broken

How can your compiler be broken? Did you drop it? Do you need a new one?

if any one can corrected if i have errors

Can't you tell if you have errors? Is that why your compiler is broken?

I really don't know what you are trying to tell us. If you have compiler errors, you must tell us what they are and where they are.

I'm sorry what I said you my dear to say me this
i didnt said write a code . i just said correct . i am a student . not a professional
and i study a course that related to c and c++ languages .
for that i try to learn by myself .
, Thank you for all

#include <iostream.h>
void bubble_sort(int Array[ ],int  ,int   );
void chick(int[],int );
void main()
{int n_elements,temp=0,Array[50];
	cout<<"How many elements ? ";
	cin>>n_elements;
	cout<<"Enter the elements:-\n";
	for(int i=0;i<n_elements;i++) 
	   cin>>Array[i];
bubble_sort(Array,n_elements,temp);	
chick ( Array, n_elements);
}
void bubble_sort(int Array[],int n_elements,int temp)
{ 
		for(int i=0;i<n_elements;i++)
		{
			for(int j=0;j<n_elements;j++)
			{
				if (Array [i]<Array[j]){
				temp=Array[i];
				Array[i]=Array[j];
				Array[j]=temp;
			}
		}
	}
	for( i=0;i<n_elements;i++)
		cout<<Array[i]<<"  ";}
void chick(int Array[],int n_elements ,int compare ,int qMax ,int max,int temp)
{
	for (int i=0;i<n_elements;i++)
	{		temp = 0;	
	compare = Array[i]; 	
	for (int j=0;j<n_elements;j++)	
	{			if (Array[j] == compare)	
	{				temp++;			}		} 	
	if (temp > qMax)		{	
		max = compare;		
		qMax = temp;
		 if ( max=temp)cout << "there is no mode ";	}	} 
	cout<<"The most repeating number: "<<max;
 
}

this my errors ;
Linking...
try.obj : error LNK2001: unresolved external symbol "void __cdecl chick(int * const,int)" (?chick@@YAXQAHH@Z)
Debug/try.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

try.exe - 2 error(s), 0 warning(s)

You have a prototype that defines chick as taking two parameters: void chick(int[],int ); Then you call chick with 2 parameters: chick ( Array, n_elements); And you actually defined the function to take 6 parameters: void chick(int Array[],int n_elements ,int compare ,int qMax ,int max,int temp) You need to make everything match.

And please, format your code better. It's very difficult to read.

Edited 5 Years Ago by WaltP: n/a

This article has been dead for over six months. Start a new discussion instead.