Trying to find the mode of an array

Hello all, and thanks in advance for any help you provide me. What I'm trying to do is find the mode of an int array, or most often occurring value. Below is what I've been working on so far. The user for all intents and purposes enters whatever values he want's in ascending order. I.E he enters 1,2,3,4,4,4,5,6,7,8 so the mode would be '4'. Trying to work within what I already know about c++.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

int array[10];
int array2[10];
int index=1;

bool equals=false;

for (int i=0; i<10; i++) //I use this for loop to put values
{                        //into the first array in ascending
 cin >> array[i];        //order
}

for(int x=0; x<10; x++)  //This will be the loop where I use the
{                        //other array to compare values
                         
  do  
   {                           //Went with  a nested do while loop to move
    if(array[x] = array[x+1)   //through both arrays
      {
        array2[x] = index++
      }
    else
      {
       array2[x] = index;
      }

   }while(!equals)

}


The problem is I'm not sure where to go from there. I'll try to update this later tonight when I can take another look at it. But I think I have to go back and somewhere try to link the indexes between the two arrays so when I find the most occurring in the second array I will have it in the first.

This is still a work in progress I just want to know if I'm heading in the right direction for the most part or if I should head in another. Again thanks.
I think this might help this topic has already been covered in the forum: http://www.cplusplus.com/forum/general/9787/
Hmm that thread didn't really help me understand what I need to do. It just hasn't clicked in my brain where I get it. I just need help with my code so that maybe I can understand it better without using any of the libraries. Just the two arrays and moving through each.

Here's where I'm at right now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

   int array[10];
   int array2[10];
   int mode=0, index=0, countMode=1;

    for (int e=0; e<10; e++)
    {
        if(array[e] == array[e+1])
        {
            array2[e] = countMode++;
        }

    }

    for(int f=0; f<10; f++)        //got rid of do while for for loops
    {
        if(array2[f]>array2[f+1])
        {
            f = index;
        }                                     //still not getting the mode to come up
    }                                         //and getting run time errors as program doesnt
    mode = array[index];         //return 0
    cout << "The mode of the array is " << mode << endl;
Last edited on
Good Im against do while loops I don't see the difference from
A normal while loop but I'll try to have it to you by tommorow I have a ton of Work
Good Im against do while loops I don't see the difference from
A normal while


A do-while has the condition checked at the end of the loop; a while has the condition checked at the start.
A do-while has the condition checked at the end of the loop; a while has the condition checked at the start.

That's interesting but i still do not see a very good reason for use
The do-while guarantees you get one execution of the loop. The while can finish without any executions of the loop.
Last edited on
I don't like do loops either - I only use them if I have to - which is rare.

Even though they always execute once before testing the condition, it quite often possible to write a while or for loop that behaves the same way.

If I can name drop massively - Kernighan & Ritchie (Inventors of C) along with BjarneStroustrup don't like them either.
it quite often possible to write a while or for loop that behaves the same way.


That's very true. The same can be said of using goto instead of any loops at all, or writing the whole thing in assembler. It's just another tool to use that sometimes is the best choice and sometimes is not.
That's very true. The same can be said of using goto instead of any loops at all, or writing the whole thing in assembler. It's just another tool to use that sometimes is the best choice and sometimes is not.


Yes.

I was pushing my own preference:

I don't like do loops either - I only use them if I have to - which is rare.
Thanks for all the input. And yea I try to stay away from Do While as well. I finally figured it out, if someone wants I can post the code but it works for me using two loops. One counting the digits in the other.
I'd love to see the code sounds interesting
Topic archived. No new replies allowed.