Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New To C, Simple Loop

Tags:

c

I have a file called testdata1 and I have 20 values of the same number in it. How can I add all these numbers and then print that number onto the screen. I currently have this, but I know that it's probably the worst way to do it and should probably use a loop.

#include <stdio.h>

int main(int argc, char *argv[]) {
FILE* fin;
fin = fopen("testdata1.txt", "r");
int n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, n13, n14, n15, n16, n17, n18, n19, n20, sum;

fscanf(fin, "%d", &n1);
fscanf(fin, "%d", &n2);
fscanf(fin, "%d", &n3);
fscanf(fin, "%d", &n4);
fscanf(fin, "%d", &n5);
fscanf(fin, "%d", &n6);
fscanf(fin, "%d", &n7);
fscanf(fin, "%d", &n8);
fscanf(fin, "%d", &n9);
fscanf(fin, "%d", &n10);
fscanf(fin, "%d", &n11);
fscanf(fin, "%d", &n12);
fscanf(fin, "%d", &n13);
fscanf(fin, "%d", &n14);
fscanf(fin, "%d", &n15);
fscanf(fin, "%d", &n16);
fscanf(fin, "%d", &n17);
fscanf(fin, "%d", &n18);
fscanf(fin, "%d", &n19);
fscanf(fin, "%d", &n20);

sum = n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9 + n10 + n11 + n12 + n13 + n14 + n15 + n16 + n17 + n18 + n19 + n20;
printf("The sum of numbers is %d.\n", sum);
fclose(fin);

return 0; 
}
like image 773
Bill Avatar asked Feb 06 '23 02:02

Bill


2 Answers

You don't really need to use an array. Just add the numbers together as you read them in:

#include <stdio.h>

int main(int argc, char *argv[]) {
    int i, n, sum = 0;
    FILE* fin = fopen("testdata1.txt", "r");
    if (!fin) {
        fprintf(stderr,"Error: Unable to open file\n");
        return 1;
    }

    for (i=0; i<20; i++) {
        if (fscanf(fin, "%d", &n) != 1) {
            fprintf(stderr,"Error: Unexpected input\n");
            fclose(fin);
            return 1;
        }
        sum += n;
    }
    printf("The sum of numbers is %d.\n", sum);
    fclose(fin);

    return 0; 
}
like image 137
r3mainer Avatar answered Mar 23 '23 21:03

r3mainer


I'm not really sure what you're asking here but I'm pretty sure you're asking for a loop that can add all these values, so I'll go off of that.

First though there are some terrible mistakes in your design, the first of which is how you declare your variables. You really don't need to declare 20 variables. You can use two methods depending on your needs:

A. Declare an array in case you need to access all of those 20 variables later on as such: int values[20]

B. Declare two int variables at the top, one for the sum and one for storing the next number from the file

For case A it would look something like this:

int main(int argc, char* argv[]){
    int sum, values[20];
    FILE* file = fopen("file");
    for (i=0; i<20; i++) {
        if (fscanf(fin, "%d", &values[i]) != 1) {
            printf("%s\n","error handling here");
            fclose(fin);
            return 1;
        }
        sum += n;
    }
}

As for case B, well squeamish ossifrage already answered it for you :)

like image 22
zee Avatar answered Mar 23 '23 21:03

zee