Trouble with array...

Sep 13, 2013 at 5:28pm
I have set of 5 integers I want to be declare as array elements, and I want each following element in the array to preform arithmetic, say simply subtract 20 from each of them, while the first is set to 100.
So the 5th and final element would be set to 20..
Last edited on Sep 13, 2013 at 5:28pm
Sep 13, 2013 at 5:41pm
I have set of 5 integers I want to be declare as array elements
int arr[]={100,80,60,40,20}
Sep 13, 2013 at 5:43pm
Here's another option -> http://ideone.com/QTKmp5
Sep 13, 2013 at 5:45pm
use a for loopfor(int x=0;x<5;x++)
Sep 13, 2013 at 5:52pm
@m4ster r0shi nice lol ;)
Sep 13, 2013 at 5:56pm
I know. Thanks.
Sep 13, 2013 at 5:59pm
I think xplainet's head is boiling lol
Sep 13, 2013 at 6:07pm
1
2
3
4
5
6
7
8
9
 
    int j[5];
    int n, i;
    i = 0;
    for (n = 0; n < 5; ++n) {
        j[n] = 100 - i;
        cout << j[n] << " ";
        i = i + 20;
        }


Maybe that could help you, too.
Sep 14, 2013 at 9:28pm
@chriscpp I was actually referring to a way to make each elements in the array more variable.
say I have an alternating variable that would change each value of the array.
So if a variables value is 2, each array will divide the previous by 2. If it changes to 3, each element in the array would divide the previous by 3.
Or add, multiply whatever.
The idea is to set the array elements to certain possible X values for the f(x)


Also: is there some way to collect each value of the array and set the sum of each to another variable?
Last edited on Sep 14, 2013 at 9:30pm
Sep 14, 2013 at 11:52pm
is there some way to collect each value of the array and set the sum of each to another variable?

Sure -> http://ideone.com/FB31nC (if I correctly understood what you want...)

Useful link -> http://www.cplusplus.com/reference/numeric/accumulate/
Last edited on Sep 14, 2013 at 11:53pm
Topic archived. No new replies allowed.