Tuesday, April 11, 2023

Coding Challenge #48

submitted for Coursera quiz. Read int's from a file, and calculate their average

#include <stdio.h>
int main(){
    FILE *filePointer; // this is the pointer to the file
    int buffer[1000]; // file content
    int safety = 10000;
    int count = 0;
    filePointer = fopen("file.tsv", "r"); // open the file with file pointer to the file
    while(fscan(filePointer, "%d\t", &buffer[count]) && --safety){
        count++;
    } // read the file the file pointer file
    fclose(filePointer); // close the file pointer to the file
    int total = 0; // total the weight
    for(int xint = 0;xint<count;xint++)total+=buffer[xint]; // it make the total the total to make the average
    int average=total/count;
    printf("average:%d",count);
}

No comments:

Post a Comment

Coding Challenge #54 C++ int to std::string (no stringstream or to_string())

Gets a string from an integer (ejemplo gratis: 123 -> "123") Wanted to come up with my own function for this like 10 years ago ...