You are on page 1of 18

Pimpri Chinchwad Education Trust’s

Pimpri Chinchwad College of Engineering, Pune

Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II

Subject: Computer Programming and Problem Solving-II Laboratory (BFE2317)

DIVISION L
ROLL NO OF STUDENT L1219
NAME OF STUDENT Rohan Chaudhari
2
ASSIGNMENT NO

A class teacher wants to keep record of 10 students in the


class along with the names and marks obtained in 5
subjects. Write a C program with function that displays
a) Name of the student with highest marks in a particular

TITLE OF ASSIGNMENT subject.


b) Overall percentage result of the class
c) Total number of passing students in the class,
d) Total number of students failing in one subject
e) Total number of distinctions in the class.

DATE OF COMPLETION 22\6\21


Pimpri Chinchwad Education Trust’s

Pimpri Chinchwad College of Engineering, Pune

Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II

Subject: Computer Programming and Problem Solving-II Laboratory (BFE2317)

Assignment No. 02

Aim: A class teacher wants to keep record of 10 students in the class along with the names
and marks obtained in 5 subjects. Write a C program with function that displays
a) Name of the student with highest marks in a particular subject
b) Overall percentage result of the class
c) Total number of passing students in the class,
d) Total number of students failing in one subject
e) Total number of distinctions in the class.

Objective: To demonstrate structures, structure arrays and passing arrays as parameter

Outcomes: Students will be able to create user defined functions, passing arrays to
functions.
Theory:

Category of Functions

A function, depending on whether arguments are present or not and whether a value is
returned or not, may belong to one of the following categories.
Pimpri Chinchwad Education Trust’s

Pimpri Chinchwad College of Engineering, Pune

Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II

Subject: Computer Programming and Problem Solving-II Laboratory (BFE2317)

Category 1: Function with no arguments and no return values


Category 2: Function with no arguments and return values
Category 3: Function with arguments and no return values
Category 4: Function with arguments and return values

Category 1: Function with no arguments and no return values

This is the simplest function category. In this category we defined the function that does
not take any argument or does not return any type of value. The example of this category
is given below.

Void main()
{
void line(); // function declaration clrscr();
line(); // function calling getch();
}
void line() // function prototype or function defination
{
printf(“================”);
}
so in this example we have defined one function that will print line. Since the function
does not return any type of value the type of function is void. So function with no
arguments and no return values are written like the above way.

Category 2: Function with no arguments and return values


In this category we defined the function that does not take any argument but return the
value. The example of this category is given below.

void main()
{
Pimpri Chinchwad Education Trust’s

Pimpri Chinchwad College of Engineering, Pune

Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II

Subject: Computer Programming and Problem Solving-II Laboratory (BFE2317)

int sum(); // function declaration int a;


clrscr();
a=sum(); // function calling printf(“%d”,a);
getch();
}
int sum() // function prototype or function defination
{
int c; c=3+4;
return c;
}

since this function return the value of type integer the data type of that function is int.
now this function return the sum of (3+4) which is stored in variable c and sum function
will return variable c whose type is integer and contain the value 7.

Since the function return the values in main() function we need one integer type variable
in which we assign the function value.

Category 3: Function with arguments and no return values


In this category the function will take arguments but return no values. So the nature of
data communication between the calling function and the called function with arguments
but no return values.
We shall modify declaration of both the called function to include arguments as follows:

Printline(ch), Value(p,r,n)

The arguments ch,p,r,n are called the formal arguments. The calling function can now send
values to these arguments using function calls containing appropriate arguments. For
example, the function call

Value(500,0.12,5) Would send the values 500,0.12 and 5 to the function Value(p,r,n);

And assign 500 to p, 0.12 to r and 5 to n. the values 500,0.12 and 5 are the actual
Pimpri Chinchwad Education Trust’s

Pimpri Chinchwad College of Engineering, Pune

Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II

Subject: Computer Programming and Problem Solving-II Laboratory (BFE2317)

arguments which become the values of the formal arguments inside the called function.
The actual and formal arguments should match in number, type, and order.
The values of actual arguments are assigned to the formal arguments on a one to one
basis, starting with the first argument.
We should ensure that the function call has matching arguments. In case, the actual
arguments are more than the formal arguments, the extra actual arguments are
discarded. On the other hand, if the actual arguments are less than the formal arguments,
the unmatched formal arguments are initialized to some garbage values. Any mismatch
in data type may also result in passing of garbage values. Remember, no error message
will be generated. While the formal arguments must be valid variable names, the actual
arguments may be variable names, expressions, or constants. The variables used in actual
arguments must be assigned values before the function call is made.
Remember that, when a function call is made, only a copy of the values of actual
arguments is passed into the called function.

Category 4: Function with arguments and return values


In this category the function will take the argument and return the value.
So, here a self-contained and independent function should behave like a black box that
receive a predefined form of input and outputs a desired value. Such function will have
two- way data communication.
For example,
main()
{
int a=3,b=2,c; int
sum(int,int); clrscr();
c=sum(a,b); printf(“%d”,c);
getch();
}

int sum(int a,int b)


{
return a+b;
}
Pimpri Chinchwad Education Trust’s

Pimpri Chinchwad College of Engineering, Pune

Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II

Subject: Computer Programming and Problem Solving-II Laboratory (BFE2317)

output:
5
so here this function is of integer type so type of function is int. and it will take two
arguments whose type is also integer.

Pass arrays to a function in C


In C programming, you can pass en entire array to functions. Before we learn that, let's see
how you can pass individual elements of an array to functions.
Passing individual array elements
Passing array elements to a function is similar to passing variables to a function.
Example 1: Passing an array
#include <stdio.h>
void display(int age1, int age2)
{
printf("%d\n", age1);
printf("%d\n", age2);
}

int main()
{
int ageArray[] = {2, 8, 4, 12};

// Passing second and third elements to display()


display(ageArray[1], ageArray[2]);
return 0;
}
Output
Pimpri Chinchwad Education Trust’s

Pimpri Chinchwad College of Engineering, Pune

Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II

Subject: Computer Programming and Problem Solving-II Laboratory (BFE2317)

8
4
Example 2: Passing arrays to functions
// Program to calculate the sum of array elements by passing to a function

#include <stdio.h>
float calculateSum(float age[]);

int main() {
float result, age[] = {23.4, 55, 22.6, 3, 40.5, 18};
// age array is passed to calculateSum()
result = calculateSum(age);
printf("Result = %.2f", result);
return 0;
}

float calculateSum(float age[]) {

float sum = 0.0;

for (int i = 0; i < 6; ++i) {


sum += age[i];
}

return sum;
}
Output
Pimpri Chinchwad Education Trust’s

Pimpri Chinchwad College of Engineering, Pune

Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II

Subject: Computer Programming and Problem Solving-II Laboratory (BFE2317)

Result = 162.50
To pass an entire array to a function, only the name of the array is passed as an argument.

result = calculateSum(age);
However, notice the use of [] in the function definition.

float calculateSum(float age[]) {


... ..
}
This informs the compiler that you are passing a one-dimensional array to the function.

Passing Multidimensional Arrays to a Function

To pass multidimensional arrays to a function, only the name of the array is passed to the
function(similar to one-dimensional arrays).

Example 3: Passing two-dimensional arrays


#include <stdio.h>
void displayNumbers(int num[2][2]);
int main()
{
int num[2][2];
printf("Enter 4 numbers:\n");
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
scanf("%d", &num[i][j]);

// passing multi-dimensional array to a function


displayNumbers(num);
return 0;
}
Pimpri Chinchwad Education Trust’s

Pimpri Chinchwad College of Engineering, Pune

Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II

Subject: Computer Programming and Problem Solving-II Laboratory (BFE2317)

void displayNumbers(int num[2][2])


{
printf("Displaying:\n");
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
printf("%d\n", num[i][j]);
}
}
}
Output

Enter 4 numbers:
2
3
4
5
Displaying:
2
3
4
5
Note: In C programming, you can pass arrays to functions, however, you cannot return
arrays from functions.

Pseudo code:
Sample variables :
int num_students = 10;
int num_subjects = 5;
Pimpri Chinchwad Education Trust’s

Pimpri Chinchwad College of Engineering, Pune

Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II

Subject: Computer Programming and Problem Solving-II Laboratory (BFE2317)

int max_marks = 100;


int passing_marks = 45;
int distinction_percent = 70;
char names[num_students][10];
int marks[num_students][num_subjects];

Sample function Prototypes


// 1. Name of the student with highest marks in a particular subject.
int getHighestStudent(int[num_students][num_subjects], int subject)
{
Step1 // write a code to find student index with highest marks in subject
// return the index
}
Step2 : Print the final result as name of the student
//2. Overall percentage result of the class
float class_percentage_result(int[num_students][num_subjects])
{
Step 1: // Write code to find percentage of result of the class
//return the percentage
}
Step 2: Print the percentage of result
//3. Total number of passed students
int num_passed(int[num_students][num_subjects])
{
Step 1: // write code to find number of passed students
//return the result
}
Step 2: Print number of passed students in the class
Pimpri Chinchwad Education Trust’s

Pimpri Chinchwad College of Engineering, Pune

Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II

Subject: Computer Programming and Problem Solving-II Laboratory (BFE2317)

//4. Total number of students failing in one subject


int num_failed(int[num_students][num_subjects])
{
Step 1: // write code to find number of failed students
//return the result
}
Step 2: Print number of failed students in the class
//5. Total number of distinctions in the class.
int num_distinctions(int[num_students][num_subjects])
{
Step 1: // write code to find number of distinction students
//return the result
}
Step 2: Print number of distinction students in the class
Test Cases:
For testing no of student = 5

Input Output
Marks - a) Name of the student with highest marks
Stud 1 - 45 45 45 35 45 in a ‘4 th ’ subject.
Stud 2 – 71 71 71 71 71 => Stud 3
Stud 3 – 60 60 60 80 60 b) Overall percentage result of the class
Stud 4 - 65 65 65 65 60 => 80%
Stud 5 – 45 45 45 45 45 c) Total number of passing students in the
class
=> 4
Pimpri Chinchwad Education Trust’s

Pimpri Chinchwad College of Engineering, Pune

Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II

Subject: Computer Programming and Problem Solving-II Laboratory (BFE2317)

d) Total number of students failing in one


subject
=>1
e) Total number of distinctions in the class.
=> 1

Conclusion:
Hence this program stores information of 10 students.
The program accurately find out
a) Name of the student with highest marks in a particular subject.

b) Overall percentage result of the class

c) Total number of passing students in the class,

d) Total number of students failing in one subject

e) Total number of distinctions in the class.

[Note : Attach the copy of program and output]


Questions:
1. Write a short note on local and global variables in c. Also comment on the scope of
local and global variables with respect to user defined functions.
2. How C programming supports nested user defined functions?
Pimpri Chinchwad Education Trust’s

Pimpri Chinchwad College of Engineering, Pune

Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II

Subject: Computer Programming and Problem Solving-II Laboratory (BFE2317)

My Code :
#include <stdio.h>
int a1;
int p1=0;
int findmax(int a[])
{
int max=0;
for(int i=0;i<10;i++)
{
if(max<a[i])
{
max=a[i];
a1=i;
}
}

return max;
return a1;
}

float percentage(int p)
{
float final_avg=p/5;
return final_avg;
}
int grade(int s1[])
{
for(int i=0;i<10;i++)
{
p1=0;
if(s1[i]>35)
p1++;
}
return p1;
}
Pimpri Chinchwad Education Trust’s

Pimpri Chinchwad College of Engineering, Pune

Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II

Subject: Computer Programming and Problem Solving-II Laboratory (BFE2317)

int main()
{
char std_name[10][10];
int sub1[10];
int sub2[10];
int sub3[10];
int sub4[10];
int sub5[10];
int sum1=0, sum2=0, sum3=0, sum4=0, sum5=0;
float avg1=0, avg2=0, avg3=0, avg4=0, avg5=0, avg=0;
float overall_percentage;
int d1=0;
for(int i=0;i<10;i++)
{
printf("Enter the name of student %d \n",i+1);
scanf("%s",std_name[i]);
printf("Enter marks of subject 1: \n");
scanf("%d",&sub1[i]);
printf("Enter marks of subject 2: \n");
scanf("%d",&sub2[i]);
printf("Enter marks of subject 3: \n");
scanf("%d",&sub3[i]);
printf("Enter marks of subject 4: \n");
scanf("%d",&sub4[i]);
printf("Enter marks of subject 5: \n");
scanf("%d",&sub5[i]);
}
printf("Entered details:");
printf("\nName of student \t Subject1 \t Subject2 \t Subject3 \t Subject4 \t
Subject5");
for(int i=0;i<10;i++)
{
printf("\n %s \t %d \t %d \t %d \t %d \t
%d",std_name[i],sub1[i],sub2[i],sub3[i],sub4[i],sub5[i]);
}
Pimpri Chinchwad Education Trust’s

Pimpri Chinchwad College of Engineering, Pune

Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II

Subject: Computer Programming and Problem Solving-II Laboratory (BFE2317)

printf("\nMaximum marks in subject1 is %d",findmax(sub1));


printf(" It is scored by %s",std_name[a1]);
printf("\nMaximum marks in subject2 is %d",findmax(sub2));
printf(" It is scored by %s",std_name[a1]);
printf("\nMaximum marks in subject3 is %d",findmax(sub3));
printf(" It is scored by %s",std_name[a1]);
printf("\nMaximum marks in subject4 is %d",findmax(sub4));
printf(" It is scored by %s",std_name[a1]);
printf("\nMaximum marks in subject5 is %d",findmax(sub5));
printf(" It is scored by %s",std_name[a1]);
for(int i=0;i<10;i++)
{
sum1=sum1+sub1[i];
}
avg1=sum1/10;
for(int i=0;i<10;i++)
{
sum2=sum2+sub2[i];
}
avg2=sum2/10;
for(int i=0;i<10;i++)
{
sum3=sum3+sub3[i];
}
avg3=sum3/10;
for(int i=0;i<10;i++)
{
sum4=sum4+sub4[i];
}
avg4=sum4/10;
for(int i=0;i<10;i++)
{
sum5=sum5+sub5[i];
}
avg5=sum5/10;
Pimpri Chinchwad Education Trust’s

Pimpri Chinchwad College of Engineering, Pune

Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II

Subject: Computer Programming and Problem Solving-II Laboratory (BFE2317)

avg=avg1+avg2+avg3+avg4+avg5;
overall_percentage=percentage(avg);
printf("\nThe overall percentage of class is %f",overall_percentage);
printf("\nThe number of students passed in subject 1 are %d",grade(sub1));
int f1=10-p1;
printf("\nThe number of students failed in subject 1 are %d",f1);
printf("\nThe number of students passed in subject 2 are %d",grade(sub2));
int f2=10-p1;
printf("\nThe number of students failed in subject 2 are %d",f2);
printf("\nThe number of students passed in subject 3 are %d",grade(sub3));
int f3=10-p1;
printf("\nThe number of students failed in subject 3 are %d",f3);
printf("\nThe number of students passed in subject 4 are %d",grade(sub4));
int f4=10-p1;
printf("\nThe number of students failed in subject 4 are %d",f4);
printf("\nThe number of students passed in subject 5 are %d",grade(sub5));
int f5=10-p1;
printf("\nThe number of students failed in subject 5 are %d",f5);
for(int i=0;i<10;i++)
{
int add=0;
add=sub1[i]+sub2[i]+sub3[i]+sub4[i]+sub5[i];
float dis1=add/5;
if(dis1>75)
d1++;
}
printf("\nNumber of distinction students are %d",d1);
}
Pimpri Chinchwad Education Trust’s

Pimpri Chinchwad College of Engineering, Pune

Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II

Subject: Computer Programming and Problem Solving-II Laboratory (BFE2317)

Output :
Pimpri Chinchwad Education Trust’s

Pimpri Chinchwad College of Engineering, Pune

Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II

Subject: Computer Programming and Problem Solving-II Laboratory (BFE2317)

You might also like