Dec 27, 2017 at 7:42pm UTC
THE FOLLOWING CODE IS TO CREATE A 2D ARRAY CONTAINING NO.S 1-9. AND THEN taking A number FROM the user AND REPLACING THAT PARTICULAR number in the ARRAY with the number 11.?
#include<conio.h>
#include<iostream.h>
int i,j,n,c=0,a[3][3]={1,2,3,4,5,6,7,8,9} ;
void main()
{
for(i=0;i<3;i++)
{for(j=0;j<3;j++)
cout<<a[i][j];}
cin>>n;
for(i=0;i<3;i++)
{for(j=0;j<3;j++)
if(a[i][j]==n);
{cout<<"gotcha"<<i<<" "<<j<<endl;a[i][j]=11;break;
}
} for(i=0;i<3;i++)
{for(j=0;j<3;j++)
cout<<a[i][j];}
cout<<endl; getch();
}
THE OUTPUT IS:
123456789 3
gotcha 0 3
1231156789
123456789 5
gotcha 0 3
1231156789
123456789 6
gotcha 0 3
1231156789
I'VE RUN IT MULTIPLE TIMES FOR DIFF VALUES OF N ENTERED
Last edited on Dec 27, 2017 at 8:02pm UTC
Dec 27, 2017 at 7:59pm UTC
if(a[i][j]==n); <---------- if true, do ; (nothing at all)
this statement after the if executes whether true or false above due to ; bug
because of the bug, the code in the loop isnt in the loop and it inherits an off by 1 bug as the loop variables have incremented as well.
Dec 28, 2017 at 5:05am UTC
OOOOOHHHHHHHHHH!!!! DAMN !!
sorry stupid mistake
Last edited on Dec 28, 2017 at 5:09am UTC