need to find the mode using arrays and pointers?

closed account (zw6q5Di1)
Im supposed to write a program that has a function that accepts an array of integers and an integer that indicates the number of elements in the array as arguments. This function should determine the mode (most frequent number) of the array.If there's no mode, then it should return -1 as an answer. Program is only supposed to use pointer notation (not array notation).

this is what I have so far, but i got so confused and some things got erased... could someone fix this file so it works?

#include <iostream>
using namespace std;

int main()
{

int size, *values;

cout << "Size = ? ";
cin >> n;

values = new int[size];

for( int i=0; i<size; i++)
{
cout << "values[" << i << "] = ? ";
cin >> values[ i ];
}


int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}

qsort(values, size, sizeof(int), compare);

int maxFrequency = -1;
int mode = values[0];
bool foundManyElementsWithMaxFrequency = false;

for(int j=0; j<size; )
{
int cur = values[j];
int localFrequency = 0;
while(values[j] == cur)
{
localFrequency++;
j++;
}

if(localFrequency > maxFrequency)
{
maxFrequency = localFrequency;
mode = cur;
foundManyElementsWithMaxFrequency = false;
}
else if(localFrequency == maxFrequency)
{
foundManyElementsWithMaxFrequency = true;
}
}

if( foundManyElementsWithMaxFrequency )
mode = -1;

cout << "\n Mode = " << mode;
closed account (S6k9GNh0)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
using namespace std;

int main()
{
  int size, *values;

  cout << "Size = ? ";
  cin >> n;

  values = new int[size];

  for( int i=0; i<size; i++)
  {
    cout << "values[" << i << "] = ? ";
    cin >> values[ i ];
  }

int compare (const void * a, const void * b) //You can't create a function inside of a function.
{                                                                   //This should error you out.
  return ( *(int*)a - *(int*)b );
}

  qsort(values, size, sizeof(int), compare); //?

  int maxFrequency = -1;
  int mode = values[0];
  bool foundManyElementsWithMaxFrequency = false; //Can't help but laugh. This name needs to be simplified.

  for(int j=0; j<size; )
  {
    int cur = values[j]; //int cur doesn't describe the variable. Be more elaborate but be brief.
    //Declare these variables outside of the for loop. 
    int localFrequency = 0; //What's the point of this? 
    while(values[j] == cur)
    {
      localFrequency++;
      j++;
    } //<==You were missing this.
  }

  if(localFrequency > maxFrequency)
  {
    maxFrequency = localFrequency;
    mode = cur;
    foundManyElementsWithMaxFrequency = false;
  }

  else if(localFrequency == maxFrequency)
  {
    foundManyElementsWithMaxFrequency = true;
  }

}

if( foundManyElementsWithMaxFrequency ) // This is outside of the main function. This will never be executed and I don't think it compiles.
  mode = -1;

cout << "\n Mode = " << mode; //Same thing with this. 


On this note, practice variable naming, and documenting.
Commenting your code also helps yourself out on design which is a problem here.
Last edited on
Topic archived. No new replies allowed.