I'm new and need lots of help

This is a discussion on I'm new and need lots of help within the C++ Programming forums, part of the General Programming Boards category; Alright I'm fairly new to C++ and I'm getting into some stuff that I can't quiet figure. One thing is ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    2

    I'm new and need lots of help

    Alright I'm fairly new to C++ and I'm getting into some stuff that I can't quiet figure. One thing is I need to add a number to each of the 15 elements of an integer array? Please help, thanks

  2. #2
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    :sniff:

    Smells like home work, show some effort there buddy.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    2
    no it's really not homework, I'm just working on it on my own. It's not for a school thing.

  4. #4
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Read about loops.

    Loop through each element in the array.

    Add the number to each element in the array.

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    What are you having trouble with?

    Here's what you need to know...

    How to create an array
    How to access an element of an array
    How to "add a number" to a value/variable
    How to make a loop (to do stuff over & over)

    There are 3 ways to make a loop. A for-loop, a while-loop, and a do-while-loop.

    [EDIT] -
    Check-out the cprogramming.com Loop Tutorial and the Array Tutorial. And when you have a chance, read the Board Guidelines. It will help prevent (well maybe minimize) the negative responses. BTW - It is OK to ask for help with your homework as long as you don't ask that it be done for you.
    Last edited by DougDbug; 03-16-2004 at 02:25 PM.

  6. #6
    Registered User heat511's Avatar
    Join Date
    Dec 2001
    Posts
    169
    ok, ill put it in pseudo (god i love that word) code for you

    Code:
    integer element[15]
    while(accumutator <= numberofelements)
    {
    element[accumulator] = element[accumulator] + numbertoadd
    accumular = accumulator + 1  //to make it accumulate
    }
    that should help you understand the concept (assuming that's what you're struggling with)
    "uh uh uh, you didn't say the magic word"
    -Jurassic Park

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Here is a great way for newbs, easy to understand!:
    Code:
    #include <algorithm>
    #include <iostream>
    #include <iterator>
    using namespace std;
    
    template <class T>
    class AddNum
    {
        T value;
    public:
        AddNum(const T& x): value(x)
        {
        }
        operator() (T& elem) const
        {
            elem += value;
        }
    };
    
    int main()
    {
        int array[5] = { 5, 8, 9, 10, 12 };
        
        // Output contents of original array to screen
    
        copy(array,array+5,ostream_iterator<int>(cout," "));
        cout << endl;
    
        // Add the value 6 to all elements
    
        for_each(array,array+5,AddNum<int>(6));
    
        // Output contents of modified array to screen
    
        copy(array,array+5,ostream_iterator<int>(cout," "));
        cout << endl;
    
        return 0;
    }
    Outputs:
    Code:
    5 8 9 10 12
    11 14 15 16 18
    Just kidding about it being easy for newbs. Seriously, like others said just loop through and reassign to each element of the array the original value plus whatever value you wanted to add to it.
    "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

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    Code:
    #include <iostream>
    #include <numeric>
    
    using namespace std;
    
    int main()
    {
        int myInts[15] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
        int sum = accumulate(myInts, myInts+15, 0);
        cout << "Total = " << sum << endl;
    }
    Gambling. The new yoga, with cash prizes!

Popular pages Recent additions subscribe to a feed

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