By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,920 Members | 1,226 Online
Bytes IT Community
+ Ask a Question
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
  1. #include<iostream.h>
  2.  
  3.  
  4. class Count
  5. {
  6.  
  7. int a[100],b[100],n;
  8.  
  9. public:
  10. void getdata();
  11. void cal();
  12. void show();
  13.  
  14. };
  15.  
  16. void Count::getdata()
  17. {
  18. int i;
  19. cout<<"How many numbers you want to enter"<<endl;
  20. cin>>n;
  21. cout<<"Enter the numbers"<<endl;
  22. for(i=1;i<=n;i++)
  23. cin>>a[i];
  24.  
  25.  
  26. }
  27.  
  28.  
  29. void Count::cal()
  30. {
  31. int i,j;
  32. for(i=1;i<=100;i++)
  33. b[i]=0;
  34. for(i=1; i<=n; i++)
  35. {
  36. j=a[i];
  37. b[j]++;
  38. }
  39. }
  40.  
  41.  
  42. void Count::show()
  43. {
  44. int i;
  45. for(i=1; i<=n; i++)
  46. {
  47. if(b[i]!=0)
  48. cout<<"The number "<<i<<"is present "<<b[i]<<"times"<<endl;
  49. }
  50. }
  51. int main()
  52. {
  53. Count c;
  54. c.getdata();
  55. c.cal();
  56. c.show();
  57. return 0;
  58. }
Now my question is the value of n is getting changed, when i will initialise the elements of array b to 0(zero) (See the cal function)... "Why the value is getting changed..?"...

if you try to print the value of n before the for loop then it will give you the correct ans but after the for loop the value of n is getting changed, where i am not at all altering the value of n...


Plz if you have any ieda then let me know... I want the reason not output of the program i mean to say dont alter the code because as per my knowledge the code is correct.. Just tell me what is wrong with this code..

Thanks in advance
Manjiri
Dec 28 '06 #1
Share this Question
Share on Google+
5 Replies


Expert 100+
P: 1,510
you define the arrays
Expand|Select|Wrap|Line Numbers
  1. int a[100],b[100],n;
  2.  
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
  1. void Count::cal()
  2. {
  3. int i,j;
  4. for(i=1;i<=100;i++)
  5. b[i]=0;
  6.  
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
  1. void Count::cal()
  2. {
  3. int i,j;
  4. for(i=0;i<100;i++)
  5. b[i]=0;
  6.  
also check your other for loops
Dec 28 '06 #2

P: 40
you define the arrays
Expand|Select|Wrap|Line Numbers
  1. int a[100],b[100],n;
  2.  
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
  1. void Count::cal()
  2. {
  3. int i,j;
  4. for(i=1;i<=100;i++)
  5. b[i]=0;
  6.  
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
  1. void Count::cal()
  2. {
  3. int i,j;
  4. for(i=0;i<100;i++)
  5. b[i]=0;
  6.  
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
  1. int a[100],b[100],n;
  2.  
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
  1. void Count::cal()
  2. {
  3. int i,j;
  4. for(i=1;i<=100;i++)
  5. b[i]=0;
  6.  
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
  1. void Count::cal()
  2. {
  3. int i,j;
  4. for(i=0;i<100;i++)
  5. b[i]=0;
  6.  
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
  1. int a[100],b[100],n;
  2.  
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
  1. int a[100],b[100],n;
  2.  
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

Post your reply

Sign in to post your reply or Sign up for a free account.