bsc

Comprehensive codebase and cou...
Log | Files | Refs | README | LICENSE

pc014.c (1921B)


      1 /*
      2  * Author: Amit Dutta (amitdutta4255@gmail.com) | Date: 30 Mar 2026
      3  * Repo: https://github.com/notamitgamer/bsc
      4  * License: MIT
      5  */
      6 
      7 /* Write a c program that defines a structure Student with the following members: roll (int), name (string), and marks (float).
      8  Do the below: 
      9   * Create an array to store details for 3 students.
     10   * Read the details for these 3 student from a file named students.txt. (Assume the file contains data in the format: Roll Name Marks).
     11   * Implement a recursive function float calculateTotal(struct Student arr[], int n) to calculate the sum of marks of all students in the array.
     12   * Display each student's details and the total marks calculated by the recursive function. 
     13 */
     14 
     15 #include<stdio.h>
     16 #include<stdlib.h>
     17 #include<string.h>
     18 
     19 #define FILENAME "students.txt"
     20 
     21 typedef struct Student {
     22     int roll;
     23     char name[20];
     24     float marks;
     25 } Stu;
     26 
     27 void printDetails(Stu *, int);
     28 float calculateTotal(struct Student [], int);
     29 
     30 int main() {
     31     FILE *input = NULL;
     32     Stu stu[3]; 
     33     int i = 0;
     34 
     35     input = fopen(FILENAME, "r");
     36     if(input == NULL) {
     37         printf("\nError opening file %s. Please try again.", FILENAME);
     38         exit(1);
     39     }
     40 
     41     while(i < 3 && (fscanf(input, "%d %s %f", &stu[i].roll, &stu[i].name, &stu[i].marks) == 3)) i++;
     42     printDetails(stu, 3);
     43     printf("\n\nTotal Marks: %g", calculateTotal(stu, 3));
     44     fclose(input);
     45     return 0;
     46 }
     47 
     48 float calculateTotal(struct Student stu[], int n) {
     49     if(n <= 0) {
     50         return 0;
     51     }
     52     return stu[n - 1].marks + calculateTotal(stu, n-1);
     53 }
     54 
     55 void printDetails(Stu *stu, int n) {
     56     int i;
     57     printf("\n== Student Details ==");
     58     for(i = 0; i < n; i++) {
     59         printf("\nStudent Roll:  %d"
     60                "\nStudent Name:  %s"
     61                "\nStudent Marks: %g\n",
     62                stu[i].roll, stu[i].name, stu[i].marks);
     63     }
     64 }
© notamitgamer • Site Built: 2026-07-21 13:58:23 UTC • git-mirror commit: 1037f62 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror