Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to increment array elements at a time by 1...i.e
i have an array as array[1,2,3,4,5], is there any built in function(either in java or C) to increment or decrement
by 1 at a time which becomes as array[2,3,4,5,6].
Posted
Updated 14-Sep-13 19:36pm
v2
Comments
Shubhashish_Mandal 15-Sep-13 3:21am    
No.
vjnan 15-Sep-13 8:37am    
ok...thanks for reply

There is not such a 'built-in' function (at least in C, I believe Java has not such a function too) possibly because, as ridoy correctly pointed out, you may easily implement it yourself.
 
Share this answer
 
Comments
ridoy 15-Sep-13 16:45pm    
my 5 for mentioning some real good points here and in my answer,:).
I don't know why you are searching for a built in function to do this as only 2 lines of code will get the desired code.Look how it could be done:
C#
int[] myArray = {1,2,3,4,5};
int temp=0;

for(int i=0; i<myArray.length; i++)
{
    temp = myArray[i]+1;
    myArray[i] = temp;
}
//your array value was changed above,now see exactly it works or not!
for(int k=0; k < myArray.length; k++)
System.out.println(myArray[k]);

C#
Output:
2,3,4,5,6

Hope that will help you.

Cheers!
Shuvro
 
Share this answer
 
v4
Comments
CPallini 15-Sep-13 15:17pm    
That's not standard C (not even standard C++).
By the way you might remove temp and just use ++myArray[i];
ridoy 15-Sep-13 15:29pm    
I give it as java standard not for c,because i think Op isn't concerned about language to do it as he tagged C and Java both.But basic idea isn't different whatever the language is,isn't it?And yes your next point is absolutely right,:)
CPallini 15-Sep-13 16:05pm    
Right, my 5 (strangely enough, my bad, I supposed your code was C++ with Microsoft extensions).
ridoy 15-Sep-13 16:45pm    
Thank you,:).
In C++ you can use the class std::valarray. It is similar to std::vector, but defines a number of standard operators that modify each element individually. check out http://www.cplusplus.com/reference/valarray/valarray/operators/[^] as a reference; it also includes some example code.

For incrementing an array you could write e. g.:
C++
#include <valarray>     // std::valarray

void foo ()
{
  int myarray[]= {10,20,30,40};

  std::valarray<int> bar (myarray, 4);  //  10 20 30 40

  bar += 1; // 11 21 31 41
}


P.S.:
A word of warning if you use this class in Windows: some standard windows header file #defines the macros min() and max(), which will nuke the functions std::valarray::min() and std::valarray::max() if included before you include valarray. You can prevent this in various manners:
1. use #undef to kill these definitions
2. use #define NOMINMAX before including the windows headers to prevent their definition
3. #include valarray first

(Just one of the many reasons why I hate #define !)
 
Share this answer
 
v3
Comments
nv3 16-Sep-13 5:27am    
That's a nice one, Stefan!
You will need to loop (a for loop[^] will be suitable for that) to get this done. Therefore you can't do that in that way.

Probably you are more interested in a linked list[^], where you will be able to add or remove nodes easily.

Hope this helps. :thumbsup:
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900