943,718 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 4079
  • C++ RSS
Sep 27th, 2006
1

variance calc incorrect why?

Expand Post »
#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;

}
Similar Threads
Reputation Points: 22
Solved Threads: 0
Newbie Poster
Sashar400 is offline Offline
9 posts
since Aug 2006
Sep 27th, 2006
1

Re: variance calc incorrect why?

And the question is?
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Sep 27th, 2006
1

Re: variance calc incorrect why?

Click to Expand / Collapse  Quote originally posted by Sashar400 ...
#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
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Sep 27th, 2006
1

Re: variance calc incorrect why?

same result
Reputation Points: 22
Solved Threads: 0
Newbie Poster
Sashar400 is offline Offline
9 posts
since Aug 2006
Sep 27th, 2006
1

Re: variance calc incorrect why?

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 27th, 2006
1

Re: variance calc incorrect why?

I think you are getting the formulas for deviation and variace wrong.

See this code for reference;

C++ Syntax (Toggle Plain Text)
  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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Sep 28th, 2006
1

Re: variance calc incorrect why?

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.
Reputation Points: 52
Solved Threads: 4
Junior Poster in Training
rinoa04 is offline Offline
84 posts
since Sep 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Communication Between C++, TCL
Next Thread in C++ Forum Timeline: optimize code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC