-3

This is a simple question don't look for difficulties.

I have such a cycle

int arr[]{ 111, 222, 333 }; for (size_t i = 0; i < 3; i++) { test = 1000 * test + arr[i]; } std::cout << test; 

it returns the result 111222333

the question is how to make it output the result 333222111 in short add numbers from the other side

you need to change this line to something else to add values in a different way

test = 1000 * test + arr[i]; 

I will update the question a little bit, that would be interesting, what if there is no array and you get the data in this form.

test = 1000 * test + X; 

X is an unknown number and not an array

4
  • Well needless to say I love the accepted answer on the duplicate ;-) Commented Aug 24, 2020 at 13:57
  • If you're interested in c++20, you can use std::ranges::views::reverse as mentioned in the second dupe target. Commented Aug 24, 2020 at 14:08
  • updated the question ) Commented Aug 24, 2020 at 14:13
  • An update shouldn't invalidate answers. You should instead ask a new question. Commented Aug 24, 2020 at 21:40

4 Answers 4

3

You could do the loop from the end using reverse iterators:

#include <iostream> #include <iterator> int main() { int arr[]{ 111, 222, 333 }; int test = 0; for(auto it = std::rbegin(arr); it != std::rend(arr); ++it) { test = 1000 * test + *it; } std::cout << test; } 

Here's an alternative using std::accumulate and a lambda:

#include <iostream> #include <iterator> #include <numeric> int main() { int arr[]{ 111, 222, 333 }; int test = std::accumulate(std::rbegin(arr), std::rend(arr), 0, [](const auto a, const auto b){ return 1000 * a + b; } ); std::cout << test; } 
Sign up to request clarification or add additional context in comments.

4 Comments

Yup sensible. You'll get another vote if you can use a C++11 lamda in another answer?
@Bathsheba You mean as in using std::accumulate or std::for-each or something like that?
Absolutely, yes!
@Bathsheba Added :)
2

You can use

test = 1000 * test + arr[2 - i]; 

instead of the line.

To handle input other than arrays, you can use "delta" to keep track the place to insert new values.

int delta = 1; for (size_t i = 0; i < 3; i++) { test += delta * X; delta *= 1000; } 

6 Comments

You were first to get the index correct. Oops. +1.
@Bathsheba Actually I prefer arr[3 - 1 - i] because we can use same 3 and therefore it is easy to replace this to constant value.
@MikeCAT: I can't resist putting in my preferred way -->.
updated the question and your example won't work now)
@buster added answer for the new question.
|
1

Iterate the loop from i=2 to 0 i.e for(size_t i = 2; i >= 0; i --)

2 Comments

This works if you use an int, but will not work for a size_t type.
updated the question and your example won't work now)
1

Assuming you can't reverse the elements in arr, perhaps the simplest way is to write

test = 1000 * test + arr[2 - i]; 

perhaps not hardcoding the 2. This is considerably simpler than running the loop the other way: size_t is unsigned so getting the stopping conditional correct takes some thought. Personally I use

for (size_t i = 3; i --> 0;) { 

for this kind of thing. Note well the slide operator -->. It's not really an operator at all but it consists of the postfix -- followed by the comparison. It's not to everyone's taste and some software houses ban its use.

6 Comments

thank, but I don't think it's worth writing this code --> :)
@buster: you don't like for (size_t i = 3; i --> 0;) {?
Another variant is for (size_t i = 2; i <= 2; --i) { but I don't like the repeat.
@Bathsheba it looks confusing to me, although it seems simple.
@buster: perhaps I've been doing this for too long now, but I find it natural, and no worse than function pointer declarations.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.