473,507 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why the value is getting changed...?

40 New Member
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
5 1714
horace1
1,510 Recognized Expert Top Contributor
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
Manjiri
40 New Member
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
Manjiri
40 New Member
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
horace1
1,510 Recognized Expert Top Contributor
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
Manjiri
40 New Member
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

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

Similar topics

21
9821
by: | last post by:
Hi, I am setting the NumericUpDown .Value property and the ValueChanged event is NOT being fired. Does this ONLY get fired when I change it on the UI and not programatically? Thanks
8
3063
by: DaKoadMunky | last post by:
Please consider the following... <CODE> #include <string> using namespace std; typedef int PrimitiveType; typedef string ClassType;
6
1882
by: Tim Roberts | last post by:
I've been doing COM a long time, but I've just come across a behavior with late binding that surprises me. VB and VBS are not my normal milieux, so I'm hoping someone can point me to a document...
6
4318
by: Daz | last post by:
Hi everyone. Firstly, I apologise if this i not what you would call a PHP problem. I get quite confused as to what lives in which realm, so if this shouldn't be posted here, please suggest where...
7
3229
by: turtle | last post by:
I want to find out the max value of a field on a report if the field is not hidden. I have formatting on the report and if the field doesn't meet a certain criteria then it is hidden. I want to...
7
16232
by: pooba53 | last post by:
I am working with VB .NET 2003. Let's say my main form is called Form1. I have to launch a new form (Form2) that gathers input from the user. How can I pass variable information back to Form1...
0
990
by: rawatgaurav81 | last post by:
I had this strange problem in handling XMLDocuments in asp.net.Though the problem occurs while working on the main application.I will try to explain it with shorter code. I had a form with 2...
275
12037
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
6
3587
by: FrankF | last post by:
Two years ago, member acoder gave patient help to a newbie whose function worked in IE but not in Firefox. I have a similar problem, but mine works fine in Firefox and exhibits astonishing behavior...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7114
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1544
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.