First Pointer Program

Firstly, I was trying to make this program using arrays and then I would attempt to change it into pointers with dynamically allocated memory. Which I will be needing a little assistance with.

The issues with the code I have presented is with the MODE, I'm unsure what I am doing incorrectly, but it only outputs the first number of the set of data.

And I'd appreciate a hint as to how to change my Reverse function to only print the first 32 reversed numbers of the data.

Here is the programming assignment:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
You are to write a program to compute some statistic information from a set of 
input numbers.  You are to read a set of numbers from the input file “numbers.txt”.    
There will be less than 100 numbers in the file, so allocate your 
array for 100 locations dynamically.  Read in the values and then sort the array.  

Compute mean of the array.  Them mean is the average to one decimal point. 
 Determine from the numbers in the array the ‘mode’.  The mode is the value that 
occurs most often in the list of numbers.  Find the ‘median’ of the set of values. 
 The median is the middle value in the list.  If the list has an odd number of 
values, then select the middle one.  If the list has an even number of values, then 
take the average of the two middle values in the list.  The last action is to reverse 
the values in the array and then print out the first 32 values of the list, 8 numbers 
per line.

Restrictions:  You must dynamically allocate your array.  You must use pointer to manipulate the array.  
No [ ]’s are to be found in your code.  You can use the [ ]’s for the ‘new’ command.

INPUT DATA: Sorted Data, didn't want to display 92 numbers down the page.
1
2
3
4
5
6
7
8
9
10
11
12
-56, -47, -43, -27, -27, -24, -14, -3, 
1, 2, 4, 5, 7, 8, 8, 9, 
11, 12, 15, 16, 16, 17, 20, 20, 
23, 23, 27, 27, 27, 28, 28, 32, 
32, 34, 35, 35, 37, 42, 42, 42, 
42, 45, 45, 45, 45, 45, 45, 46, 
46, 51, 51, 52, 53, 55, 56, 62, 
62, 64, 66, 72, 72, 72, 72, 72, 
72, 72, 72, 73, 73, 73, 73, 78, 
79, 82, 82, 82, 82, 82, 82, 83, 
83, 84, 85, 86, 87, 87, 88, 93, 
97, 99, 100, 101, 

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
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

//I know this isn't proper practice I just do this for ease.
ofstream out("Pointer.out");

const int MAX = 100;

//Prototypes
void read(int[],int&);
void print(int[], int&);
void mode(int[],int);
void median(int[],int);
void mean(int[], int);
void SelectSort(int[],int);
void Reverse(int[],int);

//function reads the data in.
void read(int ary[],int& cnt)
{
    
    ifstream inf("Data.in");// I know
   // ary = new int [MAX]; //Ignore this.
    int num;
    cnt=0;
    while(inf >> num && cnt < MAX)
    {
        ary[cnt] = num;
        cnt++;
    }
}

//Function Prints the data
void print(int ary[], int& cnt)
{
    for(int i=0;i<cnt; i++)
    {
        out << ary[i] << ", ";
        if((i+1) % 8 == 0)
            out << endl;
    }
}

//Function finds the mean of data
void mean(int ary[], int cnt)
{
    out << setprecision(1) << fixed;

    int sum=0;
    for(int i=0;i<cnt;i++)
        sum += ary[i];
    out << "\nThe MEAN is: " << double(sum/cnt) << endl;
}

//Function needs to find the mode, but i'm messing up someplace
void mode(int ary[], int cnt)
{
    int num = ary[0];
    int mode = num;
    int cntmode=1;
    cnt = 1;
    for (int i = 0; i < cnt; i++)
    {
        if ( ary[i] == num)
        {
            cnt++;
        }
            else
            {
                if(cnt > cntmode)
                {
                    cntmode = cnt;
                    mode = num;
                }
                    cnt =1;
                    num=ary[i];
            }
    }
    out << "\nThe MODE is: " << mode << endl;
}

//Functions finds the median of the data
void median(int ary[], int cnt)
{
     int median, mid;
     mid = cnt / 2;
     if (cnt % 2 == 0)
     {
        median =(ary[mid] + ary[mid - 1]) / 2;
     }
     else
     {
        median = (ary[mid]);
     }
     out << "\nThe MEDIAN is: " << median << endl;
}

//Copied this sort from the book
void SelectSort(int ary[], int cnt)
{
    int minIndex, minValue;
    for(int i=0;i<(cnt-1);i++)
    {
        minIndex=i;
        minValue=ary[i];
        for(int j= i+1; j<cnt;j++)
        {
            if(ary[j]<minValue)
            {
                minValue=ary[j];
                minIndex=j;
            }
        }
        swap(ary[minIndex],ary[i]);
    }
}

//Function needs to print the first 32 numbers.
void Reverse(int ary[], int cnt)
{
    for(int i=cnt-1;i>=0;i--)
    {
        out << ary[i] << ", ";
        if((i)%8==0)
            out << endl;
    }
}

int main()
{
    int ary[MAX], cnt;
    double avg;
    read(ary,cnt);
    SelectSort(ary,cnt);
    print(ary,cnt);
    mean(ary,cnt);
    mode(ary,cnt);
    median(ary,cnt);
    Reverse(ary,cnt);
    return 0;
}

OUTPUT:
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
-56, -47, -43, -27, -27, -24, -14, -3, 
1, 2, 4, 5, 7, 8, 8, 9, 
11, 12, 15, 16, 16, 17, 20, 20, 
23, 23, 27, 27, 27, 28, 28, 32, 
32, 34, 35, 35, 37, 42, 42, 42, 
42, 45, 45, 45, 45, 45, 45, 46, 
46, 51, 51, 52, 53, 55, 56, 62, 
62, 64, 66, 72, 72, 72, 72, 72, 
72, 72, 72, 73, 73, 73, 73, 78, 
79, 82, 82, 82, 82, 82, 82, 83, 
83, 84, 85, 86, 87, 87, 88, 93, 
97, 99, 100, 101, 
The MEAN is: 44.0

The MODE is: -56

The MEDIAN is: 45
101, 100, 99, 97, 
93, 88, 87, 87, 86, 85, 84, 83, 
83, 82, 82, 82, 82, 82, 82, 79, 
78, 73, 73, 73, 73, 72, 72, 72, 
72, 72, 72, 72, 72, 66, 64, 62, 
62, 56, 55, 53, 52, 51, 51, 46, 
46, 45, 45, 45, 45, 45, 45, 42, 
42, 42, 42, 37, 35, 35, 34, 32, 
32, 28, 28, 27, 27, 27, 23, 23, 
20, 20, 17, 16, 16, 15, 12, 11, 
9, 8, 8, 7, 5, 4, 2, 1, 
-3, -14, -24, -27, -27, -43, -47, -56, 
Last edited on
Basically, your mode function is just wrong.

How is it meant to work? What did you intend it to do?
No [ ]’s are to be found in your code.


You code has a LOT of [ ] in it.
Also you failed to dynamically allocate the array as specified in the instructions.

@Repeater @jib,
I said in the OP that I was trying to get the code to run with arrays(since I know how that works) and then translate it to pointers once I know the code is up and running properly.

I am trying to work out the bugs before I dive into something unfamiliar.

WIth the mode I'm trying to get the mode(number most frequent in the data).
The current Mode function I was looking throughout the interwebs for some sort of assistance, my current function is a rendition of what I have found online. I'm unsure of how to create a mode function properly.
The array is sorted.

Count how many of the first item there are. For example, if the array was:

1 1 1 3 3 4

Then the first item is 1, and there are three of them. This is the current winner.

When you get to the second item, start counting those. In this case, the second item is 3, and there are two of them.

Each time you get to a new group, check to see if there were more of the last group than the current winner. If there were, then the last group is the new current winner.

When finished, you have a winner.
I have updated the program with pointers. I'm stuck with the reverse function. The value 45 is the beginning value for the reverse, I suspect the median value is still "stuck" in the variable and isn't resetting. I need it to print the first reversed 32 numbers.
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

ofstream out("Pointer.out");

//Prototypes
void read(int*,int*&);
void print(int*, int*);
void mode(int*,int*);
void median(int*,int*);
void mean(int*, int*);
void Sort(int*,int*);
void Reverse(int*,int*);

//function reads the data in.
void read(int *ptr,int *&cnt)
{
    ifstream inf("Data.in");
    if(!inf)
        cout << "Error";
    else
    {
        while(inf >> *ptr)
       {
            cnt++;
            ptr++;
       }
    }
    inf.close();
}

//Function Prints the data
void print(int *ptr, int *cnt)
{
    for(ptr=ptr; ptr < cnt; ptr++)
    {
        out << *ptr << ", ";
        if((cnt-ptr) % 8 == 0)
            out << endl;
    }
}

//Function finds the mean of data
void mean(int *ptr, int *cnt)
{
    out << setprecision(1) << fixed;

    double sum=0;
    int *i = ptr;
    for(ptr=i;ptr<cnt;ptr++)
        sum += *ptr;
    out << "\n\nThe MEAN is: " << (sum/(cnt-i));
}

void mode(int *ptr, int *cnt)
{
   int mode = *ptr,
       num = 1,//Number of times it appears
       reps = 1;//Number appears at least once
   for(ptr = ptr; ptr < cnt; ptr++)
   {
       //IF the number occurs more than once add to counter
       if(*ptr == *(ptr-1))
        reps++;//Counter
       else//ELSE number occurs just once
        reps=1;//Occurrences
        //IF occurrences are greater than previous highest counted number
       if(reps > num)
       {
           //Replace with new number
           num = reps;
           //Mode equals the highest occurred number
           mode = *ptr;
       }
   }
    out << "\nThe MODE is: " << mode;

}

//Functions finds the median of the data
void median(int *ptr, int *cnt)
{
     int median, mid;
     mid = (cnt-ptr) / 2;
     //IF number has a remainder of 2 then it's even
     if ((cnt-ptr) % 2 == 0)
     {
        median =(*(ptr + (mid)) + *(ptr + ((mid)+1))) / 2.0;
     }
     //ELSE number is odd
     else
     {
        median = *(ptr + mid);
     }
     out << "\nThe MEDIAN is: " << median << "\n\n";
}

//Functions sorts the data from lowest to highest of value.
void Sort(int *ptr, int *cnt)
{
    int *minIndex=ptr;
    for(int *i=cnt-1;i>minIndex;i--)
    {
        for(ptr = minIndex; ptr < cnt; ptr++)
        {
            if(*ptr > *(ptr+1))
            {
                int temp = *(ptr+1);
                *(ptr+1) = *ptr;
                *ptr = temp;
            }
        }
    }
}

//Function needs to print the first 32 numbers.
void Reverse(int *ptr, int *cnt)
{
    out << "Reversed:\n";
    for(ptr=ptr; ptr < cnt; ptr++)
    {
        int temp=*cnt;
        *cnt=*ptr;
        *ptr=temp;
        cnt--;
    }
    cnt = ptr + 32;
    for(ptr=ptr; ptr < cnt; ptr++)
    {
        if((cnt - ptr) % 8 == 0)
            out << endl;
            out << *ptr << ", ";
    }
}

int main()
{
    int *ary = new int[100];
    int *ptr = ary;
    int *last = ary;

    out << "\t\t\tStatistic with Pointers Program\n";
    read(ary,last);
    out << "Unsorted:\n";
    print(ary,last);
    Sort(ary,last);
    out << "\n\nSorted:\n";
    print(ary,last);
    mean(ary,last);
    mode(ary,last);
    median(ary,last);
    Reverse(ary,last);

    return 0;
}

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
OUTPUT:
			Statistic with Pointers Program
Unsorted:
5, 20, 15, 45, 97, 
79, 42, 51, 72, 16, 46, 64, 72, 
51, 82, 46, 62, 72, 82, 53, 100, 
35, 23, -24, 84, 45, 7, -47, 73, 
83, 42, 45, -14, 82, 52, 73, 72, 
88, 42, 72, 82, 35, 27, 8, 32, 
37, -27, 99, 86, 82, 87, 28, 23, 
45, 2, 73, 28, 1, 72, 82, 85, 
27, 72, 83, 12, 16, 20, 55, 62, 
78, 27, 45, 45, 8, 32, 9, 93, 
17, 101, 72, 66, -43, -3, 34, -27, 
87, 73, 11, 56, -56, 42, 4, 

Sorted:
-56, -47, -43, -27, -27, 
-24, -14, -3, 0, 1, 2, 4, 5, 
7, 8, 8, 9, 11, 12, 15, 16, 
16, 17, 20, 20, 23, 23, 27, 27, 
27, 28, 28, 32, 32, 34, 35, 35, 
37, 42, 42, 42, 42, 45, 45, 45, 
45, 45, 45, 46, 46, 51, 51, 52, 
53, 55, 56, 62, 62, 64, 66, 72, 
72, 72, 72, 72, 72, 72, 72, 73, 
73, 73, 73, 78, 79, 82, 82, 82, 
82, 82, 82, 83, 83, 84, 85, 86, 
87, 87, 88, 93, 97, 99, 100, 

The MEAN is: 43.6
The MODE is: 72
The MEDIAN is: 45

Reversed:

45, 45, 45, 45, 45, 42, 42, 42, 
42, 37, 35, 35, 34, 32, 32, 28, 
28, 27, 27, 27, 23, 23, 20, 20, 
17, 16, 16, 15, 12, 11, 9, 8, 


EDIT: I just realized my sorted numbers are incorrect. It isn't printing the 101 which is in the data file. Somewhere between sort and print the "array of numbers" is producing a zero to input into the sorted printout. A 0 is not a part of the infile.
Last edited on
Topic archived. No new replies allowed.