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

FACULTY OF MECHANICAL ENGINEERING TECHNOLOGY

PDT 236
COMPUTER PROGRAMMING

LABORATORY 6
FUNCTION 2

SEMESTER 1 2020/2021
MATERIAL PROCESSING (RY – 58)

NAME: MATRIC NO.

1. MOHAMMAD SYAFIQ RAIMIE BIN RAZALLY 191352155

2. MUHAMMAD FAIDZI BIN SHUKRI 191352825

3. AZIL IRFAN FIKRI BIN AHMAD FIKRI 191352627

4. MUHAMMAD ALIFF HAIKAL BIN SHOLAHUDIN 191352635

LECTURE NAME:

1. NURUL NAJWA BINTI MANSOR

2. DR. NORSHAH AIZAT BIN SHUIB

DATE OF SUBMISSION: 23TH FEBRUARY 2021


1.0 OBJECTIVE
 To introduce the array data structure.
 To be able to define an array, initialize an array and refer to individual elements of an
array.
 To be able to use arrays to store and sort data in a program.
 To be able to pass arrays to functions.

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

d) Print matrix no., total marks and grade.


e) Print matrix no., and grade.

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);

}while (choice =='y'|| choice == 'Y');

return 0;
}

//ASK USER TO KEY IN VARIABLES


void input (long int *no_matrix, float *score1, float *score2, float *score3)
{
printf ("\nInsert matrix no. of the student: ");
scanf ("%ld", no_matrix);
printf ("\nInsert marks for Test 1 (total of 25) : ");
scanf ("%f", score1);

printf ("\nInsert marks for Test 2 (total of 25) : ");


scanf ("%f", score2);

printf ("\nInsert marks for Final Exam (total of 50) : ");


scanf ("%f", score3);
}

//SUMMATION OF TOTAL MARKS


void sum_marks (float score1, float score2, float score3, float *total)
{
*total = score1 + score2 + score3;
}

//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);
}

void print_matrix_grade (long int no_matrix, char gred)


{
printf ("\tMatrix No. : 0%ld\n", no_matrix);
printf ("\tGrade : %c\n", gred);
}

Figure 1.1 BLACKSCREEN OF PROGRAM 2.1


2.2 Modify your answer for program below from Lab Module 4 by adding two (2) pass by
reference functions to perform the following tasks:

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;

int no_r; //no_r = number of resistors

float r; //r = variable to store resistor value

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);

printf ("Parallel resistance = %.2f ohms\n",parallel_r);

return 0;

//KEY IN VALUE

void fnResistorValue()

printf ("\nEnter the number of resistors: ");

scanf("%d",&no_r);

while (counter<=no_r)

printf ("\nEnter resistors values: ");

scanf("%f",&r);

fnSeries();

fnParallel();

counter++;

//FORMULA FOR SERIES

void fnSeries(){
series_r = series_r + r;

//FORMULA FOR PARALLEL

void fnParallel(){

partial_div = partial_div + 1/r;

parallel_r = 1/partial_div;

Figure 1.2 BLACKSCREEN OF PROGRAM 2.2


2.3 Write a C program that calculates a customer water bill. The water bill includes RM 5.00
basic cost and cost for water usage with rate of RM 1.10 per thousand liter. Water usage
is calculated by subtracting current meter reading with previous month meter reading
(meter is read in thousand liter unit). Your program should check whether there is an
unpaid bill. If the balance of unpaid bill is greater than 0, RM 2.00 fine is charged
together with the unpaid bill and this will be included in the current monthly bill. The
program will also calculate the bill collected for the day. The program will continue if
user enters y- yes, else it will stop and display the collection for the day. Your program
should use these functions:

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

float calc_usage_cost(int, int);


float calc_unpaid_cost(float);
float calc_total_bill(float, float);
void print_bill(int, float);

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);

//CALCULATION OF WATER USAGE


usage = calc_usage_cost(prev_meter, cur_meter);
//KEY IN UNPAID BILL
unpaid = calc_unpaid_cost(unpaid_bill);
//CALCULATION OF TOTAL BILL
total = calc_total_bill(usage, unpaid);
//DISPLAY OF ACT NUMBER & BILLS
print_bill(act_num, total);
//TOTAL BILL TO PERLIS WATER GATHERED
collection = collection + total;
//CONTROL LOOP
printf("\n\nDo you want to continue : y or n ? ");
scanf("%s", &choice);
}while((choice == 'y') || (choice =='Y'));
printf("\n\nPerlis Water collection : RM %.2f\n", collection);
return 0;
}

float calc_usage_cost(int prev, int cur)


{
float water_usage, usage_cost;
water_usage = cur - prev;
usage_cost = (water_usage/1000) * cost_1000_liter;
return(usage_cost);
}
float calc_unpaid_cost(float unpaid)
{
float unpaid_cost;

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

You might also like