# Thread: Help with arrays and pointers please...

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

3. can you post an example so I can see exactly, i'm new at c++ and just need to see the code. Thanks

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

5. thanks for the code, helped me through it...

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

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

8. sorry, i should've posted the code I had, wont happen again, just needed to compare. Thanks for the help

9. 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 ;-)

Popular pages Recent additions