Sorting a array to numerical order problem

I need to find the Mean, median, mode, and make a histogram of a 99 value array. How ever my sorting function is not sorting the array at all how can I fix this.
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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <math.h>
#include <cctype>

using namespace std;

void sort(int x[], int n)
{
	int m, sum = 0;
	int hold, median, average = 0;


	for (int k = 0; k <= n - 2; k++)
	{
		m = k;
		for (int j = k + 1; k <= n - 2; k++)
		{
			if (x[j]<x[m])
			{
				m = j;
			}
		}
		hold = x[m];
		x[m] = x[k];
		x[k] = hold;
	}
	for (int b = 0; b<99; b++)
	{
		cout<<x[b]<<" ";
	}
	
	for (int i = 0; i<99; i++)
	{
		sum = sum + x[i];
	}
	average = sum / 99;
	cout << "Average: " << average << endl;

	cout << "Mode: " << x[49] << endl;
	


}

int main()
{
	char fileName[16];
	cout << "Enter file name\n";
	cin >> fileName;
	ifstream in;
	in.open(fileName);

	int x[99];
	if (in.fail())
	{
		cout << "Wrong name\n";

		exit(1);
	}
	for (int i = 0; i<99; i++)
	{
		in >> x[i];
	}
	sort(x, 99);



	return 0;
}

P.S. I also need help with the histogram and the median.
line 18: for (int j = k + 1; k <= n - 2; k++)
read carefully.
by the way, you never touch the last element (x[n-1])

Also, don't try to do everything in the sort() function
I tried changing the 2 to a 1 and it still didnt work. I Dont know what to do.
Again, read carefully.
In line 18 your condition is k <= n - 2 where it should be j <= n - 1
same for the increment, you've got k++ instead of j++
Thank you. How can I find the mode and the histogram.
I have this for the mode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
for (int i=1; i<99; i++)
{
	int number=x[0];
	int mode = number;
	int count = 1;
	int countMode = 1;
      if (x[i] == number) 
      { 
         countMode++;
      }
      else
      {
            if (count > countMode) 
            {
                  countMode = count;
                  mode = number;
            }
           count = 0; 
           number = x[i];
  }
}
	cout<<"Mode: "<<mode<<endl;

However it does not give the correct value.
How can I fix it.
Last edited on
First analyze it.
Lines 3--6 are inside the loop, so they would be executed at every iteration. That means that `count' couldn't be greater than 1.

Then in line 13 you say if (count > countMode), however nowhere in your code you touch `count' (except for line 18, that resets it)


By the way, ¿what would be the mode if several numbers have the maximum repetition?


The histogram would be the count of the repetition of each number (perhaps grouped somehow)
You'll need another array which size is the number of ranges you are interested in.
count is used on line 15. I really cant figure out the mode at all and I do not know where to start with the histogram.
Last edited on
you never change the value of `count'
I found How to do the mode.
Last edited on
Topic archived. No new replies allowed.