In this C program we are calculating the total and average/percentage of marks of N students using Arrays.
Arrays are the collection of elements of same type of data. Arrays can store only one type of data like int, float, char, double.
Description:
i – Loop control variable.n – Number of students.
m1,m2 and m3 – Array which stores the marks of 3 subjects of ‘n’ students.
total – Array which stores the sum of marks of 3 subjects.
average – Array which stores the total/3 : average marks/percentage.
Input : Number of students, Marks of 3 subjects of ‘n’ students.
Output : Average/Percentage of marks, Result of student
Algorithm to calculate total & average/percentage of marks of N students:
STEP 1: STARTSTEP 2: READ the number of students whose average needs to be calculated
READ n
STEP 3: READ the marks of 3 subjects of ‘n’ students into array
LOOP from i = 0 to n
READ m1 m2 m3
STEP 4: CALCULATE the total marks and average marks of ‘n’ students
LOOP from i = 0 to n
total(i) = m1(i)+m2(i)+m3(i) average(i) = total(i)/3
STEP 5: PRINT the average marks and PASS/FAIL
PRINT “average” and “PASS/FAIL”
STEP 6: END
C Program to find total & average/percentage of marks of N students:
#include<stdio.h>#include<conio.h>
void main()
{
int n, a[25], i, total[20],m1[20],m2[20],m3[20];
float average[20];
printf("Enter number of students: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the marks of 3 subjects of student %d: ",i+1);
scanf("%d %d %d",&m1[i],&m2[i],&m3[i]);
}
for(i=0;i<3;i++)
{
total[i]=m1[i]+m2[i]+m3[i];
average[i]=(float)total[i]/3;
}
for(i=0;i<n;i++)
{
printf("\nAverage/percentage marks of student %d is: %f",i+1,average[i]);
if(average[i]>=35)
printf("\nStudent is PASS");
else
printf("\nStudent is FAIL");
}
getch();
}
Output 1:
Enter number of students: 3Enter the marks of 3 subjects of student 1: 89 76 94
Enter the marks of 3 subjects of student 2: 32 89 11
Enter the marks of 3 subjects of student 3: 22 36 33
Average/percentage marks of student 1 is: 86.333336
Student is PASS
Average/percentage marks of student 2 is: 44.000000
Student is PASS
Average/percentage marks of student 3 is: 30.333334
Student is FAIL
Output 2:
Enter number of students: 2Enter the marks of 3 subjects of student 1: 88 55 98
Enter the marks of 3 subjects of student 2: 23 43 24
Average/percentage marks of student 1 is: 80.333336
Student is PASS
Average/percentage marks of student 2 is: 30.000000
Student is FAIL
QUIZ:
1] Array stores data from index.- zero (0)
- one (1)
- any index
- – 1
- 1-D
- 2-D
- Multidimensional
- All
- TRUE
- FALSE