#include <iostream.h>
#include <conio.h>
#include <stdio.h>
int main()
{
clrscr();
int sales[3][6];
int i,j,total;
for(i=1;i<=3;++i)
{ total=0;
cout<<"enter sales for "<<i<<": \n";
for(j=1;j<=6;j++)
{ cout<<" Month "<<j<<":";
cin>>sales[i][j];
total = total+sales[i][j];
}
cout<<"\n Total sales of salesman"<<i<<"="<<total<<"\n";
}
getch();
return 0;
}
The result is observed as:
Enter sales for 1:
Month 1:1
Month 2:1
Month 3:1
Month 4:1
Month 5:1
Month 6:1
Total sales of salesman1=6
enter sales for 2:
Month 1:1
Month 2:1
Month 3:1
Month 4:1
Month 5:1
Month 6:1
total sales of salesman2=2
enter sales for 3:
Month 1:1
Month 2:1
Month 3:1
Month 4:1
Month 5:1
Month 6:1
total sales of salesman3=6.
i know i have entered wrong array indexes and wrong inequality...Please check the total sales of salesman2 and the total sales of salesman 1 and salesman3.We find that the total is correct for 1 and 3 but not for salesman 2.I want to know why is that so? what's the cause behind it.Can you please help me. I have used turboc++ compiler.
You're reading from and writing to memory locations that are outside that matrix. This causes undefined behavior. When the behavior of a program is undefined, literally anything can happen. The program may work correctly, it may compute incorrect results, it may crash, it may try to delete all your files, the computer may crash, etc. You just don't know. That's why you must always be careful to index arrays correctly.
Thank you very much sir, for your answer. I have understood that. But i am curious to know more deeper.Why is it only 2nd and why not 3.You see 3 is outside the matrix but sir still we get correct sollution.Why is that so? Can you please tell me
the arrays are mapped down to a 1-d block of memory. It is possible to put the wrong values in and get the correct answer; it can happen for example at (0,0) of course, and on a square array any (x,x) repeated value will work if reversed. You can also luck into the same value, if (3,2) and (2,3) happen to both be the same number. Honestly, this is the WORST thing that can happen, is getting the correct answer from the wrong code. It happens sometimes, and its one way bugs can sneak into code after testing.
Thank you sir but when i <=4 in the same program with Array sale[3][6] we get correct answer for salesman 1,3 and 4 leaving 2 again. You see..that the total only goes wrong for 2 and getting correct output for rest including outer matrices...
Like I said, the behavior is undefined. If you want to know at the immediate reason for why the program does what it does, you'll have to look at the disassembly. It's not possible to predict what a program with undefined behavior will do just by looking at the C++ code.
Although all good points I think they have missed the small point. Arrays start at an index of zero yet the for loops start at one. Which would make it very easy to exceed the top bound of the array.
Otherwise the program ran fine for me. I could not duplicate your problem yet.
Just to emphasize how deceitful could be looking for predictable results from undefined behaviour, I’ve modified Mike’s code to keep track of what happens at run time to the variable involved.
The result is totally machine-dependent and the output of the Windows machine is the one which can, perhaps, shed some light on the strange behaviour Mike pointed out.
Modified code: