Can't format a table to save my life

I was able to get my code to give the correct numbers for my assignment, but I'm having trouble putting the information into a table correctly.

This is what the table should resemble: (Looks weird on the forum but basically there needs to be a column for seconds and a column for distance)

Seconds   Distance
=====================
1           16.0850
2           64.3400
3           144.7650
4           257.3600
5           402.1250
6           579.0600
7           788.1650
8           1029.4400
9           1302.8850
10         1608.5000 


This is the code I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream> 
#include<iomanip>
using namespace std; 

double fallingDistance(int t = 0);

int main()
{
	int t = 0; 
	double d = 0;

	for (int i = 1; i <= 10; i++)
	{
		d = fallingDistance(i);
		cout << i << " second -  " << "distance - " << d << endl;
		
		

	}
	return 0;
} 

double fallingDistance(int t)
{
	double d = 0;
	const double g = 32.17;

	d = (0.5 * g) * (pow(t, 2)); 
	return d;

}


Last edited on
Edit your post and add the format options [code] and [/code] around your program text.

You can also wrap your table with [output] and [/output].

See also: https://stackoverflow.com/a/42243130/8690169
Last edited on
You may want to consider using the stream manipulators to format the output. Things like setw(), left, right, precision, fixed.

Hello scubaa,

Along with what jlb has mentioned this is a good place to start http://www.cplusplus.com/reference/iomanip/

Look at "setw" and "setprecision".

I think what you are trying for is:

Seconds      Distance
=====================
  1            0.0000
  2           16.0850
  3           64.3400
  4          144.7650
  5          257.3600
  6          402.1250
  7          579.0600
  8          788.1650
  9         1029.4400
 10         1302.8850
 11         1608.5000


To start with put this before the for loop:
1
2
3
4
std::cout << std::fixed << std::showpoint << std::setprecision(4);

std::cout << "\nSeconds      Distance\n";
std::cout << std::string(21, '=') << '\n';

Line 1 sets up to print 4 decimal places instead of the 6 numbers that is the default when not using "setprecision". The whole number part is counted in the 6. So 1.23456 or 10.3456 or 100.456 would print. Using the "setprecision" set to 4 you would get 1.1234 or 100.1234 which in the end will line up the decimal points.

For the for loop starting "i" at 1 works for what you want, but most things like arrays start at (0) zero and you should get use to working with that. Also the "<=" can cause a for loop to loop one more time that is need if you are not careful. You for loop would be better as for (int i = 0; i < 10; i++) and even better is for (int i = 0; i < MAXLOOPS; i++) where "MAXLOOPS" is defined as constexpr int MAXLOOPS{ 10 }; and I put this at the top of "main". Sometimes you may want to define this as a global variable to use anywhere in the file.

Inside the for loop you would write cout << i + 1 <<... to compensate for starting at (0) zero.

The last point is in the for loop instead of setting "d" equal to the return value of the function and then using "d" in the "cout" statement you can put the function call in the "cout" and it will print the returned value after first being called.

That should be enough to get you started. Adjust your code and post it in a new message. Then if you are having problems they can be addressed then.

Andy
Topic archived. No new replies allowed.