Read values from a column in a file

c
72

Solution 1

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) );

Solution 2

#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);
}
Share:
72

Related videos on Youtube

sandalwood
Author by

sandalwood

Updated on November 29, 2022

Comments

  • sandalwood
    sandalwood over 1 year

    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 
    
    • Some programmer dude
      Some programmer dude over 8 years
      You do know about structures? You do know about arrays? You do (obviously) know about loops. Explore those three concepts and experiment with them.
    • David C. Rankin
      David C. Rankin over 8 years
      If you do not want number, then there i no need to fill it. fscanf will allow the use of the assignment suppression operator '*' (e.g. fscanf(fp, "%*d %f %f %f %f", &mark1, &mark2, &mark3, &mark4 ) Using '*' will effectively read and discard the value without adding to the match count.
    • sandalwood
      sandalwood over 8 years
      @JoachimPileborg I completely forgot about arrays, thank you. @DavidC.Rankin Sorry but I think you misunderstood, it is not that I don't want number but I just wanted to be able to read values from each column and add those together rather than a row. Thank you.
    • user3629249
      user3629249 over 8 years
      this line: printf("File cannot be opened"); fails to tell the user the root cause of the problem with opening the file. Suggest: `perror("fopen failed for inpput of grades.txt"); Which tells the user what file failed. The perror() function will also output the relevant system error message (selected by the value in errno) that says why the system thinks the call to fopen() failed
    • Julie Pelletier
      Julie Pelletier over 7 years
      Does the sound work on the host? What is the host's OS?
    • Halnex
      Halnex over 7 years
      Yes, the sound works on the host. Windows 10.
    • Halnex
      Halnex over 7 years
      @GAD3R I already tried that, it doesn't help and it automatically unchecks after I check the "Connected" checkbox.
  • mpromonet
    mpromonet over 8 years
    Don't you think It could be easier to understand replacing somme with sum ?
  • sandalwood
    sandalwood over 8 years
    Yes it would but this was only an example of how I would approach it not something I would use one for one.
  • user3629249
    user3629249 over 8 years
    it is better to check the returned value from fscanf() for successful input conversions because any other value is an error. If you wanted to recover from mal-formed lines in the file, the read each line (via fgets()) into a buffer and use sscanf() to extract each field
  • Halnex
    Halnex over 7 years
    I don't see ALSA sound card in the dropdown of VM settings. doing alsa-utils on Kali returns bash: alsa-utils: command not found which is weird because it is already installed.
  • GAD3R
    GAD3R over 7 years
    And the output of aplay -l ?
  • Halnex
    Halnex over 7 years
    This card 0: AudioPCI [Ensoniq AudioPCI], device 0: ES1371/1 [ES1371 DAC2/ADC] and this card 0: AudioPCI [Ensoniq AudioPCI], device 1: ES1371/2 [ES1371 DAC1]
  • GAD3R
    GAD3R over 7 years
    install oss4-base package then run dpkg-reconfigure linux-sound-base
  • Halnex
    Halnex over 7 years
    Installed oss4-base but running dpkg-reconfigure linux-sound-base returns this pkg-query: package 'linux-sound-base' is not installed and no information is available and trying to install linux-sound-base returns this Package linux-sound-base is not available, but is referred to by another package. This may mean that the package is missing, has been obsoleted, or is only available from another source. However the following packages replace it: oss4-base
  • GAD3R
    GAD3R over 7 years
    test the sound from the terminal as user speaker-test -c 2
  • Halnex
    Halnex over 7 years
    This is the outcome pastebin.com/trx4FEiA but no sound came out from the headphones.
  • Halnex
    Halnex over 7 years