#include <iostream>
#include<iomanip>
#include <cmath>
usingnamespace std;
int main()
{
char choice;
int total_numbers = 0 ;
const int arraysize=20;
int num[arraysize];
do
{
cout<<"I will give you the Sum,Mean,Var & the Std Dev of any series of numbers?Y/N:"<<endl;
cin>>choice;
if(choice =='Y'||choice =='y')
{
cout<<"How many numbers will you enter? (up to 20)?";
// cin>>num[arraysize]; this is wrong it means entering value in 20th element
cin >> total_numbers ; // this is correct way of accpeting single number
for (int j=0; j < total_numbers; j++)
{
cout<<"Enter Number"<<j+1<<":"; cin>> num[j];
}
cout << " You have entered the following:"<<endl;
for (j = 0; j < total_numbers; j++)
{
cout << num[j]<<" ";
}
int sum=0;
for (j=0; j < total_numbers; j++)
sum+= num[j];
cout<<"\nThe Sum is "<<sum<<endl;
float mean= (sum/ total_numbers);
cout << showpoint << fixed << setprecision (2);
cout<<"The Mean is "<<mean<<endl;
float var=((num[0]-mean)*pow(num[0]-mean,2) + (num[1]-mean)*pow(num[1]-mean,2) + + (num[2]-mean)*pow(num[2]-mean,2))/ (num[arraysize-1]);
cout << showpoint << fixed << setprecision (2);
cout<<"The Varience is " <<var<<endl;
float sqrtvar=sqrt(var);
cout << showpoint << fixed << setprecision (2);
cout<<"The Standard deviation is "<<sqrtvar<<endl;
}
}while(choice!='N'&& choice!='n');
return 0;
}
I think you gettting the fundamentals of programming wrong. Why you taking in the number of elements entered by the user into an array. Why not just use a simple int variable like "total_numbers".
I have made some changes in teh program, try running it now and see what you get. And also format your code a bit so it can be read by other people and put the code in the [code] tags.
Hope it helped ,bye.
Last edited by ~s.o.s~; Sep 27th, 2006 at 12:45 pm.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
Offline 8,871 posts
since Jun 2006