943,706 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 12518
  • C++ RSS
Sep 26th, 2006
0

setprecision

Expand Post »
I have #include<iomanip> header also and it's still not working.I am getting 2 dec places but it's .00 Why is that?Please help.
here is the code
cout << showpoint << fixed << setprecision (2);
cout<<"The Mean is "<<mean<<endl;


also this code is not working ,when I use the [ ] at the end,I am getting an error so I am using () instead but the calculation is incorrect.Please help

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]);

#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;

}
Last edited by Sashar400; Sep 26th, 2006 at 2:05 pm. Reason: entire code
Similar Threads
Reputation Points: 22
Solved Threads: 0
Newbie Poster
Sashar400 is offline Offline
9 posts
since Aug 2006
Sep 26th, 2006
1

Re: setprecision

Click to Expand / Collapse  Quote originally posted by Sashar400 ...
I have #include<iomanip> header also and it's still not working.I am getting 2 dec places but it's .00 Why is that?Please help.
here is the code
cout << showpoint << fixed << setprecision (2);
cout<<"The Mean is "<<mean<<endl;


also this code is not working ,when I use the [ ] at the end,I am getting an error so I am using () instead but the calculation is incorrect.Please help

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]);
If you want help you need to post the whole code. Read this
Last edited by andor; Sep 26th, 2006 at 1:59 pm.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Sep 26th, 2006
1

setprecision

entire code

#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;
//not working

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;
//not working
float sqrtvar=sqrt(var);
cout << showpoint << fixed << setprecision (2);
cout<<"The Standard deviation is "<<sqrtvar<<endl;
//not working
}

}while(choice!='N'&& choice!='n');
return 0;

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

Re: setprecision

lovely code -- but what is your question ? please edit your post to include code tags.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Sep 26th, 2006
1

Re: setprecision

Please explain the problem clearly so that we can help you out. Post your entire code along with the problems you are encountering.. whether it is compile time error or run time error or the program is not working as expectd and so on. Maybe then we would be able to help you out.
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 26th, 2006
1

Re: setprecision

I suggest you do a google search of setprecision and pow.

I think pow expects a double somewhere. So you'll have to write pow(2,3) and pow (2.0,3.0) or something? Not to sure.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

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:





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


Follow us on Twitter


© 2011 DaniWeb® LLC