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... -
#include<iostream.h>
-
-
-
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"<<endl;
-
cin>>n;
-
cout<<"Enter the numbers"<<endl;
-
for(i=1;i<=n;i++)
-
cin>>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 "<<i<<"is present "<<b[i]<<"times"<<endl;
-
}
-
}
-
int main()
-
{
-
Count c;
-
c.getdata();
-
c.cal();
-
c.show();
-
return 0;
-
}
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
5 1714 horace1 1,510
Recognized Expert Top Contributor
you define the arrays
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 -
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 -
void Count::cal()
-
{
-
int i,j;
-
for(i=0;i<100;i++)
-
b[i]=0;
-
also check your other for loops
you define the arrays
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 -
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 -
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...
you define the arrays
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 -
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 -
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...?
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
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.
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
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
by: DaKoadMunky |
last post by:
Please consider the following...
<CODE>
#include <string>
using namespace std;
typedef int PrimitiveType;
typedef string ClassType;
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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'
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |