Thread: plzz help

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    3

    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
    Last edited by aj11; 11-21-2007 at 05:11 PM.

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    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 ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Here's a hint, use a loop.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Surely you were taught this anyway? Its quite a simple thing to do and one-dimensional arrays are quite easy to work with
    Double Helix STL

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    3
    only if sum1 can show me

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    plz read forum guidelines & homewrok policy from teh top of teh page 1st
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by aj11 View Post
    only if sum1 can show me
    I think that is partly what the professor is getting paid for.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    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

    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];
        }
    }
    Last edited by rodrigorules; 11-22-2007 at 08:35 PM.

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    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.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    ah i didnt catch that, thanks
    Code:
         for(int x=0;x<size;x++)
         {
                 
                 (*y)=age;
                 y+=1;
         }

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    3
    thanks guyz u rock

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help me out plzz!!
    By salmanriaz in forum Game Programming
    Replies: 11
    Last Post: 03-20-2009, 09:56 AM
  2. beginner plzz help urgent
    By sara101 in forum C Programming
    Replies: 11
    Last Post: 01-14-2007, 10:38 PM
  3. plzz suggest me some good online VC++ tuorials, i am a beginner
    By annamayya in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 11-18-2004, 05:56 AM
  4. Plzz help problem with strings
    By UltimoFX in forum C Programming
    Replies: 5
    Last Post: 04-14-2003, 08:06 PM
  5. need help , plzz
    By kfatima in forum C Programming
    Replies: 1
    Last Post: 10-11-2002, 09:19 PM