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

Discussion Overview

The discussion revolves around creating a C++ function named MakeMilesKmTable() that displays a table of miles converted to kilometers. Participants are focused on both the implementation of the function and the formatting of the output table, which includes two columns for miles and kilometers, as well as addressing issues related to increments and calculations.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant requests help to modify their function to display a complete table of miles and kilometers, indicating they can only run the first part successfully.
  • Another participant suggests that the second column of miles can be calculated by multiplying the corresponding value by 1.61, but notes that the split calculation may not work correctly with an odd number of entries.
  • Concerns are raised about the increment being set as an integer, which limits the ability to use non-integer increments.
  • One participant expresses confusion about the output, stating they can see the left side columns but not the right side columns, and asks for clarification on how to calculate the kilometers.
  • Another participant points out that the calculation for the second column of miles needs to be adjusted to include the increment correctly.
  • A participant shares their updated code but still encounters issues with the second miles column, indicating they are not getting the expected output.
  • Suggestions are made to simplify variable declarations to improve clarity and functionality of the code.
  • One participant expresses gratitude for the assistance received and confirms their code is now working.

Areas of Agreement / Disagreement

Participants generally agree on the need to adjust the calculations for the second column of miles and the handling of increments. However, there is no consensus on the best approach to resolve the issues, as multiple suggestions and corrections are offered without a definitive solution being established.

Contextual Notes

Limitations include potential issues with the calculation of the split value, the handling of increments as integers, and the overall structure of the output table, which may not align with the intended format.

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