-
January 21st, 2013, 03:49 PM
#1
Square Roots and Totals Table c++
Hey I am looking to create a table somthing like this output below but I am very new to this and my code is abit everywhere.
x sqrt(x) x ^ 2 x ^ 3
========================================
1 1 1 1
2 1.414 4 8
3 1.732 9 27
4 2 16 64
5 2.236 25 125
6 2.449 36 216
7 2.646 49 343
8 2.828 64 512
9 3 81 729
10 3.162 100 1000
========================================
Totals: 55 22.47 385 3025
========================================
This is the code I have I have got it to cal some of the values but numbers our everywhere. I am doing more Trial and error than anything.
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x
;
cout.precision(4);
cout << " x sqrt(x) x^2\n\n x^3\n";
for(x = 1.0; x <= 20.0; x++) {
cout.width();
cout <<x*x*x << " ";
cout.width(7);
cout << x << " ";
cout.width(7);
cout << sqrt(x) << " ";
cout.width(7);
cout << x*x << '\n';
}
system("pause");
return 0;
}
-
January 21st, 2013, 04:02 PM
#2
Re: Square Roots and Totals Table c++
Without a specific question, all I can say is look at the order of your cout statements and your header. They don't match. You may want to get rid of the \n in your header too.
Also, your for loop variable should be an int. You can cast it to a double to call the sqrt function.
-
January 21st, 2013, 04:08 PM
#3
Re: Square Roots and Totals Table c++
Here is the question I am trying to do
Write a program that prints a table of values such as the one below for all numbers starting at 1 and up to and including a number which will be input from the console.
Your output might look something like:
x sqrt(x) x ^ 2 x ^ 3
========================================
1 1 1 1
2 1.414 4 8
3 1.732 9 27
4 2 16 64
5 2.236 25 125
6 2.449 36 216
7 2.646 49 343
8 2.828 64 512
9 3 81 729
10 3.162 100 1000
========================================
Totals: 55 22.47 385 3025
========================================
-
January 21st, 2013, 04:24 PM
#4
Re: Square Roots and Totals Table c++
That's not a specific question addressing a problem you're having. Your question should be along the lines of "Here's my code. It's doing x when I want it to do y, and I'm not sure how to make it do z."
Anyway, I gave you a couple of suggestions in my previous post.
-
January 21st, 2013, 04:36 PM
#5
Re: Square Roots and Totals Table c++
GCDEF gave you the hints that you need to make your program right. Also, please format your code. Use 'Go Advanced' then select the code then press '#' It makes the code much easier to read.
Code:
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x;
cout.precision(4);
cout << " x sqrt(x) x^2\n\n x^3\n";
for (x = 1.0; x <= 20.0; x++) {
cout.width();
cout << x*x*x << " ";
cout.width(7);
cout << x << " ";
cout.width(7);
cout << sqrt(x) << " ";
cout.width(7);
cout << x*x << '\n';
}
system("pause");
return 0;
}
When you have \n in a string this causes a new line to be started in the output. So the \n\n after x^2 causes 2 new lines to be output which is probably not what you want. You also want the output in the same order as your heading which is x sqrt(x) x^2 and x^3 but you are outputting in the order x^3 x sqrt(x) x^2. Cout outputs in the order that it is used.
-
January 21st, 2013, 04:53 PM
#6
Re: Square Roots and Totals Table c++
I was kind of hoping he'd take the hints and figure it out himself, rather than having it spoon fed to him.
-
January 21st, 2013, 05:21 PM
#7
Re: Square Roots and Totals Table c++
This is the way it should be?... I was also wondering what way I would go about adding a total row along the bottom?
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x;
;
cout.precision(4);
cout << " x sqrt(x) x^2 x^3\n\n";
for(x = 1.0; x <= 10.0; x++) {
cout.width(7);
cout << x << " ";
cout.width(7);
cout << sqrt(x) << " ";
cout.width(7);
cout << x*x << " ";
cout.width(7);
cout << x*x*x << '\n';
}
system("pause");
return 0;
}
-
January 21st, 2013, 05:29 PM
#8
Re: Square Roots and Totals Table c++
cheers lads never done much of this **** before really just tryin to get my head round
-
January 21st, 2013, 05:58 PM
#9
Re: Square Roots and Totals Table c++
Originally Posted by Rich1e1990
This is the way it should be?... I was also wondering what way I would go about adding a total row along the bottom?
Go back and change your loop to use integers, not doubles.
The reason is that floating point arithmetic is not exact. Right now, you're only getting away with it because you're adding 1 to the value. If you were adding 0.1 or some other fractional element, the loop will execute an indefinite number of times due to floating point rounding errors.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|