Write a Modular C Programming code for Getting details of a Student who took the highest marks and Sum & Avg of marks Using Pointers*.
Read the number of students, read their information, and display the details of the students:
CODE:
#include<stdio.h> struct student { char name[20]; int rno; float marks; }; void read(struct student *ptr, int n); void display(struct student *ptr, int n); void avgmarks(struct student *ptr, int n); void maxmarks(struct student *ptr, int n); int n, i; struct student s[50]; struct student *ptr; main() { ptr=&s; printf("Enter the no. of students you want to give info: "); scanf("%d",&n); read(ptr,n); avgmarks(ptr,n); maxmarks(ptr,n); } void read(struct student *ptr, int n) { for(i=0;i<n;i++) { printf("\nEnter Details of Student %d",i+1); printf("\nEnter the Name: "); scanf("%s",&(ptr+i)->name); printf("Enter the RollNo: "); scanf("%d",&(ptr+i)->rno); printf("Enter the Marks: "); scanf("%f",&(ptr+i)->marks); printf("\n+++++++++++++++++++++++++++++++++++++++++++++++++\n"); } } void avgmarks(struct student *ptr, int n) { int sum=0; float avg; for(i=0;i<n;i++) { sum=sum+(ptr+i)->marks; } printf("\nThe sum of all marks is = %d\n",sum); avg=sum/n; printf("\nThe Avarage of all marks is = %f\n\n",avg); } void display(struct student *ptr, int n) { printf("\nName: %s",(ptr+n)->name); printf("\nRollNo: %d",(ptr+n)->rno); printf("\nMarks: %f",(ptr+n)->marks); printf("\n\n+++++++++++++++++++++++++++++++++++++++++++++++++\n"); } void maxmarks(struct student *ptr, int n) { int max=ptr->marks,k; for(i=0;i<n;i++) { if ((ptr+i)->marks>max) { max=(ptr+i)->marks; k=i; } } printf("The Details of Student who took Highest Marks is:\n"); display(ptr,k); }
OUTPUT
Enter the no. of students you want to give info: 3 Enter Details of Student 1 Enter the Name: Jam Enter the RollNo: 50 Enter the Marks: 100 +++++++++++++++++++++++++++++++++++++++++++++++++ Enter Details of Student 2 Enter the Name: Coke Enter the RollNo: 80 Enter the Marks: 30 +++++++++++++++++++++++++++++++++++++++++++++++++ Enter Details of Student 3 Enter the Name: Bull Enter the RollNo: 200 Enter the Marks: 120 +++++++++++++++++++++++++++++++++++++++++++++++++ The sum of all marks is = 250 The Avarage of all marks is = 83.000000 The Details of Student who took Highest Marks is: Name: Bull RollNo: 200 Marks: 120.000000 +++++++++++++++++++++++++++++++++++++++++++++++++ Process returned 0 (0x0) execution time : 44.351 s Press any key to continue.
Please find some more codes of 1D Arrays, 2D Arrays, Pointers on the below page:
Top 100+ C Programming codes – KLE Technological University