0

i want to add an integer in the all indexes of an array .without using loop .in single ststement.

int a[4]={4,4,4,4}; i want to add 1 in all indexes.. so the out put is cout<

output:_ 5 5 5 5

i relly thnks to all..

3
  • 5
    "without using loop .in single ststement" - sounds like an artificial requirement. What exactly you are trying to do? Commented Dec 26, 2010 at 17:28
  • 1
    Why do you want to do this? This sounds like homework... Commented Dec 26, 2010 at 17:29
  • I did it without using loops in one line see my answer below Commented Dec 28, 2010 at 15:32

6 Answers 6

8
#include <functional>
#include <algorithm>

int main()
{
    int a[] = {1, 2, 3, 4};

    std::transform(a, a + sizeof(a) / sizeof(a[0]), a, std::bind1st(std::plus<int>(), 1));
}
Sign up to request clarification or add additional context in comments.

6 Comments

This is such a roundabout way of obfuscating the loop. +1!
OP's question aside, when would one ever, ever, ever find this useful in practice over a loop? :P
@marcog, well, true, in C++ it's better to use loops for trivial tasks :) in other languages though this is natural (i.e. in F# this would be a |> Array.map ((+) 1)).
@ marcog: I find it rare that I ever actually write loops to do this kind of thing. One of the std::algorithms is nearly always my choice. As they are correct concise and tend to be highly optimizeable by the compiler.
@marcog: There is a direct relation between how much you use a feature and how obscure it looks. I bet that in a couple of years, with lambdas added to the language, you will find this type of solution common, if expressed differently: std::transform( a, a+4, a, []( int x ) { return x+1; } );
|
2

Yet another possibility would be to use an std::valarray:

int aa[] = {4, 4, 4, 4};

std::valarray<int> a(aa, 4);

a += 1;

I can't say I'd really recommend this, but it does work.

13 Comments

Why would you give an answer you don't recommend?
@Fred Nurk: Because I think it's as good an answer as any to a silly question.
Why should silly questions get any answers? Joke answers (though yours isn't nearly as bad as some I've seen) are fun, but noise, especially in the cases where later clarification is given or someone figures out what the OP really means.
Maybe a different tack will illustrate what I'm trying to say: "I can't say I'd really recommend this, but it does work." has the same problem as "I can't say I'd really recommend stabbing yourself with a samurai sword, but it does work to get you into the emergency room quickly to have that toothache looked at." That isn't advice I'd give anyone, except in jest. I've never thought of you as one who gives answers purely for amusement.
Joke answers where people upvote for "This is such a roundabout way of obfuscating the loop. +1!" (that comment is itself at +2 currently) just make fun of the OP and the question (I did say yours isn't nearly as bad as some I've seen, but I also believe it was submitted in good faith unlike the really bad ones). Perhaps that pointing and laughing of the OP is deserved, perhaps not, but I don't think it makes a good answer.
|
1

How about doing it recursively:

template<typename Itr>
void inc(Itr itr, Itr end)
{
   if (itr == end) return;
   ++(*itr);
   inc(++itr,end);
}

int main()
{
   int a[4]={4,4,4,4}; 
   inc(a,a + 4);
   return 0;
}

This is tail-recusive, so a decent compiler should convert this into a loop.

Comments

1

SSE2 solution (using intrinsics):

__m128i v1 = _mm_set_epi32(4, 4, 4, 4);
__m128i v2 = _mm_set_epi32(1, 1, 1, 1);
__m128i v3 = _mm_add_epi32(v1, v2);

Comments

1
#include <iostream>
using namespace std;
int main()
{
int a[4]={4,4,4,4};
a[0]++,a[1]++,a[2]++,a[3]++;
return 0;
}

2 Comments

To those who have down voted this, please explain why. please also note that in C++ two operations separated by a , are not considered two separate operations.the , operator chains this together. Whilst this may not be the nicest looking solution, and definitely not scalable it dam well fits the question
They are separate operations, but not separate statements, which is (for unknown reasons) important to the OP.
0

If your array can only have 4 elements, you might be able to use the PADDD instruction of the SSE2 instruction-set extension. I have never used this, so i cannot provide more details.

This question might give you more hints on how to do it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.