-
Stuck!!
Hello,
I am stuck in the following program. Try to be nice, I'm new at this! I am needing to return the actual number that occurs the most times in an array. I have figured out which one that is, but can't seem to display it correctly. I've read, and reread my book on the subject concerning pointers (the chapter the problem is from). I still don't understand. I know I need to find the array subscript which correlates to the most reoccuring number. Please HELP!!!
[code]
#include <iostream.h>
#include <iomanip.h>
//Function prototype
int findCommon(int array[],int aray[],int number);
int main(void)
{
int numGrades;
int *grades;
int *freq;
cout<<"How many grades do you want to enter?<more than one please.>";
cin>>numGrades;
grades=new int [numGrades];
freq= new int[numGrades];
for(int count=0;count<numGrades;count++)
{
cout<<"Grade "<<(count+1)<<": ";
cin>>*(grades+count);
}
cout<<"Grades :";
for (int index=0;index<numGrades;index++)
{
cout<<setw(3)<<grades[index];
}
cout<<endl;
cout<<"The mode is: "<< findCommon(grades,freq , numGrades)<<endl;
return 0;
}
int findCommon(int grades[], int freq[],int numGrades)
{
int count=0;
for(int number=0;number<=numGrades;number++)
{
for (int compare=0;compare<=numGrades;compare++)
{
if (grades[number] == grades[compare])
{
count=count+1;
}
}
freq[number]=count;
count=0;
}
int most=freq[0];
for (int k=0;k<=numGrades; k++)
{
if (most < freq[k])
{
most=freq[k];
}
}
return freq[most];
//problem area: set most to original array????
}
[\code]
I know this is probably an easy one for anyone helping, but I' ve been staring at it too long, and my teacher can't show me.
Thanks,
George
-
Please read this, then edit your post accordingly. Thanks :)
[edit] I see you're only a character out.... wrong type of slash on the closing code tag. Good try :)
-
I just looked over your code quickly, but try using 'return most;', because you are assigning it the value of the most largest element of freq, not the index.
-
wow this is messy..
hmm here is an edited version of your code..
I dont have time to test/compile it. so dont blame me if doesnt work..
Code:
#include <iostream> //dont use .h use iso headers
#include <iomanip>
using namespace std;
int findCommon(int array[],int aray[],int number);
int main(void)
{
int numGrades;
int *grades;
int *freq;
cout<<"How many grades do you want to enter?<more than one please.>"<<endl;
cin>>numGrades;
grades=new int [numGrades];
freq= new int[numGrades];
for(int count=0;count<=numGrades;count++)
{
cout<<"Grade "<< count++ <<": ";
cin>>grades[count]; // I think this is what u meant?
}
cout<<"Grades :";
for (int index=0;index<=numGrades;index++)
{
cout<<setw(3)<<grades[index];
}
cout<<endl <<"The mode is: "<< findCommon(grades,freq , numGrades)<<endl;
return 0;
}
int findCommon(int grades[], int freq[],int numGrades)
{
int count=0;
for(int number=0;number<=numGrades;number++)
{
for (int compare=0;compare<=numGrades;compare++)
{
if (grades[number] == grades[compare])
{
count++;
}
}
freq[number]=count;
count=0;
}
int most=freq[0];
for (int k=0;k<=numGrades; k++)
{
if (most < freq[k])
{
most=freq[k];
}
}
return freq[most];
}
Luigi
-
I think this is your error:
change
Code:
for (int k=0;k<=numGrades; k++)
{
if (most < freq[k])
{
most=freq[k];
}
}
return freq[most];
to
Code:
int index=0;
for (int k=0;k<=numGrades; k++)
{
if (most < freq[k])
{
most=freq[k];
index=k;
}
}
return freq[index];
Also, make sure you set the frequency array to all zeros before doing anything with it.