Creating a Mode function using array ponters

I am trying to create a mode to find the mode in an array of pointers. In my code works except for my find mode function. I set mode to return -1 if it is unable to find the mode and it is only returning the value of 100 which is the last number in my pointer array. I've included a few cout statements to test my pointer array. But I am not sure as how I can compare the the values within the pointer array. Below is my code:

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// This program displays the mode of a set of values.
#include <iostream>
using namespace std;

// Function prototypes
void arrSelectSort(int *[], int);
void showArray(const int [], int);
void showArrPtr(int *[], int);
int Find_Mode(int *[], int);

int main()
{
    const int NUM_DONATIONS = 15;           // Number of donations

    // An array containing the donation amount.
    int donations[NUM_DONATIONS] = { 5,  100, 5,  25, 10,
                                     5,  25,  5,  5,  100,
                                     10, 15,  10, 5,  10 };

    // An array of points to int.
    int *arrPtr[NUM_DONATIONS] = { nullptr, nullptr, nullptr, nullptr, nullptr,
                                   nullptr, nullptr, nullptr, nullptr, nullptr,
                                   nullptr, nullptr, nullptr, nullptr, nullptr};

    // Each element of arrPtr is a pointer to int. Make each
    // element point to an element in the donations array.
    for (int count = 0; count < NUM_DONATIONS; count++)
         arrPtr[count] = &donations[count];

    // Sort the elements of the array of pointers.
    arrSelectSort(arrPtr, NUM_DONATIONS);

    // Display the donations using the array of pointers. This
    // will display them in sorted order.
    cout << "The donations, sorted in ascending order, are: \n";
    showArrPtr(arrPtr, NUM_DONATIONS);

    // Display the donations in their original order.
    cout << "The donations, in their original order, are: \n";
    showArray(donations, NUM_DONATIONS);

    // Display the mode
    cout << " The Mode is: " << Find_Mode(arrPtr, NUM_DONATIONS) << endl;
    return 0;
}

//*******************************************************************
// Definition of function arrSelectSort.                            *
// This function performs an ascending order selection sort on      *
// arr, which is an array of pointers. Each element of array        *
// points to an element of a second array. After the sort,          *
// arr will point to the elements of the second array in            *
// ascending order.                                                 *
//*******************************************************************

void arrSelectSort(int *arr[], int size)
{
    int startScan, minIndex;
    int *minElem;

    for (startScan = 0; startScan < (size  - 1); startScan++)
    {
        minIndex = startScan;
        minElem = arr[startScan];
        for (int index = startScan +1; index < size; index++)
        {
            if (*(arr[index]) < *minElem)
            {
                minElem = arr[index];
                minIndex = index;
            }
        }
        arr[minIndex] = arr[startScan];
        arr[startScan] = minElem;
    }
}

//***************************************************************
// Definition of unction showArray.                             *
// This function displays the contents of arr. size is the      *
// number of elements.                                          *
//***************************************************************

void showArray(const int arr[], int size)
{
    for (int count = 0; count < size; count++)
        cout << arr[count] << " ";
    cout << endl;
}

//***************************************************************
// Definition of unction showArrPtr.                            *
// This function displays the contents of the array pointed to  *
// by arr. size is the number of elements.                      *
//***************************************************************

void showArrPtr(int *arr[], int size)
{
    for (int count = 0; count < size; count++)
        cout << *(arr[count]) << " ";
    cout << endl;

    // Display the donations using the array of pointers. This
    // will display them in descending order.
    cout << "The donations, sorted in descending order, are: \n";
    for (int count = (size - 1); count > 0; count--)
        cout << *(arr[count]) << " ";
    cout << endl;
}
//*******************************************
// Definition of Find_Mode() function.      *
//*******************************************
int Find_Mode(int *Array[], int Number_Elements)
{
    int counter;    // To hold the value of number of appearances
    int maximum = 0;    // To hold the value of maximum number of appearances
    int *mode;
    // Create a loop to loop through the pointer array values
    for (int i = 0; i < Number_Elements; i++)
    {
        cout << "The value of i is: " << i << "\nThe value of the pointer is: " << *(Array + i) << endl;
        // Reset the value of counter at the start of each iteration
        counter = 0;
        // Create a second loop to compare the pointer array values of *(Array + i) to *(Array + j)
        for (int j = i; j < Number_Elements; j++)
        {
            if (*(Array + i) == *(Array + j))
                counter++;                      // Increment counter if the values are equal
            if (counter > maximum)
            {
                maximum = counter;              // Set counter to the maximum appearance value.
                // Make mode point to the current mode
                mode = *(Array + i);
            }
            cout << "The value of j is: " << j << "\nThe value of the pointer is: " << *(Array + j) << endl;
            cout << "The value of counter is: " << counter << endl;
        }
    }

    // If the maximum number of appearances is 1 then return -1
    if (maximum == 1)
        return -1;
    return *mode;
}
Last edited on
You're comparing the pointers, not the values.
And you aren't resetting your counter back to 0!
So declare counter inside the loop so it inits to 0 each time, and compare the values:
1
2
3
4
5
6
        int counter = 0;
        for (int j = i; j < Number_Elements; j++)
        {
            if (*Array[i] == *Array[j])
                counter++;
        }

Since the input array is sorted, you don't need the double loop.
I'm not sure if this is entirely correct, but something like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int Find_Mode(int *Array[], int Number_Elements)
{
    int last = *Array[0]; // assuming at least 1 element
    int counter = 1, mode = last, maxcount = counter;
    for (int i = 1; i < Number_Elements; i++)
    {
        int curr = *Array[i];
        if (curr == last)
            ++counter;
        else if (counter > maxcount)
        {
            maxcount = counter;
            mode = last;
            counter = 1;
        }
        last = curr;
    }
    return counter <= 1 ? -1 : mode;
}

I was able to get my code to work by modifying it so it compared the values in my Find_Mode function. As such:

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
//*******************************************
// Definition of Find_Mode() function.      *
//*******************************************
int Find_Mode(int *Array[], int Number_Elements)
{
    int counter;    // To hold the value of number of appearances
    int maximum = 0;    // To hold the value of maximum number of appearances
    int *mode;
    // Create a loop to loop through the pointer array values
    for (int i = 0; i < Number_Elements; i++)
    {
        // Reset the value of counter at the start of each iteration
        counter = 0;
        // Create a second loop to compare the pointer array values of *(Array + i) to *(Array + j)
        for (int j = i; j < Number_Elements; j++)
        {
            if (*(Array[i]) == *(Array[j]))
                counter++;                      // Increment counter if the values are equal
            if (counter > maximum)
            {
                maximum = counter;              // Set counter to the maximum appearance value.
                // Make mode point to the current mode
                mode = Array[i];
            }
        }
    }

    // If the maximum number of appearances is 1 then return -1
    if (maximum == 1)
        return -1;
    return *mode;
}
Topic archived. No new replies allowed.