Sort students by marks (descending order) Dry Run in C

Sort students by marks (descending order) is an interactive C dry run visualizer from the Structures Arrays programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

Sort students by marks (descending order) Program Code

#include <stdio.h>
#include <string.h>

struct Student {
    int roll;
    char name[40];
    float marks;
};

void sortByMarks(struct Student s[], int n) {
    struct Student temp;
    for(int i = 0; i < n-1; i++) {
        for(int j = 0; j < n-i-1; j++) {
            if(s[j].marks < s[j+1].marks) {  // Descending order
                // Swap entire structures
                temp = s[j];
                s[j] = s[j+1];
                s[j+1] = temp;
            }
        }
    }
}

int main() {
    struct Student students[50];
    int n;
    
    printf("Enter number of students: ");
    scanf("%d", &n);
    
    for(int i = 0; i < n; i++) {
        printf("\nStudent %d:\n", i+1);
        printf("Roll: "); scanf("%d", &students[i].roll);
        printf("Name: "); scanf(" %[^\n]", students[i].name);
        printf("Marks: "); scanf("%f", &students[i].marks);
    }
    
    sortByMarks(students, n);
    
    printf("\n========== MERIT LIST (Highest to Lowest) ==========\n");
    printf("%-5s %-25s %-8s\n", "Rank", "Name", "Marks");
    for(int i = 0; i < n; i++) {
        printf("%-5d %-25s %-8.2f\n", i+1, students[i].name, students[i].marks);
    }
    
    return 0;
}

View the complete Structures Arrays programs page.

Program Console Sort students by marks (descending order) Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.