# calculating the mode of an array

Page 1 of 1

## 5 Replies - 27202 Views - Last Post: 17 August 2008 - 07:59 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=60837&amp;s=c546c091213011178522b0290c23a953&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 sms123

Reputation: 0
• Posts: 64
• Joined: 15-August 08

# calculating the mode of an array

Posted 16 August 2008 - 09:22 AM

hey guys i have a program which allows the user to enter the size of an array and then fill it with integers, i want it then to display which number appears the most (the mode).
right now if i make an array of 8 digits and fill it with 3,4,3,3,4,2,2,2 it then prints out this;

Enter amount of numbers
8
1.)3
2.)4
3.)3
4.)3
5.)4
6.)2
7.)2
8.)2

Results:
2 appeared 3 time(s).
3 appeared 3 time(s).
2 appeared 2 time(s).
3 appeared 2 time(s).
4 appeared 2 time(s).
2 appeared 1 time(s).
4 appeared 1 time(s).
3 appeared 1 time(s).
Press any key to continue . . .

here's my code;
```#include <cstdlib>
#include <iostream>

using namespace std;
//Variables
int counter=1,temp,temp2,n,x,y,z;
bool swapOccur;
//Functions
int main(int argc, char *argv[]){
cout << "Enter amount of numbers" << endl;
cin >> n;
int array[n],counterArray[n];
for(x=0;x<n;x++){
cout << x+1 << ".)";
cin >> array[x];
}
for(y=0;y<n;y++){
counter=1;
for(z=(y+1);z<n;z++){
if(array[y]==array[z]){
counter ++;
}
}
counterArray[y] = counter;
}
swapOccur = true;
while(swapOccur){
swapOccur = false;
for(z=0;z<(n-1);z++){
if(counterArray[z]>counterArray[z+1]){
temp = counterArray[z];
temp2 = array[z];
counterArray[z] = counterArray[z+1];
array[z] = array[z+1];
counterArray[z+1] = temp;
array[z+1] = temp2;
swapOccur = true;
}
}
}
cout << "\nResults:\n";
for(y=0;y<n;y++){
cout << array[(n-y)-1] << " appeared " << counterArray[(n-y)-1] << " time(s)."<< endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}

```

Is This A Good Question/Topic? 0

## Replies To: calculating the mode of an array

### #2 sms123

Reputation: 0
• Posts: 64
• Joined: 15-August 08

## Re: calculating the mode of an array

Posted 17 August 2008 - 01:37 AM

Come on guys the problem is that it checks how many time the number appears when it first finds it, then if it finds the same number later on in the array it checks how many times it appears again.

### #3 joske

• D.I.C Regular

Reputation: 43
• Posts: 297
• Joined: 04-September 07

## Re: calculating the mode of an array

Posted 17 August 2008 - 06:48 AM

you can not dynamically create an array as you try to do:
```int n;
cin >> n;
int array[n],
int counterArray[n];
```

You can do two things: declare an array that is big enough for all your problems, or use pointers to create a dynamically sizable array.

first solution:
```int MAX_SIZE = 1000;
int array[MAX_SIZE],
int counterArray[MAX_SIZE];
cin >> n;
if (n > MAX_SIZE)
{
cout << "Sorry, to big array" << endl;
}

```

second solution:
```int n;
cin >> n;
int* array = new int[n],
int* counterArray = new int[n];

// .... do something

// at the end, clean up memory
delete array;
array = 0;
delete counterArray;
counterArray = 0;

```

### #4 sms123

Reputation: 0
• Posts: 64
• Joined: 15-August 08

## Re: calculating the mode of an array

Posted 17 August 2008 - 07:24 AM

joske, on 17 Aug, 2008 - 06:48 AM, said:

you can not dynamically create an array as you try to do:
```int n;
cin >> n;
int array[n],
int counterArray[n];
```

You can do two things: declare an array that is big enough for all your problems, or use pointers to create a dynamically sizable array.

first solution:
```int MAX_SIZE = 1000;
int array[MAX_SIZE],
int counterArray[MAX_SIZE];
cin >> n;
if (n > MAX_SIZE)
{
cout << "Sorry, to big array" << endl;
}

```

second solution:
```int n;
cin >> n;
int* array = new int[n],
int* counterArray = new int[n];

// .... do something

// at the end, clean up memory
delete array;
array = 0;
delete counterArray;
counterArray = 0;

```

why not? it works fine, try it.

### #5 baavgai

• Dreaming Coder

Reputation: 7070
• Posts: 14,743
• Joined: 16-October 07

## Re: calculating the mode of an array

Posted 17 August 2008 - 07:55 AM

To be perfectly honest, given your code, I have no idea what you're thinking. I don't want to give the answer, but the approach is very short. Here's the steps you can take:

1. loop through the array
2. set count for current position to 1
3. look for the current value in the array ( ignoring self )
4. if you find current value, set count position found to +1 and set count for current position to 0, break from loop

Then, your results would look like:
```cout << "\nResults:\n";
for(int i=0; i<n; i++) {
if (counterArray[i]>0) {
cout << array[i] << " appeared " << counterArray[i] << " time(s)."<< endl;
}
}

```

Another way to do it would be to simply sort the array you have, then loop through it, keeping at count and keeping track of the last value. When the current value doesn't match the last value ( and isn't the first value ), print the count and set it to 1.

Hope this helps.

### #6 sms123

Reputation: 0
• Posts: 64
• Joined: 15-August 08

## Re: calculating the mode of an array

Posted 17 August 2008 - 07:59 AM

baavgai, on 17 Aug, 2008 - 07:55 AM, said:

To be perfectly honest, given your code, I have no idea what you're thinking. I don't want to give the answer, but the approach is very short. Here's the steps you can take:

1. loop through the array
2. set count for current position to 1
3. look for the current value in the array ( ignoring self )
4. if you find current value, set count position found to +1 and set count for current position to 0, break from loop

Then, your results would look like:
```cout << "\nResults:\n";
for(int i=0; i<n; i++) {
if (counterArray[i]>0) {
cout << array[i] << " appeared " << counterArray[i] << " time(s)."<< endl;
}
}

```

Another way to do it would be to simply sort the array you have, then loop through it, keeping at count and keeping track of the last value. When the current value doesn't match the last value ( and isn't the first value ), print the count and set it to 1.

Hope this helps.

i see your point, i did that the first time, but the problem is say array[1] is 5 and array[7] is 5 aswell
then it says
five appeared twice
five appeared once
i want it to not display the same number again