sandalwood
sandalwood

Reputation: 41

Read values from a column in a file

I am trying to read values from a file from a specific column (ignoring the first number) and then find the average of that column. So far I have this code which works for reading and finding the average of the line however not for the column.

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

int main(){

    FILE *fp;
    int id;
    float mark1;
    float mark2;
    float mark3;
    float mark4;
    int number;
    float average;

    fp= fopen("grades.txt", "r");

    if(fp == NULL){
        printf("File cannot be opened");
        exit(-1);
    }

    while(fscanf(fp, "%d %f %f %f %f", &number, &mark1, &mark2, &mark3, &mark4 ) != EOF){
        //code to calculate average and print output
        //Below is the code I used to find the average of the line (not what I want to do)
        average = (mark1 + mark2 + mark3+ mark4)/4;
        printf("Average for %d : %.2f\n", number, average);

    }

    fclose(fp);
}

So in the file there is a total of 5 columns, the first is the ID which is just and interger value (int number) and the others numbers are all floats.

FILE CONTENTS:

12345 60 30 63.2 95
54321 54.2 49 40.5 80
15243 99.5 100 90 98 

Upvotes: 1

Views: 91

Answers (3)

user3629249
user3629249

Reputation: 16540

the following code is:

  • more complex than necessary to keep it simple, easy to follow.
  • compiles cleanly
  • performs the desired function

A bit of thinking and paper//pencil work would have given you similar logic

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

int main( void )
{

    FILE *fp;

    float mark1;
    float mark1Average = 0.0f;
    float mark2;
    float mark2Average = 0.0f;
    float mark3;
    float mark3Average = 0.0f;
    float mark4;
    float mark4Average = 0.0f;
    int   number;
    int   numberAverage = 0;


    fp= fopen("grades.txt", "r");

    if(fp == NULL){
        printf("File cannot be opened");
        exit(-1);
    }

    // get first line to initialize average values
    if  ( 5 != fscanf( fp, "%d %f %f %f %f",
                &numberAverage,
                &mark1Average,
                &mark2Average,
                &mark3Average,
                &mark4Average )
        )
    { // then fscanf failed
        perror( "fscanf for first line of grades.txt failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, fscanf successful

    while(5 == fscanf(fp, "%d %f %f %f %f", &number, &mark1, &mark2, &mark3, &mark4 ))
    {
        numberAverage = (numberAverage+number)/2;
        mark1Average  = (mark1Average+mark1)/2.0f;
        mark2Average  = (mark2Average+mark2)/2.0f;
        mark3Average  = (mark3Average+mark3)/2.0f;
        mark4Average  = (mark4Average+mark4)/2.0f;
    }

    printf( "numberAverage: %d\n", numberAverage);
    printf( "mark1Average:  %f\n", mark1Average);
    printf( "mark2Average:  %f\n", mark2Average);
    printf( "mark3Average:  %f\n", mark3Average);
    printf( "mark4Average:  %f\n", mark4Average);

    fclose(fp);
}

Upvotes: 0

V. Michel
V. Michel

Reputation: 1619

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

int main(){

    FILE *fp;
    int id;
    float mark1;
    float mark2;
    float mark3;
    float mark4;
    int number;
    float somme[4];
    int nbLg=0;
    int i;
    fp= fopen("grades.txt", "r");

    if(fp == NULL){
        printf("File cannot be opened");
        exit(-1);
    }

    while(fscanf(fp, "%d %f %f %f %f", &number, &mark1, &mark2, &mark3, &mark4 ) != EOF){
        nbLg++;
        somme[0]+= mark1;
        somme[1]+= mark2;
        somme[2]+= mark3;
        somme[3]+= mark4;

    }

    for(i=0;i<=3;i++) {
        printf("Average for %d : %.2f\n", i+1, somme[i] / (float) nbLg);
    }
    fclose(fp);
}

Upvotes: 2

Haris
Haris

Reputation: 12270

You can keep a variable sum to add all the values of a particular column, and later use that to get the average, something like

int sum=0, n=0;

while(fscanf(fp, "%d %f %f %f %f", &number, &mark1, &mark2, &mark3, &mark4 ) != EOF){
    //code to calculate average and print output
    //Below is the code I used to find the average of the line (not what I want to do)
    average = (mark1 + mark2 + mark3+ mark4)/4;
    printf("Average for %d : %.2f\n", number, average);

    sum += mark2 //this can be any mark depending on your requirement
    n++;         //this is to keep track of the number of lines

}

printf("the average of column 2 is %d", (sum/n) );

Upvotes: 3

Related Questions