Skip to content

pc014.c

Problem Statement

Write a c program that defines a structure Student with the following members: roll (int), name (string), and marks (float). Do the below: Create an array to store details for 3 students. Read the details for these 3 student from a file named students.txt. (Assume the file contains data in the format: Roll Name Marks). Implement a recursive function float calculateTotal(struct Student arr[], int n) to calculate the sum of marks of all students in the array. Display each student's details and the total marks calculated by the recursive function.

Metadata

Property Detail
Author Amit Dutta (amitdutta4255@gmail.com)
License MIT
Difficulty Intermediate (index: 4 / 10)

Concepts

Beta Feature

This concept detection system is still in beta and may occasionally show incorrect or incomplete results.

  • Pointers
  • Recursion
  • Array
  • Iteration

Actions

Raw View on GitHub

You can print or save this file by opening Raw and using your browser.

Source Code

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

#define FILENAME "students.txt"

typedef struct Student {
    int roll;
    char name[20];
    float marks;
} Stu;

void printDetails(Stu *, int);
float calculateTotal(struct Student [], int);

int main() {
    FILE *input = NULL;
    Stu stu[3]; 
    int i = 0;

    input = fopen(FILENAME, "r");
    if(input == NULL) {
        printf("\nError opening file %s. Please try again.", FILENAME);
        exit(1);
    }

    while(i < 3 && (fscanf(input, "%d %s %f", &stu[i].roll, &stu[i].name, &stu[i].marks) == 3)) i++;
    printDetails(stu, 3);
    printf("\n\nTotal Marks: %g", calculateTotal(stu, 3));
    fclose(input);
    return 0;
}

float calculateTotal(struct Student stu[], int n) {
    if(n <= 0) {
        return 0;
    }
    return stu[n - 1].marks + calculateTotal(stu, n-1);
}

void printDetails(Stu *stu, int n) {
    int i;
    printf("\n== Student Details ==");
    for(i = 0; i < n; i++) {
        printf("\nStudent Roll:  %d"
               "\nStudent Name:  %s"
               "\nStudent Marks: %g\n",
               stu[i].roll, stu[i].name, stu[i].marks);
    }
}

Explanation

Explain with AI

Copy the prompt below and paste it into any AI assistant.

    You are explaining a C programming code to a beginner.

    STRICT RULES:

    - Only use the given code. Do NOT assume anything not present.

    - Do NOT add extra examples.

    - Keep explanation clear and short.

    - If something is unclear, say "Not clear from code".

    - Follow the exact format below. Do NOT change headings.

    FORMAT:

    [START]

    ## What it does

    (Explain the overall purpose in 1-2 sentences)

    ## Step-by-step

    (Explain how the code works in steps, simple language)

    ## Key Concepts

    (List concepts like loop, condition, function, etc.)

    ## Notes

    (Mention any limitations, errors, or assumptions)

    [END]

    CODE (pc014.c):

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

    #define FILENAME "students.txt"

    typedef struct Student {
        int roll;
        char name[20];
        float marks;
    } Stu;

    void printDetails(Stu *, int);
    float calculateTotal(struct Student [], int);

    int main() {
        FILE *input = NULL;
        Stu stu[3]; 
        int i = 0;

        input = fopen(FILENAME, "r");
        if(input == NULL) {
            printf("\nError opening file %s. Please try again.", FILENAME);
            exit(1);
        }

        while(i < 3 && (fscanf(input, "%d %s %f", &stu[i].roll, &stu[i].name, &stu[i].marks) == 3)) i++;
        printDetails(stu, 3);
        printf("\n\nTotal Marks: %g", calculateTotal(stu, 3));
        fclose(input);
        return 0;
    }

    float calculateTotal(struct Student stu[], int n) {
        if(n <= 0) {
            return 0;
        }
        return stu[n - 1].marks + calculateTotal(stu, n-1);
    }

    void printDetails(Stu *stu, int n) {
        int i;
        printf("\n== Student Details ==");
        for(i = 0; i < n; i++) {
            printf("\nStudent Roll:  %d"
                   "\nStudent Name:  %s"
                   "\nStudent Marks: %g\n",
                   stu[i].roll, stu[i].name, stu[i].marks);
        }
    }