Professional Documents
Culture Documents
C Lab2 Studs
C Lab2 Studs
C : LAB 2
LAB SCOPES
1 - Looping
2 - Functions
Question 1
#include<stdio.h>
int main()
{
int counter;
counter = 1;
while(counter <= 10)
{
printf("%d ", counter);
counter = counter + 1;
}
system("pause");
return 0;
}
a) 1 2 3 4 5 6 7 8 9 10
b) Counter,1
c) counter<=10
d) counter=counter+1
e) print the counter until the loop ends
f) for(int i=10;counter<=10;counter++)
Question 2
Write a program using do..while loop that calculates and prints the average of several integers.
Continue reading values until the sentinel 9999 is read. A typicals set of input values may be
10 8 11 7 9 14 9999
#include int main() { int sum = 0; int count = 0; int value; printf("Enter integers (enter 9999 to
calculate average): "); do { scanf("%d", &value); if (value != 9999) { sum += value; count++; } } while
(value != 9999); if (count > 0) { double average = (double)sum / count; printf("Average of entered
integers: %.2f\n", average); } else { printf("No integers were entered.\n"); } return 0; }
Question 3
#include <stdio.h>
int main(){
int option;
double priceBfr, priceAftr;
switch(option){
case 1:
priceBfr = 2260;
break;
case 2:
priceBfr =2098;
break;
case 3:
priceBfr = 1499;
break;
case 4:
priceBfr = 1579;
break;
case 5:
priceBfr = 1419;
break;
}
priceAftr = priceBfr*0.75;
printf("The model is %d", option);
printf("The price before discount is %f and the price after
discount is %f", priceBfr, priceAftr);
}
Question 4
a - Drivers are concerned with the milage obtained by their automobiles. One driver has kept
track of several tanks of petrol by recording the kilometers driven and the litres used for each
tank. Write a C program using for loop structure that will input the number of tanks,
kilometers driven and litres used for each tank. The program should calculate and display the
kilometers per litres obtained for each tank of petrol. After processing all input information,
the program should be able to calculate and print the average kilometers per litres obtained for
all tanks.
b - Based on your answer in (a), modify the program so that the calculation of the average
kilometers per litres obtained for all tanks is performed by a function called calcAverage().
The function will also display the answer to the user.
a)
#include <stdio.h>
int main() {
int numTanks;
double totalKilometers = 0, totalLiters = 0;
// Update totals
totalKilometers += kilometers;
totalLiters += liters;
}
// Calculate and display average kilometers per liter for all tanks
if (totalLiters > 0) {
double avgKPL = totalKilometers / totalLiters;
printf("Average kilometers per liter for all tanks: %.2f\n", avgKPL);
} else {
printf("No data entered.\n");
}
return 0;
}
b)
double calcAverage(double totalKilometers, double totalLiters) {
if (totalLiters > 0) {
return totalKilometers / totalLiters;
} else {
return 0; // Return 0 if no data entered to avoid division by zero
}
}
Question 5
i) In function named displayMenu(), write code segment that prints the insurance fee to pay
for a pet according to the following rules:
ii) In the main program, prompt the user for the appropriate information using a code to
determine the kind of animal. For example, D or d represents a dog, C or c represents a cat, B
or B represents a bird, R or r represents a reptile and anything else represents some other kind
of animal. User may insure more than one pets.
iii) In another function named calcBill(), calculate and return the insurance fee that a user has
to pay for his/her pet(s) so that main program is able to display the total fee to the user.
default:
printf("Error: Invalid animal type for pet %d\n", i + 1);
break;
}
}
return totalFee;
}