user1205487
user1205487

Reputation: 11

Output number larger than it should be - gcc compiling

My assignment is to find the greatest product from two five digits number found in a huge character array.

You have to do it by brute force.

The largest 5 digit number found in the array is 99890, so the largest product is simply that times itself which is 9978012100, but I cannot seem to get that answer, and in fact I get an answer larger than that when I run the program.

My program works when I change the program to look for the largest 4 digit number product, but it falls apart when I look for 5. I'm wondering if the numbers are just getting too big for my crappy IDE or something.

Could someone compile and run the following code in gcc and tell me what the answer they get is? It would be much appreciated.

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

int main (void){

char array[1001] = "73167176531330624919225119674426574742355349194934"
"96983520312774506326239578318016984801869478851843"
"85861560789112949495459501737958331952853208805511"
"12540698747158523863050715693290963295227443043557"
"66896648950445244523161731856403098711121722383113"
"62229893423380308135336276614282806444486645238749"
"30358907296290491560440772390713810515859307960866"
"70172427121883998797908792274921901699720888093776"
"65727333001053367881220235421809751254540594752243"
"52584907711670556013604839586446706324415722155397"
"53697817977846174064955149290862569321978468622482"
"83972241375657056057490261407972968652414535100474"
"82166370484403199890008895243450658541227588666881"
"16427171479924442928230863465674813919123162824586"
"17866458359124566529476545682848912883142607690042"
"24219022671055626321111109370544217506941658960408"
"07198403850962455444362981230987879927244284909188"
"84580156166097919133875499200524063689912560717606"
"05886116467109405077541002256983155200055935729725"
"71636269561882670428252483600823257530420752963450";

char *a_ptr, *b_ptr;
char string[6];
int i, j;
int aNumber, bNumber;
long long greatestProd, prodHolder;

a_ptr = &array[0];
b_ptr = &array[0];

for (i=0; i<996; i++){
    a_ptr = &array[i];
    strncpy(string, a_ptr, 5);
    aNumber = atoi(string);

    for (j=0; j<996; j++){
        b_ptr = &array[j];
        strncpy(string, b_ptr, 5);
        bNumber = atoi(string);


        prodHolder = aNumber * bNumber;
        if (prodHolder > greatestProd){
            greatestProd = prodHolder;
        }

    }

}
printf("%d\n", greatestProd);


return 0;
}

Upvotes: 1

Views: 104

Answers (2)

Clifford
Clifford

Reputation: 93476

A number as large as 9978012100 requires 34 bits - log(9978012100)/log(2), plus an additional bit if using signed data types. While you have used long long which is most likely 64 bits, you have not used an appropriate format specifier in the printf() call to display the result.

If your library is C99 conformant, then the correct format specifier is "%lld".

Also the result of an int * int operation is an int, you must cast at least one of the operands to generate a long long result, the implicit cast in the assignment is insufficient since the result has already been calculated and the data lost. A possibly better solution is simply to use long long for the operand types in the first instance.

Upvotes: 1

Henrik
Henrik

Reputation: 23324

prodHolder = (long long)aNumber * (long long)bNumber; 

Upvotes: 4

Related Questions