0

I have tried to create a C++ program to solve Project Euler's 8th problem. However, my program is not outputting the correct answer. I have tried to trace through it, but I can't locate my logical error.

Project Euler's 8th problem is about finding the value of the highest product of thirteen adjacent digits in a number with 1000 digits. My code outputs 9223372019204292608 as the answer. The correct answer is I believe 23514624000. My code is the following :

    #include <iostream>
    #include <cassert>
    using namespace std;

    int main() {

       //Declare input number as a string. Declare variables
       string input = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";

       char initialArray[1000];
       int finalArray[1000];
       long products[987];
       long highestProduct = 0L;

       //Divide input string into an array of single characters
       for(int i = 0; i < 1000 ; i++){
            initialArray[i] = input.at(i);
       }

       //Translate the array of characters into an array of single-digit integers
       for(int i = 0; i < 1000 ; i++){
           finalArray[i] = input.at(i) - '0';
       }

       //Initialize each element in the products array to 1
       for (int i = 0; i < 100 ; i++){
           products[i] = 1L;
       }


       //Perform multiplications
       for(int i = 0; i < 987; i++){
           for( int j = 0; j < 13; j++){
               products[i] = products[i] * finalArray[j+i];
           }
       }

       for(int i = 0; i < 987; i++){
           if (products[i] > highestProduct){
               highestProduct = products[i];
           }
       }

       cout << "The highest product of 13 adjacent digits from the input is: " <<                            highestProduct << endl;

       return 0;
   }

   //Correct answer is supposed to be 23514624000
izhang05
  • 744
  • 2
  • 11
  • 26
Pi_
  • 23
  • 4
  • 1
    Only 1/10th of the products array is being initialized to 1. Check your third loop. This probably would not have happened if using vectors instead of arrays. – Michaël Roy Apr 13 '20 at 16:16
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5910058) – Jesper Juhl Apr 13 '20 at 16:23
  • Why do you need the arrays? Search the internet for "c++ running maximum" for an idea of how to not use the arrays. – Thomas Matthews Apr 13 '20 at 16:34

2 Answers2

2

You are not initializing all of the products array. Ideally you shouldn't be initializing them separately at all.

//Perform multiplications
for(int i = 0; i < 987; i++)
{
  products[i] = 1L;  // initialize
  for( int j = 0; j < 13; j++)
  {
     products[i] = products[i] * finalArray[j+i];
  }
}

This way, you can't go wrong with initializing all the elements.

cigien
  • 57,834
  • 11
  • 73
  • 112
  • 1
    Or `products[i] = std::accumulate(finalArray + i, finalArray + i + 13, 1u, std::multiplies<>{});`. – Jarod42 Apr 13 '20 at 16:22
  • Thank you for pointing that out @cigien. I have edited that in my original code however it still doesn't output the correct answer to the problem – Pi_ Apr 13 '20 at 16:30
1

Look here:

//Initialize each element in the products array to 1
for (int i = 0; i < 100 ; i++){


Also note you can use the only loop through array using update of product at every step like this:

product = (product / finalArray[i - 13]) * finalArray[i]
MBo
  • 77,366
  • 5
  • 53
  • 86