So im currently working on an assignment and I have completed it. It matches the
ID: 672695 • Letter: S
Question
So im currently working on an assignment and I have completed it. It matches the example output but when I upload it to our auto grader I get back an error saying segmentation fault core dump on line 10 which is my array being declared "int x[10]". Im not entirely sure what is wrong and I need some assistance.
Code:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
int main(){
int x[10];
int counter = 0;
string y_n = "y";
double sum = 0;
int max = 0;
int min = 0;
double avg;
double var;
double std_dev;
double ssq = 0;
while(y_n != "n")
{
cout << "Enter a Number: ";
cin >> x[counter];
cout << "Enter another? ";
cin >> y_n;
sum = sum + x[counter];
if (counter == 0) {
max = x[counter];
min = x[counter];
}
else if (x[counter] > max)
{
max = x[counter];
}
else if (x[counter] < min)
{
min = x[counter];
}
ssq = ssq + pow(x[counter],2);
counter = (counter+1);
}
avg = sum/(counter);
var = (ssq/counter)-pow(avg,2);
std_dev = sqrt(var);
cout <<"Max Value:" << std::setw(2) << max << endl;
cout <<"Min Value:" << std::setw(2) << min << endl;
cout <<"Count:" << std::setw(6) << counter << endl;
cout <<"Average:" << std::setw(10) << setprecision(6) << avg << endl;
cout <<"Std Dev:" << std::setw(9) << setprecision(6) << std_dev << endl;
return 0;
}
Explanation / Answer
you must have entered more than 10 integers in to the array so that error is coming.
just increase the size of the array to something larger