473,665 Members | 2,774 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 1724
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
9883
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
3072
by: DaKoadMunky | last post by:
Please consider the following... <CODE> #include <string> using namespace std; typedef int PrimitiveType; typedef string ClassType;
6
1888
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 that describes this. Here's the setup. We have a COM server, written in Python. For completeness, here is the script: ----- testserver.py ----- import pythoncom
6
4342
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 I should post it. I have created a form, which consists of a list of items, each with a checkbox. When a checkbox is checked or unchecked, the page should be refreshed. During the refresh, the data is validated and the MySQL database is...
7
3235
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 get a max of the field for the ones that are not hidden. is this possible? TIA, KO
7
16267
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 before calling the Me.close() on Form2? -Stumped
0
999
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 buttons.The first button loads a file and second button make some changed in extracted XML node.The strange thing is that I am not touching the XMLdocument created in second button but still its getting changed.It will be more clear by seeing the examples....
275
12248
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
3590
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 in IE. function chooseList(x) { alert(document.getElementById('myHiddenField').value); document.getElementById('myHiddenField').value=x; alert(document.getElementById('myHiddenField').value); ...
0
8438
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8863
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8779
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7376
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5660
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1761
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.