Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Unit 2 covered Chapters 4-7. Chapter 4 covered selection statements which includ

ID: 642704 • Letter: U

Question

Unit 2 covered Chapters 4-7. Chapter 4 covered selection statements which included if, if-else, and switch statements. Chapter 5 covered basic loop structures, such as while, for and do-while statements Chapters 6 and 7 covered functions. Refer to "What to Turn In" for specific details on what should be turned in and how to turn in your project.

An automobile travels at an average speed of 55 miles per hour for four hours. Write a C program that displays the distance driven, in miles, by the car every half-hour until the end of the trip. Sample output:

Times (hrs) Distance 0.5 27.50 1.0 55.00 1.5 82.50 2.0 110.00 2.5 137.50 3.0 165.00 3.5 192.50 4.0 220.00

Explanation / Answer

The program looks something like this:

#include<stdio.h>
int main(){
    double distance, speed , timeleft , timeelapsed = 0.0;
  
    printf("Enter the speed : ");
    scanf("%lf",&speed);
  
    printf("Enter the number of hours : ");
    scanf("%lf",&timeleft);
  
    double Distance_Covered_In_Half_Hour = 0.5 * speed; //this value can be reused
  
    distance = 0.0; //distance covered till now
    timeelapsed = 0.0; //time elapsed till now
    printf("Times(hr) Distance ");
    while(1){
        if(timeleft <= 0.5){
            distance = distance + (speed * timeleft);
            timeelapsed = timeelapsed + timeleft;
            timeleft = 0.0;
            printf("%.2lf %.4lf ",timeelapsed, distance);
            break;
        }
        else{
            timeleft = timeleft - 0.5;
            timeelapsed = timeelapsed + 0.5;
            distance = distance + Distance_Covered_In_Half_Hour;
            printf("%.2lf %.4lf ",timeelapsed, distance);
        }
    }
    return 0;
}

If you have any doubts/queries about the program , feel free to cotact. Hope this helps :)