454,920 Members | 1,226 Online
Need help? Post your question and get tips & solutions from a community of 454,920 IT Pros & Developers. It's quick & easy.

# Why the value is getting changed...?

 P: 40 Hello Everybody... Here i have the program which prints how many number of times the element appears in the array.... The code is as follows... Expand|Select|Wrap|Line Numbers #include     class Count {   int a[100],b[100],n;   public: void getdata(); void cal(); void show();   };   void Count::getdata() { int i; cout<<"How many numbers you want to enter"<>n; cout<<"Enter the numbers"<>a[i];     }     void Count::cal() { int i,j; for(i=1;i<=100;i++) b[i]=0; for(i=1; i<=n; i++) { j=a[i]; b[j]++; } }     void Count::show() { int i; for(i=1; i<=n; i++) { if(b[i]!=0) cout<<"The number "<
5 Replies

 Expert 100+ P: 1,510 you define the arrays Expand|Select|Wrap|Line Numbers int a[100],b[100],n;   where b has elements b[1] thru b[99] - remember C/C++ starts array indexes at 0 not 1 in cal() you have a loop Expand|Select|Wrap|Line Numbers void Count::cal() { int i,j; for(i=1;i<=100;i++) b[i]=0;   which will assign 0 to elements b[1] to b[100] (loop terminates when i = 101) corrupting whatever follows b in memory. change your loop to Expand|Select|Wrap|Line Numbers void Count::cal() { int i,j; for(i=0;i<100;i++) b[i]=0;   also check your other for loops Dec 28 '06 #2

 P: 40 you define the arrays Expand|Select|Wrap|Line Numbers int a[100],b[100],n;   where b has elements b[1] thru b[99] - remember C/C++ starts array indexes at 0 not 1 in cal() you have a loop Expand|Select|Wrap|Line Numbers void Count::cal() { int i,j; for(i=1;i<=100;i++) b[i]=0;   which will assign 0 to elements b[1] to b[100] (loop terminates when i = 101) corrupting whatever follows b in memory. change your loop to Expand|Select|Wrap|Line Numbers void Count::cal() { int i,j; for(i=0;i<100;i++) b[i]=0;   also check your other for loops Hello friend Thank you very much.... it worked but i am not yet cleared with the thing.. What happens if indexes start with 0 also... why the value n gets changed..? Plz clear this also... Dec 28 '06 #3

 P: 40 you define the arrays Expand|Select|Wrap|Line Numbers int a[100],b[100],n;   where b has elements b[1] thru b[99] - remember C/C++ starts array indexes at 0 not 1 in cal() you have a loop Expand|Select|Wrap|Line Numbers void Count::cal() { int i,j; for(i=1;i<=100;i++) b[i]=0;   which will assign 0 to elements b[1] to b[100] (loop terminates when i = 101) corrupting whatever follows b in memory. change your loop to Expand|Select|Wrap|Line Numbers void Count::cal() { int i,j; for(i=0;i<100;i++) b[i]=0;   also check your other for loops Hey friend if i execute the same program in c(with index initialised from 1) it is getting executed then how come it is possible...? Dec 28 '06 #4

 Expert 100+ P: 1,510 Hey friend if i execute the same program in c(with index initialised from 1) it is getting executed then how come it is possible...? what happened was that when you initialised b[] to 0 you overran the end of the array and what ever was in memory after it was zeroed - in this case it was n Expand|Select|Wrap|Line Numbers int a[100],b[100],n;   However, the way that information is stored in memory depends upon the compiler, linker, various options, etc. , i.e. with a different compiler n may not immediatly dollow b[] in memory and the program may well appear to work OK. In such a case the error often shows up later when input data is changed or the code is ported to a different system. Dec 28 '06 #5

 P: 40 what happened was that when you initialised b[] to 0 you overran the end of the array and what ever was in memory after it was zeroed - in this case it was n Expand|Select|Wrap|Line Numbers int a[100],b[100],n;   However, the way that information is stored in memory depends upon the compiler, linker, various options, etc. , i.e. with a different compiler n may not immediatly dollow b[] in memory and the program may well appear to work OK. In such a case the error often shows up later when input data is changed or the code is ported to a different system. Ok............... Now i got know.. Thanks friend... I appreciate your talent... Manjiri Dec 29 '06 #6