0% found this document useful (0 votes)
134 views

Lab 12

The document describes a C program that uses functions to: 1. Accept student data like matrix number and marks for tests and exams. 2. Calculate total marks and grade based on marks ranges. 3. Print the student details, total marks, and grade. 4. The program uses functions to input data, calculate totals and grades, and print outputs. Sample output is provided showing the program flow and results.

Uploaded by

William Kwong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
134 views

Lab 12

The document describes a C program that uses functions to: 1. Accept student data like matrix number and marks for tests and exams. 2. Calculate total marks and grade based on marks ranges. 3. Print the student details, total marks, and grade. 4. The program uses functions to input data, calculate totals and grades, and print outputs. Sample output is provided showing the program flow and results.

Uploaded by

William Kwong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

Function2

Name: William kwong fook chen (B200057A)

Date: 17/9/2021
Objective:
1. Passing Argument by reference
2. Recursive Function

1. Passing argument by reference.

A function’s parameter that receives the location (memory address) of the corresponding actual
variables is said passing the arguments by reference.

When we attach * (star) after the arg_type in the parameter list of a function, then the variable
following that arg_type is passed by reference

It stores the address of the actual variable, NOT the value. During program execution to manipulate
the data, the address stored will direct control to the memory space of the actual variable

Syntax
1 In function protoype and function definition, put the * (star) after the data type
 Example function prototype:
void inputmarks (float*, float*);

2 In function call, put the &(ampersand) before the argument name to be passed by reference
 Example calling the above function
Inputmarks(&mark1, &mark2);

3. Recursive Function.

Recursive function is a function that calls itself. We usually use a recursive function when we have a
series of same job to be completed. As an example solving a Fibonacci series.

fib(n) = fib(n-1) + fib(n-2)

f( 3 )

return f( 2 ) + f( 1 )

return f( 1 ) + f( 0 ) return 1

return 1 return 0
Question1:
Example of functions that “return” more than one value. To pass the address of the
arguments. Type, save and compile the below program.

#include <stdio.h>
void readmarks(float*, float*); //the prototype of function that pass by reference
float calc_avg(float, float); //the prototype of calc_avg function
void print(float); //the prototype of print function
int main(void)
{
float marks1, marks2, avg;
void readmarks(&marks1, &marks2); //calling function readmarks, pass address of marks1 &
marks2
avg = calc_avg(marks1, marks2);
print(avg);
return 0;
}
void read_marks(float *m1, float *m2)
{
printf("Enter marks for test1 and test2 : ");
scanf("%f %f", m1,m2); //notice no &, this is because m1 and m2 is already adresses
}
float calc_avg(float m1, float m2)
{
return((m1 + m2)/2); //do the calculation and return the answer
}
void print(float average)
{
printf("\nAverage marks are :%.2f\n",average);
}

Write down output of the program.

Enter marks for test1 and test2 : 50 30

Average marks are :40.00


Press any key to continue...
Question 2:
Write, save, compile and understand the below program.

#include <stdio.h>
void fun1(int, int*); //function prototype
int main(void)
{
int a=5, b=10; //
printf("Before fun 1\n“);
printf(" a = %d b = %d“,a, b);
fun1(a, &b); //function call, passing value of a and address of b
printf(“\n\nAfter fun 1\n“);
printf("a = %d b = %d\n“,a,b);
return 0;
}
void fun1(int aa, int * bb) //function definition
{
aa++;
*bb--;
printf("\n\nInside fun 1\n“);
printf("aa = %d bb = %d“,aa,bb);
}
Write the output of the program. Explain about the output of the program

Before fun 1: a = 5 b = 10

Inside fun 1: aa = 6 bb = 9

After fun 1 : a = 5 b = 9

Press any key to continue...


Question 3
Below is the factorial program. The factorial program is an example of a recursive function.
To refresh your memory, if we want to do factorial 4: 4 x 3 x 2 x 1. So the factorial function will call
itself.
Type, save, compile and understand the program below.

#include <stdio.h>
int Factorial(int);
void main()
{
int n=4;
printf(“Factorial %d is %d“,n, Factorial(n));
}
int Factorial(int n)
{
if (n <= 1)
return 1; //when the n is 1 or less then it return to main.
else
return ( n * Factorial(n-1)); //here the factorial function call itself
}
Write down the output of the program

Factorial 4 is 24
Press any key to continue...
Question 4
Problem Solving question.
Write various type of functions appropriately (void, with input parameters 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 test1, test2 and final exam. Total marks for
test1 and test2 is 25. Total mark for final exam is 50.
b) Compute the sum of marks for test1, test2, and final exams 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.

Basically you need 5 functions


void input (long int*, float*, float*, float*);
void sum_marks (float, float, float, float*);
char compute_grade (float);
void print_all (long int, float, char);
void print_matrix_grade (long int, char);

Sample Output

Insert matrix no. of the student : 051060007


Insert their marks for Test 1 (total of 25) : 21
Insert their marks for Test 2 (total of 25) : 18
Insert their marks for Final Exam (total of 50) : 30

Total marks for 051060007 is 69.00 and the grade is B


Matrix No. : 051060007
Grade :B

Do you want to continue? y

Insert matrix no. of the student : 051110007


Insert their marks for Test 1 (total of 25) : 19
Insert their marks for Test 2 (total of 25) : 16
Insert their marks for Final Exam (total of 50) : 20

Total marks for 051110007 is 55.00 and the grade is C


Matrix No. : 051110007
Grade :C

Do you want to continue? n


#include <stdio.h>

void input (long int*, float*, float*, float* );


void sum_marks ( float, float, float, float*);
char compute_grade ( float);
void printf_all ( long int, float, char );
void print_matrix_grade (long int, char );

int main ( )
{
char answer ;
float test1, test2, final, total;
char grade;
long int matrix;
do
{

input (&matrix, &test1,& test2, &final );


sum_marks ( test1, test2, final, &total);
grade = compute_grade (total);
printf_all (matrix, total, grade );
print_matrix_grade ( matrix, grade );

printf ("Do you want to continue ? y= repeat \n");


scanf (" %c", &answer);

} while (answer =='y');


return 0;
}

void input (long int* id, float* t1, float* t2, float* last )
{
printf ("Insert matrix no. of the student\t\t\t: ");
scanf ("%ld", id);
printf ("Insert their marks for Test 1 (total of 25)\t: ");
scanf ("%f", t1);
printf ("Insert their marks for Test 2 (total of 25)\t: ");
scanf ("%f", t2);
printf ("Insert their marks for Final Exam (total of 50)\t: ");
scanf ("%f", last );
}

void sum_marks ( float t1, float t2, float last, float* sum )
{
*sum = t1+t2+last;
}
char compute_grade (float sum)
{
char grad;
if (sum >=80)
{
grad = 'A';
}
else if (sum >=65)
{
grad = 'B';
}
else if (sum >=50)
{
grad = 'C';
}
else if (sum >=40)
{
grad = 'D';
}
else if (sum >=25)
{
grad = 'E';
}
else if (sum <25)
{
grad = 'F';
}
return ( grad);
}

void printf_all ( long int id, float sum, char grad )


{
printf ("Total marks for %ld is %.2f and the grade is %c \n", id, sum, grad);
}
void print_matrix_grade (long int id, char grad )
{
printf ("Matrix No. : %ld \n", id);
printf ("Grade : %c\n", grad);
}

You might also like