0% found this document useful (0 votes)
17 views5 pages

Assigntment

The document describes a program that manages student data by reading from a file, printing the list of students, sorting them based on their marks using bubble sort, and writing the sorted data to a new file. It also includes functionality to print the contents of the new file and calculate the average mark of the students. The provided source code implements these features in C programming language.

Uploaded by

Đức Hạnh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views5 pages

Assigntment

The document describes a program that manages student data by reading from a file, printing the list of students, sorting them based on their marks using bubble sort, and writing the sorted data to a new file. It also includes functionality to print the contents of the new file and calculate the average mark of the students. The provided source code implements these features in C programming language.

Uploaded by

Đức Hạnh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

SOLVE THE PROBLEM

Reading Student Data from File (Problem 1): The program starts by opening the file named
"students.txt" in read mode. It reads data for each student from the file, including their name,
address, and mark. This data is stored in an array of structures called students. Printing Student List

(Problem 2): After reading the data, the program prints the list of students stored in the students
array. It iterates through the array and prints each student's name, address, and mark. Sorting
Students Based on Marks

(Problem 3): To sort the students based on their marks in descending order, the program uses the
bubble sort algorithm. The bubbleSort function compares adjacent elements of the students array
and swaps them if they are in the wrong order (i.e., if the mark of the current student is less than the
mark of the next student). This process is repeated until the entire array is sorted in descending
order of marks. Writing Sorted Data to Another File

(Problem 4): Once the students are sorted based on marks, the program opens a new file named
"students_2.txt" in write mode. It then writes the sorted student data to this file in the same format
as the original file, i.e., each line contains the name, address, and mark of a student. Printing
Contents of students_2.txt

(Problem 5): After writing the sorted data to the "students_2.txt" file, the program opens this file in
read mode. It reads each line from the file and prints its contents, effectively displaying the sorted list
of students. Calculating Average Mark of Students

(Problem 6): Finally, the program calculates the average mark of all students. It iterates through the
students array, summing up the marks of all students. The total sum is divided by the number of
students to compute the average mark, which is then printed to the console.

This algorithm efficiently manages text files, reads and writes data, sorts records, and performs basic
calculations on student marks.

SRC CODE

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX_STUDENTS 100

struct Student {

char name[50];

char address[100];
int mark;

};

void swap(struct Student *a, struct Student *b) {

struct Student temp = *a;

*a = *b;

*b = temp;

void bubbleSort(struct Student arr[], int n) {

for (int i = 0; i < n-1; i++) {

for (int j = 0; j < n-i-1; j++) {

if (arr[j].mark < arr[j+1].mark) {

swap(&arr[j], &arr[j+1]);

void printStudents(struct Student students[], int n) {

printf("List of Students:\n");

for (int i = 0; i < n; i++) {

printf("Name: %s, Address: %s, Mark: %d\n", students[i].name, students[i].address,


students[i].mark);

printf("\n");

int main() {

// Problem 1: Read student data from file

FILE *file = fopen("students.txt", "r");


if (file == NULL) {

printf("Error opening file.\n");

return 1;

struct Student students[MAX_STUDENTS];

int count = 0;

while (fscanf(file, "%s %s %d", students[count].name, students[count].address,


&students[count].mark) != EOF) {

count++;

if (count >= MAX_STUDENTS) {

printf("Maximum number of students exceeded.\n");

break;

fclose(file);

// Problem 2: Print student list

printf("Problem 1:\n");

printStudents(students, count);

// Problem 3: Sort students based on marks in descending order

printf("Problem 2:\n");

bubbleSort(students, count);

printf("Students sorted based on marks in descending order:\n");

printStudents(students, count);

// Problem 4: Write sorted data to another file

printf("Problem 3:\n");

FILE *outfile = fopen("students_2.txt", "w");


if (outfile == NULL) {

printf("Error opening output file.\n");

return 1;

for (int i = 0; i < count; i++) {

fprintf(outfile, "%s %s %d\n", students[i].name, students[i].address, students[i].mark);

fclose(outfile);

printf("Data written to students_2.txt successfully.\n");

// Problem 5: Print the contents of students_2.txt

printf("Problem 4:\n");

printf("Contents of students_2.txt:\n");

outfile = fopen("students_2.txt", "r");

if (outfile == NULL) {

printf("Error opening output file.\n");

return 1;

char line[256];

while (fgets(line, sizeof(line), outfile)) {

printf("%s", line);

fclose(outfile);

// Problem 6: Calculate average mark of students

printf("Problem 5:\n");

int sum = 0;

for (int i = 0; i < count; i++) {


sum += students[i].mark;

float average = (float)sum / count;

printf("Average mark of students: %.2f\n", average);

return 0;

MEMBER
Nguyễn Đức Hạnh – HE194732 (50 %)
Lã Xuân Nam -HE191400 (50%) - leader

You might also like