Help with Sorting Array

closed account (zqXLy60M)
I cannot figure out why my sorter is bring back the array address for the largest value. If you could please help! :-/
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
#include <iostream>
#include <cmath>
using namespace std;


float meanFunction(float *arr, int count);
float medianTotal(float *arr, int count);
float modeFunction(float *arr, int count);
void sortFunction(float *arr, int count);


int main() {
	int count = 0, go_on = 0;
    float num[100], mean, median, *arr = num;
	tryAgain: // Statement Label
	for (int i = 0; i < 40; i++) {
		cout << "**";
	}
    cout << "This program will calculate the mean, median, mode, and standard deviation. " << endl;
    cout << "Enter the amount of values that you will select (must be 100 or less)." << endl;
	for (int i = 0; i < 40; i++) {
		cout << "**";
	}
	cin >> go_on;
	if (go_on > 100) { // validating array size
		cout << "Please follow the directions!" << endl;;
		goto tryAgain;
	}
	cout << "You have entered " << go_on << " values." << endl;
    do {
        cout << "Enter a number: ";
        cin >> num[count];
        count++;
    }
    while (count < go_on);
	median = medianTotal(arr, count); // call for median function
	mean = meanFunction(arr,count); // call for mean function
	cout << "The Mean is: " << mean << endl;
	cout << "The Median is: " << median << endl;
	sortFunction(arr,count); // call for sort function
	cout << "Would you like to run the program again (Enter 1 for yes & 2 for no?)" << endl;
	int startOver = 2; // loop to rerun program
	cin >> startOver;
	if (startOver == 1) {
		goto tryAgain;
		delete [] num;
	}
	else {
		cout << "GOOD BYE!" << endl;
	}
    return 0;
}

void sortFunction(float *arr, int count) {
	int i, swapHolder = 0, end = count;
	float mode;
	for (int counter = count - 1; counter > 0; counter--) {
		for(i = 0; i < end; i++) {
			if (arr[i] > arr[i + 1]) {
				swapHolder = arr[i + 1];
				arr[i + 1] = arr[i];
				arr[i] = swapHolder;
			}
			cout << arr[i] << ", ";
		}
		end--;
	}
	mode = modeFunction(arr, count);
	cout << "The Mode is: " << mode << endl;
}


float modeFunction(float *arr, int count) {
	int currentNumCount = 0, mostFoundCount = 0, i = 0;
	float currentNum = arr[i], mostFound = arr[i];
	for (i = 0; i < count; i++) {
		if (currentNum == mostFound) {
			currentNumCount++;
		} 
		if (currentNumCount > mostFoundCount) {
			mostFound = currentNum;
			mostFoundCount = currentNumCount;
			currentNumCount = 1;
		}
		if (currentNumCount < mostFoundCount) {
			currentNumCount++;
		}
		currentNum = arr[i];
	}
	return mostFound;
}

float meanFunction(float *arr, int count) {
	float sum = 0;
	for (int i = 0; i < count; i++) {
		sum += arr[i];
	}
	return (sum / count);
}

float medianTotal(float *arr, int count){
	int middle = (count / 2);
	float average; 
	if ((count % 2) == 0){
		average = ((arr[middle - 1] + arr[middle]) / 2);
	}
	else {
		average = (arr[middle]);
	}
	return average;
}
closed account (zqXLy60M)
I am also still trying to get the mode function to work but no point in trying until I get the sort function working. Thank you all for the help!
I'm not sure I understand your question.

First, there's a mistake in your sort function. If "end" starts at "count" and i goes up to end-1, then i+1 can be equal to "count".
To correct that, remove the "end" variable and change your inner loop for
for(i = 0; i < counter; i++).

Second, you call medianTotal() before sorting the array. That won't work.

Third, your modeFunction(): I think it's missing an "else" in the loop, and currentNumCount should be re-initialized every time the current value changes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for (i = 0; i < count; i++)
{
    if (arr[i] == currentNum) {
        currentNumCount++;
    } 
    else
    {
        if (currentNumCount > mostFoundCount) {
            mostFound = currentNum;
            mostFoundCount = currentNumCount;
        }
        currentNum = arr[i];
        currentNumCount = 1;
    }
}
closed account (zqXLy60M)
Toum... Again thank you for your help! I made the changes to the sortFunction but it keeps leaving off the largest number entered.

new 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
#include <iostream>
#include <cmath>
using namespace std;


float meanFunction(float *arr, int count);
float medianTotal(float *arr, int count);
float modeFunction(float *arr, int count);
void stdFunction(float mean, int count);
void sortFunction(float *arr, int count);


int main() {
	int count = 0, go_on = 0;
    float num[100], mean, median, *arr = num, mode;
	tryAgain: // Statement Label
	for (int i = 0; i < 40; i++) {
		cout << "**";
	}
    cout << "This program will calculate the mean, median, mode, and standard deviation. " << endl;
    cout << "Enter the amount of values that you will select (must be 100 or less)." << endl;
	for (int i = 0; i < 40; i++) {
		cout << "**";
	}
	cin >> go_on;
	if (go_on > 100) { // validating array size
		cout << "Please follow the directions!" << endl;;
		goto tryAgain;
	}
	cout << "You have entered " << go_on << " values." << endl;
    do {
        cout << "Enter a number: ";
        cin >> num[count];
        count++;
    }
    while (count < go_on);
	sortFunction(arr,count); // call for sort function
	median = medianTotal(arr, count); // call for median function
	mean = meanFunction(arr,count); // call for mean function
	mode = modeFunction(arr, count);
	cout << "The Mode is: " << mode << endl;
	cout << "The Mean is: " << mean << endl;
	cout << "The Median is: " << median << endl;
	stdFunction(mean, count);
	cout << "GOOD BYE!" << endl;
    return 0;
}


void stdFunction(float mean, int count) {
	float stdAnswer1, stdAnswer2;
	stdAnswer1 = (mean * mean);
	stdAnswer1 = (stdAnswer1 / (count - 1));
	stdAnswer1 = sqrt(stdAnswer1);
	stdAnswer2 = stdAnswer1;
	stdAnswer1 = (mean - stdAnswer1);
	stdAnswer2 = (mean + stdAnswer2);
	cout << "The Standard Deviation is: " << stdAnswer1 << " & " << stdAnswer2 << endl;
}

void sortFunction(float *arr, int count) {
	int i;
	float mode, swapHolder;
	for (int counter = (count - 1); counter > 0; counter--) {
		for(i = 0; i < count; i++) {
			if (arr[i] > arr[i + 1]) {
				swapHolder = arr[i + 1];
				arr[i + 1] = arr[i];
				arr[i] = swapHolder;
			}
		}
	}
	for (int l = 0; l < count; l++) {
		cout << arr[l] << endl;
	}
}


float modeFunction(float *arr, int count) {
	int currentNumCount = 0, mostFoundCount = 0, i = 0;
	float currentNum = arr[i], mostFound = arr[i];
	for (i = 0; i < count; i++) {
		if (arr[i] == currentNum) {
			currentNumCount++;
		}
		else {
			if (currentNumCount > mostFoundCount) {
				mostFound = currentNum;
				mostFoundCount = currentNumCount;
			}
			currentNum = arr[i];
			currentNumCount = i;
		}
	}
	return mostFound;
}

float meanFunction(float *arr, int count) {
	float sum = 0, meanAnswer;
	for (int i = 0; i < count; i++) {
		sum += arr[i];
	}
	meanAnswer = (sum / count);
	return meanAnswer;
}

float medianTotal(float *arr, int count){
	int middle = (count / 2);
	float average; 
	if ((count % 2) == 0){
		average = ((arr[middle - 1] + arr[middle]) / 2);
	}
	else {
		average = (arr[middle]);
	}
	return average;
}
closed account (zqXLy60M)
output posted:

********************************************************************************
This program will calculate the mean, median, mode, and standard deviation.
Enter the amount of values that you will select (must be 100 or less).
********************************************************************************
10
You have entered 10 values.
Enter a number: 44
Enter a number: 50
Enter a number: 38
Enter a number: 96
Enter a number: 42
Enter a number: 47
Enter a number: 40
Enter a number: 39
Enter a number: 46
Enter a number: 50
38
-1.07374e+008
39
40
42
44
46
47
50
50
The Mode is: 47
The Mean is: -1.07374e+007
The Median is: 43
The Standard Deviation is: -1.43165e+007 & -7.15825e+006
GOOD BYE!
Press any key to continue . . .
closed account (zqXLy60M)
changed my code and now it works...

1
2
3
4
5
6
7
8
9
10
11
void sortFunction(float *arr, int count) { // bubble sort
	for(int i = 0; i < count; i++) {
      for(int j = i; j < count; j++) {
		 if(arr[i] < arr[j]) {
			 float temp = arr[i];
             arr[i] = arr[j];
             arr[j] = temp;
          }
       }
    }
}
I suggested that "i" stops before counter, not count.

It's simple: in your loop, the biggest index you're reading at is i+1.
That means you must have i+1 < count, ie i < count - 1.

Given your definition of counter, you could also improve your algorithm by changing your loop condition to i < counter.
The idea is that the inner loop will always put the biggest element at the end. So, after each inner loop you're guaranteed that the biggest of the elements you've looped through is correctly positioned.
It's then useless to check it during the next inner loop.
Your inner loop size can thus be reduced by 1 every outter loop.
Topic archived. No new replies allowed.