1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Miles to Kilometers, C++

  1. Jul 1, 2011 #1
    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



    2. Relevant 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)

    3. 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;
    }
     
  2. jcsd
  3. Jul 2, 2011 #2

    I like Serena

    User Avatar
    Homework Helper

    Hi lcam2! :smile:

    (Please use the [noparse]
    Code (Text):
    [/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.
     
  4. Jul 2, 2011 #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.
     
  5. Jul 2, 2011 #4

    I like Serena

    User Avatar
    Homework Helper

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

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


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


    Multiply "i" by 1.61, but you're already doing that.
    You're already storing that result in 'MileToKilo', and printing it.
     
  6. Jul 2, 2011 #5
    1. Im 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;
    }

    ]
     
  7. Jul 2, 2011 #6

    I like Serena

    User Avatar
    Homework Helper

    The simplest solution within the framework of your current program is to replace:
    Code (Text):
    double MileToKilo, split, MileToKilo2;
    by
    Code (Text):
    double MileToKilo, MileToKilo2;
    int split;
     
  8. Jul 2, 2011 #7
    Thank you so much you were very helpful, my code works now.
    Happy 4th of July :smile:
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Miles to Kilometers, C++
  1. C\C++ help needed (Replies: 2)

  2. C++ . (Replies: 19)

Loading...