variance calc incorrect why?

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2006
Posts: 9
Reputation: Sashar400 is an unknown quantity at this point 
Solved Threads: 0
Sashar400 Sashar400 is offline Offline
Newbie Poster

variance calc incorrect why?

 
1
  #1
Sep 27th, 2006
#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;

}
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: variance calc incorrect why?

 
1
  #2
Sep 27th, 2006
And the question is?
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,651
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: variance calc incorrect why?

 
1
  #3
Sep 27th, 2006
Originally Posted by Sashar400 View Post
#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
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 9
Reputation: Sashar400 is an unknown quantity at this point 
Solved Threads: 0
Sashar400 Sashar400 is offline Offline
Newbie Poster

Re: variance calc incorrect why?

 
1
  #4
Sep 27th, 2006
same result
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: variance calc incorrect why?

 
1
  #5
Sep 27th, 2006
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
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,651
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: variance calc incorrect why?

 
1
  #6
Sep 27th, 2006
I think you are getting the formulas for deviation and variace wrong.

See this code for reference;

  1. #include <iostream>
  2. #include<iomanip>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. float deviation, var ;
  9. char choice;
  10. int total_numbers = 0 ;
  11. const int arraysize=20;
  12. int num[arraysize];
  13. do
  14. {
  15. cout<<"I will give you the Sum,Mean,Var & the Std Dev of any series of numbers?Y/N:"<<endl;
  16. cin>>choice;
  17. if(choice =='Y'||choice =='y')
  18. {
  19. cout<<"How many numbers will you enter? (up to 20)?";
  20. cin >> total_numbers ; // this is correct way of accpeting single number
  21. for (int j=0; j < total_numbers; j++)
  22. {
  23. cout<<"Enter Number"<<j+1<<":"; cin>> num[j];
  24. }
  25. cout << " You have entered the following:"<<endl;
  26. for ( int j = 0; j < total_numbers; j++)
  27. {
  28. cout << num[j]<<" ";
  29. }
  30.  
  31. int sum=0;
  32. for (int j=0; j < total_numbers; j++)
  33. sum+= num[j];
  34. cout<<"\nThe Sum is "<<sum<<endl;
  35. float mean= (sum/ total_numbers);
  36. cout << showpoint << fixed << setprecision (2);
  37. cout<<"The Mean is "<<mean<<endl;
  38.  
  39. if (total_numbers > 1)
  40. {
  41. for (int i = 0; i < total_numbers; ++i )
  42. {
  43. var += ((num [i] - mean) * (num [i] - mean)) ;
  44. }
  45. var /= (total_numbers - 1) ;
  46. deviation =sqrt(var);
  47. }
  48.  
  49. else
  50. {
  51. var = 0.0 ;
  52. deviation = 0.0 ;
  53. }
  54. cout << showpoint << fixed << setprecision (2);
  55. cout<<"The Varience is " <<var<<endl;
  56. cout << showpoint << fixed << setprecision (2);
  57. cout<<"The Standard deviation is "<<deviation<<endl;
  58. }
  59. }
  60. while(choice!='N'&& choice!='n');
  61. return 0;
  62.  
  63. }

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
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 84
Reputation: rinoa04 is on a distinguished road 
Solved Threads: 4
rinoa04's Avatar
rinoa04 rinoa04 is offline Offline
Junior Poster in Training

Re: variance calc incorrect why?

 
1
  #7
Sep 28th, 2006
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.

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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 2821 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC