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

  • Context: Comp Sci 
  • Thread starter Thread starter lcam2
  • Start date Start date
  • Tags Tags
    C++
Click For Summary
SUMMARY

The forum discussion focuses on creating a C++ function named MakeMilesKmTable() that generates a table displaying miles and their equivalent kilometers. The function takes starting and stopping values for miles and an increment as arguments, utilizing the conversion factor of 1 mile = 1.61 kilometers. Users encountered issues displaying the second column of miles and ensuring proper increments, which were resolved by adjusting the calculation logic and variable types. The final solution successfully outputs a complete table with four columns: two for miles and two for kilometers.

PREREQUISITES
  • Understanding of C++ programming syntax and structure
  • Familiarity with functions and variable types in C++
  • Knowledge of basic input/output operations using iostream
  • Comprehension of arithmetic operations and loops in programming
NEXT STEPS
  • Explore C++ functions and their prototypes in detail
  • Learn about data types and variable scope in C++
  • Investigate formatting output in C++ using iomanip
  • Study how to implement loops and conditionals effectively in C++
USEFUL FOR

C++ developers, programming students, and anyone interested in converting units and displaying formatted tables in console applications.

lcam2
Messages
28
Reaction score
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 calculate 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;


count << "This Program converts from Miles to Kilometers." << endl;
count << endl;
count << "Miles \t Kilometers" << setw(10) << "Miles \t Kilometers" << endl;
count << "---------------------------" << 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;

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

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

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

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.
 
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.
 
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.
 
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;


count << "This Program converts from Miles to Kilometers." << endl;
count << endl;
count << "Miles \t Kilometers \t Miles \t Kilometers" << endl;
count << "---------------------------" << 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 );

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

return MileToKilo;
}

]
 
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;
 
Thank you so much you were very helpful, my code works now.
Happy 4th of July :smile:
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K