Printing two columns

Hello guys.

So the first part of the question asks to construct a function that converts miles to kilometers which I did. But the second part asks to split the display into two columns rather than one. Something like this:
Miles = Kilometers Miles = Kilometers
1
2
3
4
5
1 1.61             11 17.70
2 3.22             12 19.31
. .                . .
. .                . .
10 16.09           20 32.18


The hint they provide is this:
(Hint: Find split = (start + stop)/2. Let a loop execute from miles = start to split, and
calculate and print across one line the values of miles and kilometers for both miles and
(miles - start + split + 1).)


Here's the code I have so far which only displays it in one column.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

void MakeMilesKmTable(int);

int main()
{
for( int i= 1; i <= 10; i++)
MakeMilesKmTable(i);

return 0;
}

void MakeMilesKmTable(int miles)
{
double m;
m = miles * 1.61;

cout << miles << "\t" << m << endl;
	
return;
}


Any help would be appreciated. Thanks.
There's a lot's of different ways of doing this and for the most part you almost got it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
include    <iostream>

using namespace std;

    const   double Factor = 1.609;  // Kilometers in a mile
    
int main ( int ArgV, char *ArgC [] ) {

    for ( unsigned X = 1; X < 51; X++ ) {
        cout << X << "   -    " << X * Factor;
        cout << "\t\t" << X + 50 << "    -    " << ( X + 50) * Factor << "\n";
        }

    return 0;
    }


This example is really lacking in finise, but it does display two colums 1 - 50 on the left and 51 to 100 on the right.
Thanks for the reply. The function is the thing that's really screwing with me.

The answer is right there but I just can't seem to wrap my hands around it.


EDIT: Got it. The problem was editing the function not int main. Thanks for the help.
Last edited on
One thing you might do: When they give you a hint, define things in terms of the hint (although in this case your hint contains incorrect information.)

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
       const double kmPerMile = 1.61 ;

       unsigned start = 1 ;
       unsigned stop = 20 ;

       // The following is what your hint should've said for calculating split.
       unsigned split = (stop-start+1) / 2 ; 

       for ( unsigned miles = ? ;  miles <= ? ; ++miles )
           cout << miles << ' ' << miles*kmPerMile << '\t' << ? << ' ' << ?*kmPerMile << '\n' ;
}


I leave it to you to replace the question marks.
Topic archived. No new replies allowed.