For Loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kipj77
    New Member
    • Sep 2007
    • 11

    For Loop

    I am having trouble with the for loop. I need to display each hour driven and the amount of miles traveled calculated from a speed in mph and a time in hours "cin"ed. Please help me!>!>!>!>!>!, Thanks.
  • Studlyami
    Recognized Expert Contributor
    • Sep 2007
    • 464

    #2
    what exactly are you having problems with? What have you done? Please read the posting guidelines.

    Comment

    • mattmao
      New Member
      • Aug 2007
      • 121

      #3
      # include <math.h>

      double mhp;
      double hours;

      get the values of mhp and hours from user's keyboard inputs. (scanf will do happyly...)

      then use a for loop to print out
      int i;
      for(i=0; i<ceil(hours); i++)
      {
      //use printf to print out the result of the calculation here.
      }



      This is not the actual code. From your description, I reckon this is not a hard work:)

      BTW you can use a variable of int the record the hours, I guess the purpose of this code is to print out all the miles that the vehicle can run in every hour. ..

      Comment

      • Studlyami
        Recognized Expert Contributor
        • Sep 2007
        • 464

        #4
        Im not seeing the need of a loop at all so, I'm guess I don't understand the question. You Get MPH and the total hours driven from the user. So what is the desired result? You stated you need to display each hour driven and the miles that were driven during that hour? Can your speed change per hour? if not the math is just the MPH and for the last mile its a simple division problem.

        Comment

        • mattmao
          New Member
          • Aug 2007
          • 121

          #5
          say mhp is set to a constant value of 60.00 miles/hour
          and hours is set to 5.00

          then

          the program should print out:

          hour 1 60.00000 miles
          hour 2 120.000000 miles
          hour 3 180.000000miles
          hour 4 240.000000 miles
          hour 5 300.000000 miles


          I think so, according to his description

          Comment

          • Studlyami
            Recognized Expert Contributor
            • Sep 2007
            • 464

            #6
            alright well this is fairly easy and you have pretty much already done the problem.

            So in our code we have

            int MPH = 60; //I'm assuming you have this part completed
            int Hours = 5;

            Lets add one more variable called TotalMiles

            int TotalMiles = 0;

            the loop you put up was correct

            for(int i = 0; i<Hours; i++)
            {
            Now in here what do you need to do Every hour in order to get the total miles

            }

            You already did it yourself when you put Hour 1 = 60, hours 2 =120, ect. Now you just need to put that math calculation (the one you used to get from 60 to 120) and put that into the loop. Then output the value (if you need to output the value at each hour it will have to be in the loop, if you only need total miles you can put it outside the loop). I hope that makes sense.

            Comment

            Working...