How can I display both miles and kilometers in a table using a C++ function?

  • Comp Sci
  • Thread starter lcam2
  • Start date
  • Tags
    C++
In summary, this conversation discussed how to write and test a C++ function named MakeMilesKmTable() to display a table of miles converted to kilometers. The function takes in the starting and stopping values of miles and the increment as arguments and outputs a table with two columns, one for miles and one for their equivalent kilometer values. The conversation also addressed how to modify the function to display a second set of columns for miles and their corresponding kilometer values. Finally, some helpful hints were provided for solving the problem and the solution was provided.
  • #1
lcam2
28
0
1.
write and test a c++ function named MakeMilesKmTable() to display a table of miles converted to kilometers. the argument to the function should be the starting and stopping values of miles and the increment. the output should be a table of miles and their equivalent kilometer values. use the relationship that 1 mile = 1.61 km.
Then modify the function so that the two columns are printed. For example, if the starting value is 1 mile, the ending value is 20 miles, and the increment is 1.

Miles Kilometers Miles Kilometeres
1 1.61 11 17.70
2 3.22 12 19.31
3 . . .
. . . .
. . . .
10 16.09 20 32.18


My problem is that i can run the first part, but i don't know how to display
the other part of the table. If anyone can give me some help i will really apreciaate it.
Thanks in advance

Book Link
http://books.google.com/books?id=5-...ble of miles converted to kilometers.&f=false



Homework Equations



1 mile = 1.61 Kilometers

Hint: Find, split =(start +stop)/2. let a loop execute from miles = start to split, and calcualte and print across one line the values of miles and kilometers for both miles and (miles-start+split+1)

The Attempt at a Solution



#include <iostream>
#include <iomanip>

using namespace std;

double MakeMilesKmTable ( double, double, int); //Fuction Prototype

int main ()
{
double start = 1; //Variabledeclaration
double stop = 20;
int increment =1;


cout << "This Program converts from Miles to Kilometers." << endl;
cout << endl;
cout << "Miles \t Kilometers" << setw(10) << "Miles \t Kilometers" << endl;
cout << "---------------------------" << endl;

MakeMilesKmTable (start, stop, increment); //Calling the function


return 0;
}

//Function MakeMilesKmTable
double MakeMilesKmTable ( double start, double stop, int increment)
{
double MileToKilo, split;

split = ( start + stop )/ 2.0;

for (double i=start; i <= split; i++)
{
MileToKilo = i * 1.61;

cout << i << setw(15) << MileToKilo << "\t" << (i - start + split +1 ) << endl;
}

return MileToKilo;
}
 
Physics news on Phys.org
  • #2
Hi lcam2! :smile:

(Please use the [noparse]
Code:
[/noparse] tags right above your reply box to enclose programming code. :wink:)[/SIZE]

You have already calculated and printed the miles in the second column (i - start + split +1).
If you also multiply this value by 1.61 and print that, you have a complete second column.


You do have a couple of other problems though.

You've calculated your split to be exactly in the middle, but that only works if you have an even number of entries.

You turned the "increment" into an integer, meaning you cannot make increments of say 0.5 miles.

In your calculation of the miles needed for the second column, you've added the number 1, which should be the increment.
This means you've made the incorrect assumption that the increment is always 1.
 
  • #3
Im a bit confused, when i run the program i can see the left side columns. But i cannot see right side columns.
How do i make the right side miles columns to increment by one.
How do i calculate the left side kilometer column? multiplying the split by 1.61?
Please help me.
 
  • #4
lcam2 said:
Im a bit confused, when i run the program i can see the left side columns. But i cannot see right side columns.

How many columns do you get and what is in them?

You should have 3 columns (and you need 4).


lcam2 said:
How do i make the right side miles columns to increment by one.

You would need (i - start + split + increment)


lcam2 said:
How do i calculate the left side kilometer column? multiplying the split by 1.61?

Multiply "i" by 1.61, but you're already doing that.
You're already storing that result in 'MileToKilo', and printing it.
 
  • #5
1. I am getting 3 columns;
Miles from 1 to 10, Kilometers from 1.61 to 16.1, and miles again from 11.5 to 20.5
For the second column of miles i need 11 to 20.

I just did what you suggested but i still don't get the second miles column right
Thanks for your help and patience.

This is my new Code

[

#include <iostream>
#include <iomanip>

using namespace std;

double MakeMilesKmTable ( double, double, int); //Fuction Prototype

int main ()
{
double start = 1; //Variabledeclaration
double stop = 20;
int increment =1;


cout << "This Program converts from Miles to Kilometers." << endl;
cout << endl;
cout << "Miles \t Kilometers \t Miles \t Kilometers" << endl;
cout << "---------------------------" << endl;

MakeMilesKmTable (start, stop, increment); //Calling the function


return 0;
}

//Function MakeMilesKmTable
double MakeMilesKmTable ( double start, double stop, int increment)
{
double MileToKilo, split, MileToKilo2;

split = ( start + stop )/ 2.0;

for (double i=start; i <= split; i++)
{
MileToKilo = i * 1.61;
MileToKilo2 = 1.61*(i - start + split + increment );

cout << i << setw(15) << MileToKilo << " \t " << (i - start + split + increment ) << setw(17) << MileToKilo2 << endl;
}

return MileToKilo;
}

]
 
  • #6
The simplest solution within the framework of your current program is to replace:
Code:
double MileToKilo, split, MileToKilo2;
by
Code:
double MileToKilo, MileToKilo2;
int split;
 
  • #7
Thank you so much you were very helpful, my code works now.
Happy 4th of July :smile:
 

1. What is the formula for converting miles to kilometers in C++?

The formula for converting miles to kilometers in C++ is: kilometers = miles * 1.60934.

2. How do I input the miles value in my C++ program?

You can use the "cin" function to prompt the user to enter the miles value, and then assign the input to a variable to be used in the conversion formula.

3. Can I convert kilometers to miles using the same formula?

No, the formula for converting kilometers to miles is: miles = kilometers / 1.60934. It is the inverse of the formula for converting miles to kilometers.

4. Is there a built-in function in C++ for converting miles to kilometers?

Yes, the "stod" function in the "string" library can be used to convert a string input to a double, which can then be used in the conversion formula.

5. How can I display the converted value with a specific number of decimal places?

You can use the "setprecision" function from the "iomanip" library to specify the number of decimal places in the output. For example, "cout << setprecision(2) << kilometers;" will display the converted value with 2 decimal places.

Similar threads

  • Programming and Computer Science
Replies
2
Views
2K
Back
Top