# plzz help

• 11-21-2007
aj11
plzz help
int ages[10];
write a function called set_ages, that will take the ages array, an age, and a parameter of the size of the array and set all the elements in the array to be the same age.
such as to set all the ages to 21.

plz help me how to do this i don't understand if sum1 can explain it 2 me plz i have midterm coming up please help
• 11-21-2007
Happy_Reaper
It's funny, there isn't a question or even a question mark in that entire post. Haven't read the forum guidelines, now have you ?
• 11-21-2007
hk_mp5kpdw
Here's a hint, use a loop.
• 11-22-2007
swgh
Surely you were taught this anyway? Its quite a simple thing to do and one-dimensional arrays are quite easy to work with
• 11-22-2007
aj11
only if sum1 can show me
• 11-22-2007
anon
plz read forum guidelines & homewrok policy from teh top of teh page 1st
• 11-22-2007
MacGyver
Quote:

Originally Posted by aj11
only if sum1 can show me

I think that is partly what the professor is getting paid for.
• 11-22-2007
rodrigorules
well im new to c++ as well, and i thought this might be good practice for me,
i tried to do this and for some reason it only works halfway

i spent the time on it, and i would like to know what im doing wrong, if possible help me :o

here is my output (it is supposed to display TEN 21s)
Code:

` 21 21 -1 21 2009315348 4007008 21 8 2009312941 -1724333122`

the program the guy described in the first post says to pass an array to a function and set it full of 21s
Code:

```#include <iostream> using namespace std; void age_set(int* y,int age,int size) {     for(int x=0;x<size;x++)     {             y+=x;             (*y)=age;     } } int main() {     int ageset[10];     int asize = (sizeof(ageset))/4;  //divide by 4 since ints are held in packs of 4 bits     int* y;     y = &ageset[0]; //y pointer now holds the address of start of array in memory     age_set(y,21,asize);         for(int x=0;x<asize;x++)     {             cout << " "<<ageset[x];     } }```
• 11-22-2007
MacGyver
I would change:

Code:

`int asize = (sizeof(ageset))/4;  //divide by 4 since ints are held in packs of 4 bits`
To this:

[code]
Code:

`int asize = (sizeof(ageset))/sizeof(asize);`
That way you maintain portability and you don't have to know or rely on an int being 32-bits (although it generally will be nowadays).

Inside your function, you have the right idea except you're adding to y by way too much.

Pretend this is an array of size 5 that contain garbage:

Code:

`[1321], [2356], [9234], [2332423], [-7312]`
In your function, it's like this:

Code:

`y -> [1321], [2356], [9234], [2332423], [-7312]`
If you add one to y, since y is a pointer, it's taken to mean that you want to point to the next element, like this:

Code:

`[b][1321], y -> [2356], [9234], [2332423], [-7312]`
Now take a look at what you're adding to y in your function and see how this differs.
• 11-22-2007
rodrigorules
ah i didnt catch that, thanks
Code:

```    for(int x=0;x<size;x++)     {                         (*y)=age;             y+=1;     }```
• 11-25-2007
aj11
thanks guyz u rock