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..

Alex
  • 1
  • 1

6 Answers6

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));
}
zeuxcg
  • 9,216
  • 1
  • 26
  • 33
  • 2
    This is such a roundabout way of obfuscating the loop. +1! – Jonathan Grynspan Dec 26 '10 at 17:33
  • 3
    OP's question aside, when would one ever, *ever*, **ever** find this useful in practice over a loop? :P – moinudin Dec 26 '10 at 17:38
  • @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)`). – zeuxcg Dec 26 '10 at 17:50
  • @zeuxcg In other languages, agreed, I use functional language features all the time myself. – moinudin Dec 26 '10 at 17:53
  • 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. – Martin York Dec 26 '10 at 20:23
  • 1
    @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; } );` – David Rodríguez - dribeas Feb 21 '11 at 19:16
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.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 1
    Why would you give an answer you don't recommend? – Fred Nurk Feb 21 '11 at 18:36
  • 1
    @Fred Nurk: Because I think it's as good an answer as any to a silly question. – Jerry Coffin Feb 21 '11 at 18:41
  • 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. – Fred Nurk Feb 21 '11 at 18:42
  • @Fred Nurk: because some people honestly want to know about foolish things like using Java, C#, or avoiding loops, even though they're all bad ideas. Looked at from a slightly different direction, because some people disagree with me about what's silly. OTOH, if you want to manipulate arrays in C++ without loops, `std::valarray` is often as good an approach as any. – Jerry Coffin Feb 21 '11 at 18:45
  • You called it silly, which means it either is silly or you don't understand what the OP really wants to ask. In the former, all it will get is joke answers, because there's not even a real problem that's being solved (see Jeff Atwood's condemnation of Yahoo! Answers for good examples of that). In the latter, it's incredibly hard to provide a useful answer when you don't understand the question. – Fred Nurk Feb 21 '11 at 18:50
  • @Fred Nurk: You seem to assume that "silly" is a statement of fact that's either true or false. In reality, it's a value judgement, about which there may be legitimate disagreement. I happen to think the stated *goal* is silly, but for those who disagree, I think the answer I gave is a good as any, and better than most. – Jerry Coffin Feb 21 '11 at 18:59
  • No, I carefully didn't assume that by giving the option that either the overwhelming consensus agrees with you ("it is silly") or that your perception is flawed ("you don't understand"). – Fred Nurk Feb 21 '11 at 19:01
  • Yet it seems to me that there is (at least) a third option, where disagreement with the overwhelming majority does *not* mean ones perception is flawed at all. In some cases, the majority have a thoroughly flawed perception. Other cases really are matters of opinion or taste, where perfectly reasonable people can disagree, and there's nothing "flawed" about the perception of either. – Jerry Coffin Feb 21 '11 at 19:12
  • "Silly" means more than simple disagreement due to differently weighting concerns, or at least that is how I understood you both in that comment and with "I can't say I'd really recommend this". Akin to "makes no sense under any circumstances of which I'm aware". If I don't understand any circumstances under which my advice should be used, I shouldn't give that advice. Clarifying the question to make me aware of a new set of circumstances would resolve that flawed perception of the problem. – Fred Nurk Feb 21 '11 at 20:31
  • 1
    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. – Fred Nurk Feb 21 '11 at 20:36
  • 1
    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. – Fred Nurk Feb 21 '11 at 20:47
  • I mostly avoid amusing myself at the expense of the author, but have no problem with things like code golf, where everybody agrees up front that it's silly. In this case, the intent wasn't entirely clear, so I posted an answer, but with a little bit of a warning. It's definitely *not* intended to be obfuscated (and I don't think it is). It really is about the best way do accomplish the stated goal. Even though valarray is rarely used, it was certainly intended to be used, and this falls directly within its intended use. – Jerry Coffin Feb 21 '11 at 22:03
  • At the same time, there are some pretty good reasons that valarray is rarely used, so I do feel that almost any suggestion of using it nearly requires at least a little bit of a warning. – Jerry Coffin Feb 21 '11 at 22:04
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.

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);
Paul R
  • 208,748
  • 37
  • 389
  • 560
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
    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 – thecoshman Feb 21 '11 at 18:28
  • 1
    They are separate operations, but not separate statements, which is (for unknown reasons) important to the OP. – Fred Nurk Feb 21 '11 at 18:41
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.

Community
  • 1
  • 1
anatolyg
  • 26,506
  • 9
  • 60
  • 134