increment the data stored in an array

I have to increment the data stored in an array.

like int a;
a++;

1
2
unsigned int  arra_11[5] = {0};


How is it possible?
Last edited on
You increment it the same way as you would any other variable. You use the ++ operator with the array element you want to increment.
Last edited on
Hi amitk3553

You want to increment the value of one specific index?

for example arra_11[0]? if so, you can do it in the same way you stated using ++

1
2
3
4
5
6
unsigned int  arra_11[5] = {0};
    do
    {
        arra_11[0]++;
    }while(arra_11[0] < 10);
    printf("%i",arra_11[0]);


This will increment the index 0 until is 10
like there is some huge data, which i had stored in array, i had to increment the whole array as i had to increment the whole data, so no doubt if I increment the arra_11[0] (LSB) then whole data or whole array would be incremented, but if incrementation goes out the range of first location of arra_11 , then if we increment further, the carry would be added to 2nd location?? or at that time we need to increment 2nd location individually??


then i can increment arra_11++??
No. If you increment one element of the array, then that is incremented in isolation - there's no automatic behaviour where it will then start to increment other elements too. That's specialised behaviour, that you'd have to implement yourself.
Last edited on
Good old for each
for ( unsigned int & x : arra_11 ) ++x;

std::valarray had "for all" compound assignment operators prewritten, but that never did catch on.


@Jecs9:
Your loop ...
1
2
3
4
5
6
7
8
9
10
unsigned int  arra_11[5] = {0}; // asserts that arra_11[3] == 0

do {
  ++arra_11[3];
} while( arra_11[3] < 10 );
// OR
arra_11[3] = 10;
// OR
arra_11[3] += 1;
if ( arra_11[3] < 10 ) arra_11[3] = 10;
Topic archived. No new replies allowed.