Dynamically Allocate Memory to For Storing Information Using Pointer Variable. (Problem 1.4)

Question 1.4 #: Write a program that will input student’s information (i.e. name, id, cgpa) who have enrolled for CSE-207 course in Summer 2020. You have to declare a pointer variable to input the information and dynamically allocate memory for storing information of each students. After taking input find out the student name who has obtained highest cgpa.


Solution: 

#include<stdio.h>
struct student_info{
    char name[30];
    char id[12];
    float cgpa;
}*student;
int main(){

    int i,n;
    printf("Enter How Many Student: ");
    scanf("%d",&n);
    student = (struct student_info*)malloc(n*sizeof(struct student_info));
    for(i=0;i<n;i++){
       printf("-------------------------------------\n");
       printf("%d. Enter the Name of the student: ",i+1);
       scanf("%s",student[i].name);
       printf("%d. Enter the Id of the student: ",i+1);
       scanf("%s",student[i].id);
       printf("%d. Enter the CGPA: ",i+1);
       scanf("%f",&student[i].cgpa);

    }
    float hight = 0;
    printf("-------------------------------------\n");
    printf("Those Who Have Enrolled CSE-207 Course in Summer-2021\n");
    printf("-------------------------------------\n");
    printf("\n");
    for(i=0;i<n;i++){
         if(hight < student[i].cgpa){
            hight = student[i].cgpa;
        }
        printf("%d. %s\t%s\t%f\n",(i+1),student[i].name,student[i].id,student[i].cgpa);
    }
    printf("\n\n");
    for(i=0;i<n;i++){
        if(hight==student[i].cgpa){
           printf("-------------------------------------\n");
           printf("Highest CGPA is : %f \nAnd Student Name is: %s",student[i].cgpa,student[i].name);
        }
    }
    printf("\n-------------------------------------\n");
}


After Run:

Enter How Many Student: 3
-------------------------------------
1. Enter the Name of the student: Rohim
1. Enter the Id of the student: 2015-3-60-032
1. Enter the CGPA: 3.64
-------------------------------------
2. Enter the Name of the student: Korim
2. Enter the Id of the student: 2010-3-99-345
2. Enter the CGPA: 3.43
-------------------------------------
3. Enter the Name of the student: Akbor
3. Enter the Id of the student: 1999-3-65-343
3. Enter the CGPA: 3.89
-------------------------------------
Those Who Have Enrolled CSE-207 Course in Summer-2021
-------------------------------------

1. Rohim        2015-3-60-032   3.640000
2. Korim        2010-3-99-345   3.430000
3. Akbor         1999-3-65-343   3.890000


-------------------------------------
Highest CGPA is : 3.890000
And Student Name is: Akbor
-------------------------------------

Process returned 0 (0x0)   execution time : 88.783 s
Press any key to continue.


Previous Post Next Post