Professional Documents
Culture Documents
Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II
DIVISION L
ROLL NO OF STUDENT L1219
NAME OF STUDENT Rohan Chaudhari
2
ASSIGNMENT NO
Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II
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.
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
Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II
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.
void main()
{
Pimpri Chinchwad Education Trust’s
Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II
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.
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
Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II
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.
Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II
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.
int main()
{
int ageArray[] = {2, 8, 4, 12};
Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II
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;
}
return sum;
}
Output
Pimpri Chinchwad Education Trust’s
Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II
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.
To pass multidimensional arrays to a function, only the name of the array is passed to the
function(similar to one-dimensional arrays).
Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II
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
Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II
Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II
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
Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II
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.
Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II
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
Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II
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
Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II
Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II
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
Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II
Output :
Pimpri Chinchwad Education Trust’s
Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II