Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

CSC305 – PROGRAMMING PARADIGMS

C : LAB 2

Name : Amir Hakimi bin Kamarusshahrin


Student No. : 2022661186
Group : RCDCS1104A

LAB SCOPES

1 - Looping
2 - Functions

Question 1

Based on the given complete program in C:

#include<stdio.h>

int main()
{
int counter;
counter = 1;
while(counter <= 10)
{
printf("%d ", counter);
counter = counter + 1;
}
system("pause");
return 0;
}

a- What is the output?


b- What is the name and initial value of loop control variable?
c- What is the expression used to evaluate the loop control variable?
d- What is the assignment statement used to update the loop control variable?
e- What is the purpose of this program?
f- Modify the given program, so that this time it is using for loop.

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

PREPARED BY: NYCJ@KPPIM,UiTM CAWANGAN PERLIS KAMPUS ARAU


1
CSC305 – PROGRAMMING PARADIGMS
C : LAB 2

10 8 11 7 9 14 9999

indicating that the average of all values preceding 9999 is to be calculated.

#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

Write a complete program in C based on the following flowchart (using switch..case


statements):

PREPARED BY: NYCJ@KPPIM,UiTM CAWANGAN PERLIS KAMPUS ARAU


2
CSC305 – PROGRAMMING PARADIGMS
C : LAB 2

PREPARED BY: NYCJ@KPPIM,UiTM CAWANGAN PERLIS KAMPUS ARAU


3
CSC305 – PROGRAMMING PARADIGMS
C : LAB 2

#include <stdio.h>
int main(){
int option;
double priceBfr, priceAftr;

printf("1- Apple iPhone 5s\n2- Samsung Galaxy Note 3\n3- Samsung


Galaxy Note 2\n4- LG Nexus 5 LTE\n5- Sony Xperia Z Ultra");
printf("\n\nEnter your desired model: ");
scanf("%d", &option);

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.

PREPARED BY: NYCJ@KPPIM,UiTM CAWANGAN PERLIS KAMPUS ARAU


4
CSC305 – PROGRAMMING PARADIGMS
C : LAB 2

a)
#include <stdio.h>

int main() {
int numTanks;
double totalKilometers = 0, totalLiters = 0;

// Prompt the user to enter the number of tanks


printf("Enter the number of tanks: ");
scanf("%d", &numTanks);

// Input kilometers driven and liters used for each tank


printf("Enter kilometers driven and liters used for each tank:\n");
for (int i = 1; i <= numTanks; i++) {
double kilometers, liters;
printf("Tank %d: ", i);
scanf("%lf %lf", &kilometers, &liters);

// Calculate and display kilometers per liter for each tank


double kpl = kilometers / liters;
printf("Kilometers per liter for tank %d: %.2f\n", i, kpl);

// 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

PREPARED BY: NYCJ@KPPIM,UiTM CAWANGAN PERLIS KAMPUS ARAU


5
CSC305 – PROGRAMMING PARADIGMS
C : LAB 2

Write a complete program in C, based on the requirements given below:

i) In function named displayMenu(), write code segment that prints the insurance fee to pay
for a pet according to the following rules:

 A dog that has been neutered costs RM50


 A dog that has not been neutered costs RM80
 A cat that has been neutered costs RM40
 A dog that has not been neutered costs RM60
 A bird or reptile costs nothing
 Any other animal generates an error message

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.

float totalFee = 0.0;


for (int i = 0; i < numPets; i++) {
switch (animalTypes[i]) {
case 'D':
case 'd':
if (isNeutered[i] == 1) {
totalFee += 50.0;
} else {
totalFee += 80.0;
}
break;
case 'C':
case 'c':
if (isNeutered[i] == 1) {
totalFee += 40.0;
} else {
totalFee += 60.0;
}
break;
case 'B':
case 'b':
totalFee += 0.0;
break;
case 'R':
case 'r':
totalFee += 0.0;
break;

PREPARED BY: NYCJ@KPPIM,UiTM CAWANGAN PERLIS KAMPUS ARAU


6
CSC305 – PROGRAMMING PARADIGMS
C : LAB 2

default:
printf("Error: Invalid animal type for pet %d\n", i + 1);
break;
}
}
return totalFee;
}

PREPARED BY: NYCJ@KPPIM,UiTM CAWANGAN PERLIS KAMPUS ARAU


7

You might also like