Square Roots and Totals Table

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;
}
You can also use cout.fill('0') to make your output more uniform in it's width.

http://www.cplusplus.com/reference/ios/ios_base/width/
Topic archived. No new replies allowed.