---
title: "Student file displayed in descending marks order"
subject: "Object Oriented Programming"
chapter: 8
key: "semester_3/object_oriented_programming:student-file-descending"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/student-file-descending
site: BCT Engineering
---

# Student file displayed in descending marks order

Object Oriented Programming · Chapter 8: Stream Computation for Console and File Input/Output

- Asked in: 2070 Asar, 6 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/student-file-descending
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-8

## Question

Write a program that stores information of a students in a file and display the file's content in descending order according to their marks obtained.

## Short answer

Write student records (roll, name, marks) to a binary file. Read them into an array, sort descending by marks (e.g., bubble sort), and display. This loads all records into memory for sorting.

## Answer

Approach: write student records to a binary file, read them all back into an array, sort the array by marks in descending order (bubble sort), and display it.

```cpp
#include <iostream>
#include <fstream>
using namespace std;

class Student {
    int roll;
    char name[30];
    float marks;
public:
    void getData() { cout << "Roll, Name, Marks: "; cin >> roll >> name >> marks; }
    void display() { cout << roll << "\t" << name << "\t" << marks << endl; }
    float getMarks() { return marks; }
};

int main() {
    Student s[50], temp;
    int n;
    cout << "Number of students: ";
    cin >> n;

    ofstream fout("marks.dat", ios::binary);
    for (int i = 0; i < n; i++) {
        s[i].getData();
        fout.write((char*)&s[i], sizeof(Student));
    }
    fout.close();

    ifstream fin("marks.dat", ios::binary);       // read back
    int count = 0;
    while (fin.read((char*)&s[count], sizeof(Student)))
        count++;
    fin.close();

    for (int i = 0; i < count - 1; i++)           // sort descending
        for (int j = 0; j < count - 1 - i; j++)
            if (s[j].getMarks() < s[j + 1].getMarks()) {
                temp = s[j]; s[j] = s[j + 1]; s[j + 1] = temp;
            }

    cout << "\nRoll\tName\tMarks (descending)\n";
    for (int i = 0; i < count; i++)
        s[i].display();
    return 0;
}
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What is the approach used to display records in descending order of marks?
   A. Write the file in sorted order using ofstream
   B. Read all records into an array, sort the array, then display it
   C. Use seekg to read from the file's last record
   D. Sort the file contents on disk directly
   Answer: B. Records are written, read back into an array, sorted in memory with bubble sort and then displayed. (Key point: Overall approach)

2. Which comparison in the bubble sort produces descending order?
   A. swap if s[j].getMarks() > s[j+1].getMarks()
   B. swap if s[j].getMarks() < s[j+1].getMarks()
   C. swap if s[j].getMarks() == s[j+1].getMarks()
   D. never swap
   Answer: B. Swapping when the left mark is smaller pushes larger marks toward the front, giving descending order. (Key point: Descending comparison)

3. Why does the program count records with count++ in the read loop?
   A. To close the file after count reaches 50
   B. To compute the sum of marks
   C. To set the array size at run time
   D. To know how many records were actually read, since the array holds up to 50
   Answer: D. The file may hold fewer than 50 records, so count is used as the real number of elements to sort and display. (Key point: Counting records read)

## Related in Object Oriented Programming

- [File stream; student binary file program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afile-stream-student-binary) — Object Oriented Programming, chapter 8
- [Student records file with CRN search](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-crn-file-search) — Object Oriented Programming, chapter 8
- [Student file read/write and modify by roll](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-file-modify-roll) — Object Oriented Programming, chapter 8
- [Protected specifier; student/test/result program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprotected-three-class-program) — Object Oriented Programming, chapter 6
- [Write and display 10 employees using file](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aemployee-file-program) — Object Oriented Programming, chapter 8
- [Abstract Student class program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-abstract-program) — Object Oriented Programming, chapter 7

## Related in other subjects

- [Pass student array; display passed students](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-whole-array-student-info) — Computer Programming, chapter 7
- [96 students record; pass marks](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-96-students-ioe-txt) — Computer Programming, chapter 10
- [Binary file of 48 students; highest mark](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-binary-48-students-highest) — Computer Programming, chapter 10
- [Normal: IOE marks of 5000 students](https://www.bctengineer.com/question.md?key=semester_5%2Fprobability_and_statistics%3Anormal-5000-students-marks) — Probability and Statistics, chapter 3
