Help with arrays and pointers please...

This is a discussion on Help with arrays and pointers please... within the C++ Programming forums, part of the General Programming Boards category; Suppose you have a main() with three local arrays, all the same and type (say float). The first two are ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    6

    Help with arrays and pointers please...

    Suppose you have a main() with three local arrays, all the same and type (say float). The first two are already initialized to values. Write a function called “addarrays()” that accepts the address of the three arrays as arguments; adds the contents of the first two arrays together, element by element; and places the results in the third array before returning. A fourth argument to this function can carry the size of the arrays. Use pointer notation throughout; the only place you need brackets is in defining the arrays.

    • Plan the program first by writing the pseudocode
    • Create a pictorial of the pseudocode by constructing a flowchart
    • Do not use “C” syntax in the pseudocode
    • Add enough detail to the pseudocode to get clear picture as to what will take place

    If anybody can help, trying to pass this class but my teacher doesnt explain too well.. Thanks in advance.

  2. #2
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    If the first two arrays are equal in the number of elements they contain, then you can easily figure out how big to make the third, then just write two for loops to loop through each of the first two arrays and copy the elements into the third.
    Last edited by neandrake; 03-17-2005 at 01:33 PM.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    6
    can you post an example so I can see exactly, i'm new at c++ and just need to see the code. Thanks

  4. #4
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Here's some example code off the top of my head (no guarantees).
    sizeof(var)/sizeof(vartype) will give you the number of elements in the array (someone correct me if I'm wrong).

    Code:
    array_splice(float one[], float two[], float *three)
    {
    int size = 0;
    size += sizeof(one)/sizeof(float);
    size += sizeof(two)/sizeof(float);
    three = new float[size+1];
    
    for (int i=0; i< sizeof(one)/sizeof(float); i++)
    three[i] = one[i];
    
    for (int j=0; j<sizeof(two)/sizeof(float); j++)
    three[i+j] = two[j];
    }
    You should also post what you have come up with first.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    6
    thanks for the code, helped me through it...

  6. #6
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    So you completed your homework? Next time post what you've worked on so far. You might not be so lucky to get help without it. g/l with class
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by crazyeyesz28
    Suppose you have a main() with three local arrays, all the same and type (say float). The first two are already initialized to values. Write a function called “addarrays()” that accepts the address of the three arrays as arguments
    To get you started the prototype of such a function would be like this:
    Code:
    addarrays(float *array1ptr, float *array2ptr, float *array3ptr, unsigned int Size);
    adds the contents of the first two arrays together, element by element; and places the results in the third array before returning.
    something like this
    Code:
    while(size!=0)
    {
       *array3ptr= *array1ptr+*array2ptr;
       array1ptr++;
       array2ptr++;
       array3ptr++;
      Size--;
    }
    Last edited by Quantum1024; 03-17-2005 at 01:43 PM.

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    6
    sorry, i should've posted the code I had, wont happen again, just needed to compare. Thanks for the help

  9. #9
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    No problem. Welcome to the boards. I was just trying to give you a helpful hint when posting on the forums, people here can get really irritated by other people's homework ;-)
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 02:41 AM

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