| | |
variance calc incorrect why?
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Aug 2006
Posts: 9
Reputation:
Solved Threads: 0
#include <iostream>
#include<iomanip>
#include <cmath>
usingnamespace std;
int main()
{
char choice;
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];
for (int j=0;j<num[arraysize];j++)
{
cout<<"Enter Number"<<j+1<<":"; cin>> num[j];
}
cout << " You have entered the following:"<<endl;
for (j = 0; j < num[arraysize]; j++)
{
cout << num[j]<<" ";
}
int sum=0;
for (j=0;j<num[arraysize];j++)
sum+= num[j];
cout<<"\nThe Sum is "<<sum<<endl;
float mean= (sum/num[arraysize]);
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;
}
#include<iomanip>
#include <cmath>
usingnamespace std;
int main()
{
char choice;
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];
for (int j=0;j<num[arraysize];j++)
{
cout<<"Enter Number"<<j+1<<":"; cin>> num[j];
}
cout << " You have entered the following:"<<endl;
for (j = 0; j < num[arraysize]; j++)
{
cout << num[j]<<" ";
}
int sum=0;
for (j=0;j<num[arraysize];j++)
sum+= num[j];
cout<<"\nThe Sum is "<<sum<<endl;
float mean= (sum/num[arraysize]);
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;
}
•
•
•
•
#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.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
See also http://www.daniweb.com/techtalkforums/thread56253.html
> usingnamespace std;
Now I don't know whether this is an endemic problem with your coding, but it should be
Now maybe it's you or maybe it's some damn fool code colouring script you're running which is bugged to hell and back. Either way, it's not helpful to have to second guess what the difference is between what you posted and what we see.
Just post your code between tags, direct from your source code editor. We don't need colouring in a variable width font if the price is mangled code. Keep it nice and simple in a monospaced font.
> usingnamespace std;
Now I don't know whether this is an endemic problem with your coding, but it should be
using namespace std;, that is, there is a space in there.Now maybe it's you or maybe it's some damn fool code colouring script you're running which is bugged to hell and back. Either way, it's not helpful to have to second guess what the difference is between what you posted and what we see.
Just post your code between tags, direct from your source code editor. We don't need colouring in a variable width font if the price is mangled code. Keep it nice and simple in a monospaced font.
I think you are getting the formulas for deviation and variace wrong.
See this code for reference;
Maybe for the formula you should look here;
http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap08/means-8.html
Hope it helped, bye.
See this code for reference;
C++ Syntax (Toggle Plain Text)
#include <iostream> #include<iomanip> #include <cmath> using namespace std; int main() { float deviation, var ; 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 >> 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 ( int j = 0; j < total_numbers; j++) { cout << num[j]<<" "; } int sum=0; for (int 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; if (total_numbers > 1) { for (int i = 0; i < total_numbers; ++i ) { var += ((num [i] - mean) * (num [i] - mean)) ; } var /= (total_numbers - 1) ; deviation =sqrt(var); } else { var = 0.0 ; deviation = 0.0 ; } cout << showpoint << fixed << setprecision (2); cout<<"The Varience is " <<var<<endl; cout << showpoint << fixed << setprecision (2); cout<<"The Standard deviation is "<<deviation<<endl; } } while(choice!='N'&& choice!='n'); return 0; }
Maybe for the formula you should look here;
http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap08/means-8.html
Hope it helped, bye.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Like what ~S.O.S~ have said. The formula for variance in your coding is wrong. The formula of a variance is like this. Take the example that user input 3 numbers 1,2,3 and the mean is 2
(1-2)^2+(2-2)^2+(3-2)^2)/3. Your variance formula seems to be fixed up to 3 numbers. Therefore, the answer will be incorrect no matter how many numbers are input. Below is one of the way to correct the calculation.
Hope it helps.
(1-2)^2+(2-2)^2+(3-2)^2)/3. Your variance formula seems to be fixed up to 3 numbers. Therefore, the answer will be incorrect no matter how many numbers are input. Below is one of the way to correct the calculation.float var=0.0; for (j=0;j<num[arraysize];j++) { var += (float)pow(num[j]-mean,2); } cout << "array=" << num[arraysize] << " Var=" << var << endl; var /= num[arraysize];
Hope it helps.
Last edited by rinoa04; Sep 28th, 2006 at 6:01 am.
![]() |
Similar Threads
- Programs Identifying incorrect version of Windows! Help! (Windows NT / 2000 / XP)
- Incorrect Links to My Site (Site Layout and Usability)
Other Threads in the C++ Forum
- Previous Thread: Communication Between C++, TCL
- Next Thread: optimize code
Views: 2821 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






