Home > Net >  trying to make my code printout the correct product
trying to make my code printout the correct product

Time:09-23

The sum calculation is fine, however, for product the numbers used creates a answer larger then 10 integers, which gives an incorrect answer. I understand that its because I am using int. But when I tried float, that also didn't work.

Can someone explain how to make my product answer able to printout answers larger then 10 integers.

For example, the expected output of 6,20,4,16,15,11,6,3,19, 18 is 7800883200 but my system printsout 72076004000

Thank you

#include <string.h>
#include <stdlib.h>


int main(int argc, char* argv[]){
    
    if(argc<2){
        printf("Invalid Input: filename value missing\n");
    }
    else{
        char filename[50]; 
        strcpy(filename,argv[1]);
        FILE* fptr = fopen(filename,"r");
        
        if(fptr==NULL){
            printf("File not found!\n");
        }
        else{
            
            int arr[10]; 
            int i; 
            int val; 
            for(i=0;i<10;i  ){
                fscanf(fptr,"%d",&val); 
                arr[i] = val;
            }

          
            int sum = 0; 
            
            for(i=0;i<10;i  ){
                
                sum= sum   arr[i];
            }

            
           int product = 1;
            for(i=0;i<10;i  ){
                product= product * arr[i]; 
            }

            printf("Sum: %d\n",sum);
            printf("Product: %d\n",product);
            fclose(fptr);
        }
    }
    return 0;
}

CodePudding user response:

You need to use type with sufficient range to store your product (and sum) so instead of int use long long or alternatively double.

Missing an include. Using strerror() to generate a proper error message. No need to copy file name before opening it. You are running the same loop 3 times so might as well combine them. thsi means you no longer need your array.

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

int main(int argc, char* argv[]) {
    if(argc<2){
        printf("Invalid Input: filename value missing\n");
        return 1;
    }
    FILE *fptr = fopen(argv[1], "r");
    if(!fptr){
        printf("%s\n", strerror(errno));
        return 1;
    }
    long long sum = 0;
    long long product = 1;
    for(unsigned i=0; i<10; i  ) {
        int val;
        fscanf(fptr,"%d",&val);
        sum  = val;
        product *= val;
    }
    printf(
        "Sum: %lld\n"
        "Product: %lld\n",
        sum,
        product
    );
    fclose(fptr);
}
$ seq 10  | ./a.out /dev/stdin
Sum: 55
Product: 3628800

CodePudding user response:

To print bigger numbers, still with an unavoidable ceiling, use the additional bits available with doubles.

From Wikipedia:

Double precision (binary64), usually used to represent the "double" type in the C language family. This is a binary format that occupies 64 bits (8 bytes) and its significand has a precision of 53 bits (about 16 decimal digits).

This answers your "how can I?" It's an exercise for you to integrate this knowledge into your program.

#include <stdio.h>

int a[] = { 6,20,4,16,15,11,6,3,19, 18 };

int main() {
    double x = 1;

    for( int i = 0; i < 10; i   ) {
        printf( "%.0lf * %d = %.0lf\n", x, a[i], x * a[i] );
        x = x * a[i];
    }

    return 0;
}

Output

1 * 6 = 6
6 * 20 = 120
120 * 4 = 480
480 * 16 = 7680
7680 * 15 = 115200
115200 * 11 = 1267200
1267200 * 6 = 7603200
7603200 * 3 = 22809600
22809600 * 19 = 433382400
433382400 * 18 = 7800883200 // <== your sought-after number

There are libraries available to work with arbitrary arithmetic values that can be as big as will fit in your computer.

  •  Tags:  
  • c
  • Related