array problem

This is a discussion on array problem within the C++ Programming forums, part of the General Programming Boards category; this program uses read() function to input values and print out arrays with print() function however, i cannot get the ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Location
    192.168.1.1
    Posts
    4

    array problem

    this program uses read() function to input values and print out arrays with print() function
    however, i cannot get the arrays printed correctly...

    Code:
    void read(int[], int);
    void print(int[], int);
    int main()
    { const int MAXSIZE=100;
    int a[MAXSIZE]={0}, size;
    read(a,size);
    cout<<"The array has "<<size<<" elements: ";
    print(a,size);
    }
    
    void read(int a[], int n)
    {cout<< "Enter integers. Terminate with 0: \n";
    n=0;
    const int MAXSIZE=100;
    do
    {cout<< "a["<< n << "] : ";
    cin>>a[n];
    }while (a[n++] != 0 && n<MAXSIZE);
    --n;
    }
    void print(int a[], int n)
    {for (int i=0; i<n; i++)
    cout<< a[i]<< " ";
    }
    it always comes out as 0 array and no values being inserted.
    Thanks for the help.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,751
    The 2nd parameter of read() needs to be a reference parameter, for changes in read() to be visible to the size variable declared in main()

  3. #3
    Registered User
    Join Date
    Dec 2005
    Location
    192.168.1.1
    Posts
    4
    can some one explain why reference parameter is needed in this program?
    thnx

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,751
    Unless you say otherwise, size (in your code) will be passed by value.
    This means your function gets a copy of size, not size itself.
    Your function then modifies this copy (not the original), so by the time you get back to main, size is left as it was (uninitialised).

    You could also write the code like this
    size = read ( a );
    Where read is coded to return an int, and you finish the function with
    return n;

  5. #5
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Note that the reason your "a" parameter seems to be modified without passing it by reference is that it is a pointer variable. This means that the function is making a copy of the pointer contents you pass for "a" and, since the contents of said copy happen to be the address of in memory at which the array data begins, you are, in fact, able to modify the array data without modifying the pointer variable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 12:35 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 03:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21