Ruturaj
Ruturaj

Reputation: 630

Similar program in java and c++ different output

I created a program to simulate a puzzle. Where n switches are connected in series(output of 1st switch is connected to input of second). 1st switch always has input power and initially all switches are in off state. For every iteration if switch has input power it switches state. if switch has input power and it's state is on it produces output. there is light bulb at end and we have to find if it glows given number of iterations. I did this mathematically, this time I am trying to stimulate.

Consider case of 2 switches

power    Switch1  power   Switch2  power   Bulb
ON         OFF     OFF     OFF     OFF     OFF
ON         ON      ON      OFF     OFF     OFF
ON         OFF     OFF     ON      OFF     OFF
ON         ON      ON      ON      ON      ON 

NOTE:In third iteration state of switch2 changed since it had power input.

My code gives right output in java but wrong in c++ here's the c++ code

#include<iostream>

using namespace std;

int main(){
        int n=2;
        int taps=4;
        bool ans=false;
        bool state[n][taps];//={0};//=new boolean[n][taps];
        bool out[n][taps];//={0};//=new boolean[n][taps];
        bool power=true;
        for(int j=0;j<taps;j++){
            for(int i=0;i<n;i++){
                state[i][j]=false;
                out[i][j]=false;
            }
        }

        for(int j=0;j<taps;j++){
            for(int i=0;i<n-1;i++){
                if(j>0)
                    state[0][j] = !state[0][j-1];

                out[0][j]=(power && state[0][j]);

                if(j>0){    
                    if (out[i][j]==true){
                        if (out[i][j-1]==false)
                            state[i+1][j]=state[i+1][j-1];
                        else
                            state[i+1][j]= !state[i+1][j-1];
                    }else if(out[i][j]==false){
                        if(out[i][j-1]==true)
                            state[i+1][j]= !state[i+1][j-1];
                        else
                            state[i+1][j] = state[i+1][j-1];
                    }   
                }else
                    state[i+1][j]=false;
                out[i+1][j]= (state[i+1][j] && out[i][j]);
                ans=out[i+1][j];
            }
            cout<<ans<<endl;    
        }
        if(ans == true)
            cout<<"ON"<<endl;
        else
            cout<<"OFF"<<endl;

}

And this is java code

public class turn {
    public static void main(String args[]){
        int n=2;
        int taps=4;
        boolean ans=false;
        boolean state[][]=new boolean[n][taps];
        boolean out[][]=new boolean[n][taps];
        boolean power=true;
        for(int j=0;j<taps;j++){
            for(int i=0;i<n-1;i++){
                if(j>0)
                    state[0][j] = !state[0][j-1];

                out[0][j]=power & state[0][j];

                if(j>0){    
                    if (out[i][j]==true){
                        if (out[i][j-1]==false)
                            state[i+1][j]=state[i+1][j-1];
                        else
                            state[i+1][j]= !state[i+1][j-1];
                    }else if(out[i][j]==false){
                        if(out[i][j-1]==true)
                            state[i+1][j]= !state[i+1][j-1];
                        else
                            state[i+1][j] = state[i+1][j-1];
                    }   
                }else
                    state[i+1][j]=false;
                out[i+1][j]=state[i+1][j] & out[i][j];
                ans=out[i+1][j];
            }
            System.out.println(ans);

        }
        if(ans == true)
            System.out.println("ON");
        else
            System.out.println("OFF");
    }
}

Upvotes: 0

Views: 122

Answers (1)

TTT
TTT

Reputation: 2012

This could be one of them

out[0][j]=(power && state[0][j]);

&& is a logical comparison, so this code returns true if power and state[0][j] are true. You probably want to use the bitwise operator & here

And here:

out[i+1][j]= (state[i+1][j] && out[i][j]);

Upvotes: 1

Related Questions