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!
#include <iostream>
#include <cmath>
usingnamespace 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.