0

I want to sort elements of the marks[] array inside a structure student using a sort function, so that marks/elements print in ascending order. But marks/elements are printing in same order.

Problem:

*Write a ‘C’ program to accept n student details as roll number, name, marks of three subjects. Calculate sum, percentage and sort student details based on percentage using following function prototypes.

void accept(struct student s[10], int n);
void display(struct student s[10], int n);
void sort(struct student s[10], int n);

Here is my code:

#include <stdio.h>
#include <stdlib.h>
 
struct student{
    char name[10];
    int rollno;
    int marks[3];  //array of 3 subjects marks
    int totalMarks;
    float percent;
};

void calculate(struct student s[10], int n) {
    //calculate total marks, &percent using loop
    int i, m;
    for (i = 0; i < n; i++) {
         s[i].totalMarks = 0;
         s[i].percent = 0.0;
         for (m = 0; m < 3; m++) {  
              s[i].totalMarks += s[i].marks[m];
         }
         s[i].percent = s[i].totalMarks / 3.0;
    }
}

void display(struct student s[10], int n) {
    int i, m;
    for (i = 0; i < n; i++) {
        printf("Student's Name: %s\n", s[i].name);
        printf("Student's Rollno: %d\n", s[i].rollno);
        for (m = 0; m < 3; m++) {
            printf("Subject %d Marks: %d\n", m+1, s[i].marks[m]);
        }
        printf("Total Marks: %d\n", s[i].totalMarks);
        printf("Total Percent: %.2f\n", s[i].percent);
        printf("\n");
    }  
}

void sort(struct student s[10], int n) {
    int i, j, m;

    //Sort elements of Array(marks[] ) inside Structure (student) in C
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (s[i].marks[m] > s[j].marks[m]) {
                struct student temp = s[i];
                s[i] = s[j];
                s[j] = temp;
            }
        }
    }

    //DISPLAY
    for (i = 0; i < n; i++) {
        printf("Student's Name: %s\n", s[i].name);
        printf("Student's Rollno: %d\n", s[i].rollno);
        for (m = 0; m < 3; m++) {
            printf("%d ", s[i].marks[m]);
        }
        printf("Total Marks: %d\n", s[i].totalMarks);
        printf("Total Percent: %.2f\n", s[i].percent);
        printf("\n");
    } 
}

int main() {
    struct student s[10]; //minimum of 10 students

    int i, n, m;
    printf("Enter the number of students: ");
    scanf("%d", &n);

    for (i = 0; i < n; i++) {
        printf("\nEnter student's name: ");
        scanf("%s", s[i].name);
        printf("Enter student's rollno: ");
        scanf("%d", &s[i].rollno);
        for (m = 0; m < 3; m++) {
            printf("Enter the student's marks: ", m + 1);
            scanf("%d", &s[i].marks[m]);
        }
        printf("\n");
    }

    calculate(s, n);  //structure,number of students

    printf("\nstudent's Information-------------\n");
    display(s, n);
    printf("\n");

    printf("\nSort the Array in ascending order of marks:");
    sort(s, n);
    printf("\n");
}

The marks don't get sorted.

1
  • If you only have a few elements (like the three you have for marks) then you can simply do it with plain if ... else if ... etc.. Commented Jul 17, 2022 at 16:16

1 Answer 1

2

There is a confusion in your code between the index i into the student array and the index m into the marks array of each student, which is not even initialized, giving the code undefined behavior. Compiling with warnings enabled (eg: gcc -Wall -Wextra -Werror) would have caught this problem.

You should iterate on the student array, and for each student, sort its mark array using 2 nested loops.

Here is a modified version:

#include <stdio.h>
 
struct student {
    char name[10];
    int rollno;
    int marks[3];  //array of 3 subjects marks
    int totalMarks;
    float percent;
};

void calculate(struct student s[10], int n) {
    //calculate total marks and percentage using loop
    for (int i = 0; i < n; i++) {
         s[i].totalMarks = 0;
         s[i].percent = 0.0;
         for (int m = 0; m < 3; m++) {  
             s[i].totalMarks += s[i].marks[m];
         }
         s[i].percent = s[i].totalMarks / 3.0;
    }
}

void display(struct student s[10], int n) {
    for (int i = 0; i < n; i++) {
        printf("Student's Name: %s\n", s[i].name);
        printf("Student's Rollno: %d\n", s[i].rollno);
        for (int m = 0; m < 3; m++) {
            printf("Subject %d Marks: %d\n", m + 1, s[i].marks[m]);
        }
        printf("Total Marks: %d\n", s[i].totalMarks);
        printf("Total Percent: %.2f\n", s[i].percent);
        printf("\n");
    }
}

/* sort an array of marks */
void sort_marks(int *marks, int n) {
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < i; j++) {
            if (marks[i] > marks[j]) {
                int temp = marks[i];
                marks[i] = marks[j];
                marks[j] = temp;
            }
        }
    }
}

void sort(struct student s[10], int n) {
    //Sort elements of Array(marks[]) inside Structure (student) in C
    for (int i = 0; i < n; i++) {
        sort_marks(s[i].marks, 3);
    }
}

int main() {
    struct student s[10] = { 0 }; //maximum of 10 students
    int n;

    printf("Enter the number of students: ");
    if (scanf("%d", &n) != 1 || n <= 0 || n > 10)
        return 1;

    for (int i = 0; i < n; i++) {
        printf("\nEnter student's name: ");
        scanf("%9s", s[i].name);
        printf("Enter student's rollno: ");
        scanf("%d", &s[i].rollno);
        for (int m = 0; m < 3; m++) {
            printf("Enter the student's marks: ", m + 1);
            scanf("%d", &s[i].marks[m]);
        }
        printf("\n");
    }

    calculate(s, n);  //structure,number of students

    printf("\nstudent's Information-------------\n");
    display(s, n);
    printf("\n");

    printf("\nSort the Array in ascending order of marks:");
    sort(s, n);
    display(s, n);
    printf("\n");
    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

... and avoiding the kind of confusion afflicting the OP's code is among the reasons to use descriptive -- or at least mnemonic -- variable names.
@JohnBollinger: good point, and splitting functions into small concise ones that perform a single task is also recommended. Using single letters for index variables in small loops is not necessarily a problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.