# Thread: returning mode value from array

1. ## returning mode value from array

I have an idea of how I want to tackle this but its hard for me to implement in code. I want the function to check each element and if it spots another one just like it, to do an incrementation. I need a nudge. I'm not sure how to take that first step. Any help as always is appreciated.

Code:
```      #include <iostream>
using namespace std;
int mode(int a[],int size );//prototype
int main()
{

const int size=10;
int a[size]={1,2,-3,-4,-5,-6,7,8,-9,10};

mode(a,size);//call

return 0;

}

int mode(int a[], int size) //returns mode element of array
{
int m=0;
for(int i=0;i<size;i++){
if(i                               //????????
a[i];
}

}```

2. >>check each element and if it spots another one just like it, to do an incrementation.
You mean check for duplicates? And what do you mean by "do an incrementation"? You mean if another element is the same as the one you're checking, make the other element 1 bigger?

**EDIT**
Judging from your function name, you're trying to find the element that has the biggest number of repetitions? Personally I would use a std::map to do it, but I assume you're not into that yet. You can create another array, which is used to store the counts of each element, and then print the element with the highest count:
Code:
```int array1[10] = {1,2,2,3,4,5,5,5,6,1};
int counts[10] = {0,0,0,0,0,0,0,0,0,0};

//Count the repetitions of each element
for(int i = 0; i < 10; ++i)
{
for(int j = 0; j < 10; ++j)
{
if(array1[j] == array1[i])
++counts[i];
}
}

//Find the element with the highest count
int highestIndex = 0;
for(i = 1; i < 10; ++i)
{
if(counts[i] > counts[highestIndex])
highestIndex = i;
}

cout << array1[highestIndex]; //print out the number with the most repetitions```

3. I just mean I want the function to count how many times each element is present in the array. The element that has the highest count would be the mode which is what I want the function to return.

4. Ah yeah, I figured that out a second later... I edited my post, take a look at it and see if you can tell what it does (it's definitely not the most efficient way to do it, but it is one way).

5. Couldn't another way also involve using a switch statement?

6. I don't believe so, although if you can figure something out that would be great.

7. Ok im almost done just keep getting this one error. grrrrr.
Code:
```#include <iostream>
using namespace std;
int mode(int freq[],int a[],int size );//prototype
int main()
{

const int size=10;
int a[size]={1,2,-3,-4,-5,-6,7,8,-9,10};

mode(freq,a,size);//call

return 0;

}

int mode(int freq[], int a[], int size) //reverse order of elements in array
{

int largest=0;
int modeValue=0;

for(int i=0;i<size;i++)
freq[i]=0; //initializing frequency to 0

//summaring frequencies

for (int j=0; j<size; j++)
++freq[a[j]];

//output results

for (int rating=1;rating<size;rating++){
cout<<rating<<freq[rating]<<endl;

if(freq[rating]>largest) {
largest=freq[rating];
modeValue=rating;
}
return modeValue;
}

}```

Ok i only have one error left to take care of which is this:
Code:
`mode.cpp(17) : error C2065: 'freq' : undeclared identifier`
Not sure why i keep getting this, any help would be appreciated.

8. Dantestwin - you seem to be posting a lot of threads like this. If I may teach a quick lesson in debugging:

mode.cpp(17) - the file in which the error occurred (which you will need to know when you start building larger projects consisting of multiple files), and the line number. Look at the location specified here, as you will oftentimes notice that you just made a simple typo. It's really useful to concentrate on one area, and since the compiler tells you exactly where there's a problem, it's made easy.

error C2065 - a lot of compilers distribute a list of error codes and what they mean. In this case, you've got a bit of a summary at the end.

'freq' : undeclared identifier - just try and think about what this is trying to tell you, although it can be hard since it uses a lot of PC buzz words that newbies aren't likely to know. In this case, let me talk you through how I would do it. First of all, it's talking about 'freq', which you know is a variable that you made. "Undeclared identifier" - it's simply a variable that hasn't been declared properly. The actual cause for this could be that the variable has gone out of scope or wasn't declared.

edit: To be specific, in this case you simply didn't declare or initialize freq, like you did for size, etc...

9. thanks sean, i fixed the bug now. It compiles fine and everything but there is one logical error. The biggest element in the array is 10 and it the program prints out the current frequency for every other element but 10. for some reason, it always adds 1. like if 10 occurred 3 times, it would print out 4. and so forth. any help as to what glitch in my code is sparking this error would be greatly appreciated.

Code:
```#include <iostream>
using namespace std;
int mode(int freq[],int a[],int size );//prototype
int main()
{

const int size=10;
int a[size]={1,10,3,5,5,10,7,7,7,10};
int freq[size]={0};

cout<< mode(freq,a,size);//call

return 0;

}

int mode(int freq[], int a[], int size) //reverse order of elements in array
{

int largest=0;
int modeValue=0;

for(int i=0;i<size;i++)
freq[i]=0; //initializing frequency to 0

//summarizing frequencies

for (int j=0; j<size; j++)
++freq[a[j]];

//output results

for (int rating=1;rating<=size;rating++){
cout<<rating<<' '<<freq[rating]<<endl;

if(freq[rating]>largest) {
largest=freq[rating];
modeValue=rating;
}

}
return modeValue;

}```

10. Code:
```for (int rating=1;rating<=size;rating++){
cout<<rating<<' '<<freq[rating]<<endl;

if(freq[rating]>largest) {
largest=freq[rating];
modeValue=rating;
}```
'rating' should loop from 0 to size-1. Here is the correct code:
Code:
```for (int rating=0;rating<size;rating++){
cout<<rating<<' '<<freq[rating]<<endl;

if(freq[rating]>largest) {
largest=freq[rating];
modeValue=rating;
}```

Popular pages Recent additions