Why the value is getting changed...?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Manjiri
    New Member
    • Nov 2006
    • 40

    Why the value is getting changed...?

    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...

    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;
    }
    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
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    you define the arrays
    Code:
    int a[100],b[100],n;
    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
    Code:
    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
    Code:
    void Count::cal()
    {
    int i,j;
    for(i=0;i<100;i++)
    b[i]=0;
    also check your other for loops

    Comment

    • Manjiri
      New Member
      • Nov 2006
      • 40

      #3
      Originally posted by horace1
      you define the arrays
      Code:
      int a[100],b[100],n;
      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
      Code:
      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
      Code:
      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...

      Comment

      • Manjiri
        New Member
        • Nov 2006
        • 40

        #4
        Originally posted by horace1
        you define the arrays
        Code:
        int a[100],b[100],n;
        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
        Code:
        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
        Code:
        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...?

        Comment

        • horace1
          Recognized Expert Top Contributor
          • Nov 2006
          • 1510

          #5
          Originally posted by Manjiri
          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
          Code:
          int a[100],b[100],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.

          Comment

          • Manjiri
            New Member
            • Nov 2006
            • 40

            #6
            Originally posted by horace1
            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
            Code:
            int a[100],b[100],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

            Comment

            Working...