Sorting Array, finding min, max.

Howdy all, i need to find the the min, max, variance, deviation of 16 numbers, and display the numbers entered in 4 rows of 4 numbers. I got everything to work except min and max, for some reason they are pumping out weird numbers and i cant figure out why. Also i do not get how to make rows display. Thanks!



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

int main()
{

int n , j ,y;
double _sum=0 , variance;

cout << "How many numbers will be entered?" << endl;
cin >> y;
double max[16];
for(n=0;n<y;n++)
{
cout << "Type next number: ";
cin >> max[n];
}
n=0;
cout << endl;
cout << "Numbers are:\t";
for(j=0;j<y;j++)
{
cout << max[n] << "\t";
_sum+=max[n];
n++;
}
double mean = _sum/n;
cout <<endl<< "The mean is:\t " << mean << endl;
n=0;
_sum=0;
for(j=0;j<y;j++)
{
max[n]=pow((max[n]-mean),2);
_sum+=max[n];
n++;
}
variance=_sum/(n-1);
cout << "The smallest element is " << *min_element(max,max+16) << '\n';
cout << "The largest element is "  << *max_element(max,max+16) << '\n';
cout << "Variance is: " << variance << endl;
cout << "Standard Deviation is: " << sqrt(variance) << "\n\n";
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
How many numbers will be entered?
6
Type next number: 5
Type next number: 6
Type next number: 4
Type next number: 3
Type next number: 2
Type next number: 8

Numbers are:    5       6       4       3       2       8
The mean is:     4.66667
The smallest element is 0
The largest element is 11.1111
Variance is: 4.66667
Standard Deviation is: 2.16025
Print out elements of array just before calling min_element and max_element. It looks that you spoil your array during other calculations (for no good reason).
The sample execution you provide says that you only enter 6 numbers, therefore you only enter elements 0 - 5 (inclusive). The issue arises when you call min and max [_element]:
*min_element(max,max+16)
Elements 6 - 15 (inclusive) are garbage (in your example), but you include them anyway.

You also have some repetitive code, but we'll leave that for now.
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
#include <iostream>
#include <cmath>
using namespace std;

int main()
{

int n , j ,y;
double _sum=0 , variance;

cout << "How many numbers will be entered?" << endl;
cin >> y;
double max[16];
for(n=0;n<y;n++)
{
cout << "Type next number: ";
cin >> max[n];
}
n=0;
cout << endl;
cout << "Numbers are:\t";
for(j=0;j<y;j++)
{
cout << max[n] << "\t";
_sum+=max[n];
n++;
}
cout <<endl<<endl<< "The smallest element is " << *min_element(max,max+6) << '\n';
cout << "The largest element is "  << *max_element(max,max+6) << '\n';
double mean = _sum/n;
cout << "The mean is:\t " << mean << endl;
n=0;
_sum=0;
for(j=0;j<y;j++)
{
max[n]=pow((max[n]-mean),2);
_sum+=max[n];
n++;
}
variance=_sum/(n-1);
cout << "Variance is: " << variance << endl;
cout << "Standard Deviation is: " << sqrt(variance) << "\n\n";
}


Printing it out before calling it worked. I changed it to 6 purely for testing purposes.

Now working on getting rows to work is what i am working on now. Any tips would be helpful! :D thanks!
Topic archived. No new replies allowed.