Help I cannot figure how to start this.

Write a program to print the following table. Each row in the table has an integer, its square, its cube, and
then its square root to 2 decimal places. Print appropriate column headings. Begin the integers with 50 and
end with 75, increasing each by one. Use setw and setprecision to line up the values each column. (Only
the square root column should be printed with a decimal point.)
After all rows have printed, print a “line” and then the totals for each column. Send your output to a file.

update i cannot figure out the total.





#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;

int main() {
int sum;

cout << setprecision(2) << fixed;
cout << "_____________________________________________" << endl;
for (int i = 50; i <= 75; i++) {
cout << setw(6) << i << setw(9) << i*i << setw(12) << i*i*i << setw(13) << double(sqrt(i)) << endl;
}
cout << "_____________________________________________" << endl;

return 0;
}
Last edited on
Hello mlass7886,

I changed the order slightly, to help you think about what steps to do first and second. Do not try to write the whole program at one time. Work in one small step at a time. Some are easily combined when you get to them.

Look at the requirements this way:

Write a program to print the following table.

1.) Print appropriate column headings.

1B.) Each row in the table has an integer, its square, its cube, and then its square root to 2 decimal places.

2.) Begin the integers with 50 and end with 75, increasing each by one.

2B.) Use setw and setprecision to line up the values each column. (Only the square root column should be printed with a decimal point.)

3.) After all rows have printed, print a “line” and then the totals for each column.

4.) Send your output to a file.

Work on 1 and 1B and get that printing the way you want. You could also combine this with step 2B if you want. It would be good practice if you did.

I am wondering if there is more to this than you have posted like using loops, if statements, the "<iomanip>" header file along with any restrictions. Things like that, all which would help you write the program.

It is not a difficult program, but you will need to write some code and post it back here and ask questions on what you do not understand.

Think about what header files you will need and start with one or any opening message you might want and go from there.

Hope that helps,

Andy
I have everything else correct but I cannot figure out how to total it.






#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;

int main() {
int sum;

cout << setprecision(2) << fixed;
cout << "_____________________________________________" << endl;
for (int i = 50; i <= 75; i++) {
cout << setw(6) << i << setw(9) << i*i << setw(12) << i*i*i << setw(13) << double(sqrt(i)) << endl;
}
cout << "_____________________________________________" << endl;

return 0;
}
Hello mlass7886,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

Look at my earlier post, step 3, and you will find that you are a few variables short.

These variables will be used in the for loop to add to the totals.

After the last "cout" statement print the totals.

I defined these variables at the beginning of main and used them in the for loop. this way you can change the table in one place.

1
2
constexpr size_t START{ 50 };
constexpr size_t END{ 76 }; // One more than needed, unless you use <= in the for loop. 


Hope that helps,

Andy
Thanks man for the help and I will format it better next time. I can't use the contexpr. I know that is the easy way to do it. Is there anything else I could do?
Topic archived. No new replies allowed.