Read values from a column in a file
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);
}
Related videos on Youtube
Author by
sandalwood
Updated on November 29, 2022Comments
-
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 over 8 yearsYou 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 over 8 yearsIf you do not want
number
, then there i no need to fill it.fscanf
will allow the use of theassignment 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 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 over 8 yearsthis 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 over 7 yearsDoes the sound work on the host? What is the host's OS?
-
Halnex over 7 yearsYes, the sound works on the host. Windows 10.
-
Halnex over 7 years@GAD3R I already tried that, it doesn't help and it automatically unchecks after I check the "Connected" checkbox.
-
-
mpromonet over 8 yearsDon't you think It could be easier to understand replacing
somme
withsum
? -
sandalwood over 8 yearsYes it would but this was only an example of how I would approach it not something I would use one for one.
-
user3629249 over 8 yearsit 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 (viafgets()
) into a buffer and usesscanf()
to extract each field -
Halnex over 7 yearsI don't see ALSA sound card in the dropdown of VM settings. doing
alsa-utils
on Kali returnsbash: alsa-utils: command not found
which is weird because it is already installed. -
GAD3R over 7 yearsAnd the output of
aplay -l
? -
Halnex over 7 yearsThis
card 0: AudioPCI [Ensoniq AudioPCI], device 0: ES1371/1 [ES1371 DAC2/ADC]
and thiscard 0: AudioPCI [Ensoniq AudioPCI], device 1: ES1371/2 [ES1371 DAC1]
-
GAD3R over 7 yearsinstall
oss4-base
package then rundpkg-reconfigure linux-sound-base
-
Halnex over 7 yearsInstalled
oss4-base
but runningdpkg-reconfigure linux-sound-base
returns thispkg-query: package 'linux-sound-base' is not installed and no information is available
and trying to installlinux-sound-base
returns thisPackage 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 over 7 yearstest the sound from the terminal as user
speaker-test -c 2
-
Halnex over 7 yearsThis is the outcome pastebin.com/trx4FEiA but no sound came out from the headphones.
-
Halnex over 7 yearsLet us continue this discussion in chat.