You are on page 1of 6

COMSATS University Islamabad, Lahore Campus

Department of Computer Science

Lab Rubrics # 1 – Post Lab Assessment – Spring 2022


Course Title Introduction to Computer Programming

Assignment 1

Due Date 06-Apr-2022

Student Name Hamza Rahim

Registration Number FA20-CHE-023

Instructions:

● You need to submit a word and a pdf file against this assignment with proper name
format (e.g., RegNum_Assignment#1_PostLab_ICP)
● If found plagiarism, straight zero will be marked against assignment.
● Submit Assignment on Due Date. (No Late Submission will be Tolerated)

Question # 1 (CLO2, C4, PLO2) (CLO4, P5, PLO5, PLO10): (5+5+5+5)


Draw flowchart, write algorithm and also write a C program with 2 output screenshots of the following
statement:

“Because of the high price of petrol, you are concerned with the fuel consumption of your car. As a
result, you have a record of the kilometers driven and liters used for each tank of petrol you purchase.
Write a program that will display the kilometers driven, liters used, and consumption (in liters/100km)
for each tankful. After processing all the input information, the program should calculate the overall
average consumption.” (Hint: The liters/100km is (Liters/KM driven) x 100)

Sample Output:

Enter the liters used (-1 to end): 57.6


Enter the kilometers driven: 459
The liters/100km for this tank was 12.5
Enter the liters used (-1 to end): 45.3
Enter the kilometers driven: 320
The liters/100km for this tank was 14.2
Enter the liters used (-1 to end): -1

The overall average consumption was: 13.4


 Flowchart:
Start

Number liters, km driven, consumption, total consumption, num


records, avg consumption

Total consumption=0;

Num records=0;

Input liters;

Is

Liters=-1

Num records=num records+1;

Input km Driven;

Consumption=(liters/km driven)*100;

Total consumption=total consumption+consumption;


1

Display consumption;

Input liters;

Is Avg
numrecords=0 consumption=0

Avg consumption=total consumption/numrecords;

Display avg consumption;

End
 Algorithm:
Algorithm to input the kilometers driven and liters used and compute the consumption for each
record and average consumption for all records entered

Step 1: Declare variables liters, km Driven, consumption, total Consumption, num Records, avg
Consumption of number type
Step 2: Set totalConsumption to 0
Step 3: Set numRecords to 0
Step 4: Input liters

Step 5: While (liters! = -1) // loop that continues until user enters -1 as liters used
do
Increment numRecords by 1
Input kmDriven
Calculate consumption = (liters/kmDriven)*100

Display consumption to 1 decimal place


Add consumption to totalConsumption
Input liters
End While

Step 6: If numRecords = 0 then


Set avg Consumption to 0
Else
Calculate avg Consumption = totalConsumption/numRecords
End If

Step 7: Display avg Consumption to 1 decimal place


Step 8: End
 C Code:
#include<stdio.h>
int main()
{
double lt=0,km=0,ltpr100=0,avg=0,cnt=0;
while(1)

{
printf("Enter the liters used(-1 to end):");
scanf("%lf",&lt);
if(lt==-1)
{

break;
}
printf("Enter the kilometers driven:");
scanf("%lf",&km);
ltpr100=(lt/km)*100;

avg+=ltpr100;
printf("The liters/100km for this tank was %.2lf\n",ltpr100);
cnt++;
}
avg/=cnt;

printf("\nThe overall average consumption was: %.2lf",avg);


return 0;
}

 Screenshots:

You might also like