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...
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
Here i have the program which prints how many number of times the element appears in the array....
The code is as follows...
Code:
#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; }
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
Comment