0

I am implementing matrix chain multiplication program in c++. I am using ONLINE_JUDGE flag to write output to the file. When i run the program it does not produce correct output in file, but it is producing correct output in console.
My Program:

#include<bits/stdc++.h>
using namespace std;
int main(){
    #ifndef ONLINE_JUDGE 
        // For getting input from input.txt file 
        freopen("C:\\Users\\Rahul kumar\\desktop\\Algorithm\\input.txt", "r", stdin); 
        // Printing the Output to output.txt file 
        freopen("C:\\Users\\Rahul kumar\\desktop\\Algorithm\\output.txt", "w", stdout); 
    #endif 

    // dimension of four matrices
    vector<pair<int,int>>matrices;
    matrices.push_back(make_pair(5,4));
    matrices.push_back(make_pair(4,6));
    matrices.push_back(make_pair(6,2));
    matrices.push_back(make_pair(2,7));

    vector<int>p; // algorithm helper data
    int last; // column of last matrix. 

    for(auto matrix:matrices){
        p.push_back(matrix.first);
        last=matrix.second;
    }    
    p.push_back(last);  // add last matrix. 
    int numberOfMatrix=matrices.size();

    int dp[numberOfMatrix][numberOfMatrix];
    for(int i=0;i<numberOfMatrix;i++){
        for(int j=0;j<numberOfMatrix;j++){
            dp[i][j]=0;
        }
    }


    for(int i=0;i<numberOfMatrix;i++){
        for(int j=0;j<numberOfMatrix;j++){
            if(i==j){
                dp[i][j]=0;
            }else if(j==i+1){
                dp[i][j]=p[i]*p[i+1]*p[i+2];
            }else{
                int best=5000;
                for(int k=i;k<j;k++){
                    printf("dp[%d][%d]=%d, dp[%d][%d]=%d, p[%d]=%d, p[%d]=%d, p[%d]=%d\n",i,k,dp[i][k],k+1,j,dp[k+1][j],i,p[i],k+1,p[k+1],j+1,p[j+1]);
                    best=min(best,dp[i][k]+dp[k+1][j]+p[i]*p[k+1]*p[j+1]);
                    cout<<"I: "<<i<<" J:  "<<j<<" Best: "<<best<<endl;
                }
                dp[i][j]=best;
            }
        }
    }

    for(int i=0;i<numberOfMatrix;i++){
        for(int j=0;j<numberOfMatrix;j++){
            cout<<dp[i][j]<<"   ";
        }
        cout<<endl;
    }

    return 0;
}

OUTPUT in output.txt file:

I: 0 J:  2 Best: 40
I: 0 J:  2 Best: 40
I: 0 J:  3 Best: 140
I: 0 J:  3 Best: 140
I: 0 J:  3 Best: 110
I: 1 J:  3 Best: 168
I: 1 J:  3 Best: 104
0   120   40   110   
5000   0   48   104   
5000   5000   0   84   
5000   5000   5000   0   
dp[0][0]=0, dp[1][2]=0, p[0]=5, p[1]=4, p[3]=2
dp[0][1]=120, dp[2][2]=0, p[0]=5, p[2]=6, p[3]=2
dp[0][0]=0, dp[1][3]=0, p[0]=5, p[1]=4, p[4]=7
dp[0][1]=120, dp[2][3]=0, p[0]=5, p[2]=6, p[4]=7
dp[0][2]=40, dp[3][3]=0, p[0]=5, p[3]=2, p[4]=7
dp[1][1]=0, dp[2][3]=0, p[1]=4, p[2]=6, p[4]=7
dp[1][2]=48, dp[3][3]=0, p[1]=4, p[3]=2, p[4]=7

Not expected output.
When i comment this code

#ifndef ONLINE_JUDGE 
        // For getting input from input.txt file 
        freopen("C:\\Users\\Rahul kumar\\desktop\\Algorithm\\input.txt", "r", stdin); 
        // Printing the Output to output.txt file 
        freopen("C:\\Users\\Rahul kumar\\desktop\\Algorithm\\output.txt", "w", stdout); 
    #endif 

and run the program then the output on the console is..

dp[0][0]=0, dp[1][2]=0, p[0]=5, p[1]=4, p[3]=2
I: 0 J:  2 Best: 40
dp[0][1]=120, dp[2][2]=0, p[0]=5, p[2]=6, p[3]=2
I: 0 J:  2 Best: 40
dp[0][0]=0, dp[1][3]=0, p[0]=5, p[1]=4, p[4]=7
I: 0 J:  3 Best: 140
dp[0][1]=120, dp[2][3]=0, p[0]=5, p[2]=6, p[4]=7
I: 0 J:  3 Best: 140
dp[0][2]=40, dp[3][3]=0, p[0]=5, p[3]=2, p[4]=7
I: 0 J:  3 Best: 110
dp[1][1]=0, dp[2][3]=0, p[1]=4, p[2]=6, p[4]=7
I: 1 J:  3 Best: 168
dp[1][2]=48, dp[3][3]=0, p[1]=4, p[3]=2, p[4]=7
I: 1 J:  3 Best: 104
0   120   40   110
5000   0   48   104
5000   5000   0   84
5000   5000   5000   0
I: 1 J:  3 Best: 104
0   120   40   110
5000   0   48   104
5000   5000   0   84
5000   5000   5000   0

This output is expected, but different form output of the file. This happens many times with me. I am running this program on vs code.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • what is the input? Is it exactly the same in both cases? – 463035818_is_not_an_ai May 15 '20 at 08:11
  • 2
    [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – 463035818_is_not_an_ai May 15 '20 at 08:11
  • @idclev463035818 yes, input is same in both cases. I am not giving input throught file or console. I already assigned input to variable inside program. –  May 15 '20 at 08:18
  • 1
    Mixing C and C++ output looks at least ugly but may have additional effects (I'm not sure about). It might be worth to investigate further into this direction... – Scheff's Cat May 15 '20 at 08:19
  • Do not mix C input/output with C++ input/output. – n. m. could be an AI May 15 '20 at 08:19
  • @idclev463035818I after removing `#include` things remains same. –  May 15 '20 at 08:21
  • 1
    FYI: [std::ios::sync_with_stdio](https://en.cppreference.com/w/cpp/io/ios_base/sync_with_stdio) – Scheff's Cat May 15 '20 at 08:22
  • @n.'pronouns'm. generally people mix it for faster `I/O` , but it should not cause any problem . –  May 15 '20 at 08:24
  • I'm rather sure that `#include` isn't responsible for your issue. Nevertheless, it makes your code unnecessarily non-portable and shouldn't be used. (Exposing it in this community will likely always attract a comment like the one of @idclev463035818.) ;-) – Scheff's Cat May 15 '20 at 08:24
  • 1
    _but it should not cause any problem_ This is exactly where I'm not sure about. ;-) If C++ and C output are not synchronized it may have exactly the effect you see. I remember similar effects when `std::cout` and `std::cerr` are redirected into the same file - the order of output might change. – Scheff's Cat May 15 '20 at 08:26
  • @Scheff you are right. Making synchronization between `cout` and `printf` solves my problem. –  May 15 '20 at 08:35
  • @Scheff `Exposing it in this community will likely always attract a comment like the one of @idclev463035818.` I don't use it for production, but it is ok when we just test/write some code/program. –  May 15 '20 at 08:36
  • The Q/As in SO are dedicated to be archived for future readers googling for a similar problem. If this is a question about C++ there is no need to expose non-standard C++ code except that's the concrete subject of the question. `#include` may work for `g++` but it's none of the headers which are officially part of the C++ `std` API. Hence, it's recommendable to use the latter instead. (What you use personally for production or non-production code is your own responsibility.) ;-) – Scheff's Cat May 15 '20 at 09:23
  • "for faster I/O". Premature optimisation is the root of all evil. – n. m. could be an AI May 15 '20 at 10:05
  • boy, I hope your not working for Boeing or Airbus. In any review this will be rejected at first sight. Completely unreadable. – rioV8 May 15 '20 at 10:53

1 Answers1

0

Include it in your program std::ios::sync_with_stdio(true); on the top of every statement. It also happened with me. Including this, in your program, cout and printf will become thread-safe. Make sure the argument must be true.

Rahul
  • 310
  • 2
  • 13