Professional Documents
Culture Documents
Lab 6 CP
Lab 6 CP
PDT 236
COMPUTER PROGRAMMING
LABORATORY 6
FUNCTION 2
SEMESTER 1 2020/2021
MATERIAL PROCESSING (RY – 58)
LECTURE NAME:
2.0 TASKS
2.1 Based on the sample output given, write various type of functions appropriately (void,
with inputparameters and return parameters, i.e. call by value and call by reference) to
perform the following tasks:
a) Accept input such as matrix no. and marks for test 1, test 2 and final exam. Total
mark for test 1 is 25 and test 2 is 25. Total mark for final exam is 50.
b) Compute the sum of marks for test 1, test 2, and final exam for each student.
c) Compute grade obtained based on the following table:
MARKS GRADE
>=80 A
>=65 B
>=50 C
>=40 D
>=25 E
<25 F
ANSWER
#include<stdio.h>
//FUNCTION PROTOTYPE
void input (long int*, float*, float*, float*);
void sum_marks (float, float, float, float*);
void compute_grade (float, char*);
void print_all (long int, float, char);
void print_matrix_grade (long int, char);
int main()
{
char grade;
long int matrix;
float sum, test1, test2, final_exam, total1;
char choice;
do
{
input (&matrix, &test1, &test2, &final_exam);
sum_marks (test1, test2, final_exam, &sum);
compute_grade (sum, &grade);
print_all (matrix, sum, grade);
print_matrix_grade (matrix, grade);
getchar();
printf ("\nDo you want to continue? "); //ASK USER TO EITHER CONTINUE
scanf ("%c", &choice);
return 0;
}
//MARKS CLASSIFICATION
void compute_grade (float mark, char *gred)
{
if (mark >= 80)
*gred = 'A';
else if (mark >=65)
*gred = 'B';
else if (mark >=50)
*gred = 'C';
else if (mark >=40)
*gred = 'D';
else if (mark >=25)
*gred = 'E';
else
*gred = 'F';
}
//DISPLAY OF TOTAL MARKS
void print_all (long int no_matrix, float total, char gred)
{
printf ("\n\tTotal marks for 0%ld", no_matrix);
printf ( " is %.2f ", total);
printf ( "and the grade is %c\n", gred);
}
a) Accept input i.e. number of resistors and resistor value in function main.
b) Calculation for series resistance is performed by using function name fnSeries.
c) Calculation for parallel resistance is performed by using function name fnParallel.
d) Print the values calculated for series resistance and parallel resistance.
ANSWER:
#include <stdio.h>
//VARIABLES
int counter=1;
float partial_div=0;
float series_r=0;
float parallel_r=0;
//FUNCTIONS
void fnResistorValue();
void fnSeries();
void fnParallel();
int main ()
fnResistorValue();
printf ("\nSeries resistance = %.2f ohms\n",series_r);
return 0;
//KEY IN VALUE
void fnResistorValue()
scanf("%d",&no_r);
while (counter<=no_r)
scanf("%f",&r);
fnSeries();
fnParallel();
counter++;
void fnSeries(){
series_r = series_r + r;
void fnParallel(){
parallel_r = 1/partial_div;
1. calc_usage_cost - accepts previous and current meter reading, returns usage cost.
2. calc_unpaid_cost - accepts unpaid bill, returns unpaid cost.
3. calc_total_bill - accepts usage cost and unpaid cost, returns total bill.
4. print_bill - accepts account number and total bill.
ANSWER:
#include <stdio.h>
#define basic_cost 5.00
#define cost_1000_liter 1.10
#define fine 2.00
int main(void)
{
float unpaid_bill, usage, usage_cost, unpaid, total, collection=0;
int prev_meter, cur_meter, act_num;
char choice;
printf("-----------------Perlis Water----------------\n");
printf("This program generates monthly water bill\n");
printf("----------------------------------------------\n\n");
do
{
//ASK USER TO KEY IN DATA
printf("Enter account number : ");
scanf("%d", &act_num);
printf("\nEnter unpaid bill : ");
scanf("%f", &unpaid_bill);
printf("\nEnter previous meter reading : ");
scanf( "%d", &prev_meter);
printf("\nEnter current meter reading : ");
scanf("%d", &cur_meter);
if(unpaid > 0)
unpaid_cost = unpaid + fine;
else
unpaid_cost = 0;
return(unpaid_cost);
}
float calc_total_bill(float usage_cost, float unpaid_cost)
{
float total_bill;
total_bill = basic_cost + usage_cost + unpaid_cost;
return(total_bill);
}
void print_bill(int act, float total_bill)
{
printf("\n\nYour account number is %d \n", act);
printf("Your total bill is %5.2f\n", total_bill);
}
Figure 1.3 BLACKSCREEN OF PROGRAM 2.3