-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 ;-)
    – Bathsheba
    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.
    – cigien
    Aug 24, 2020 at 14:08
  • updated the question )
    – buster
    Aug 24, 2020 at 14:13
  • An update shouldn't invalidate answers. You should instead ask a new question. 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;
}
4
  • Yup sensible. You'll get another vote if you can use a C++11 lamda in another answer?
    – Bathsheba
    Aug 24, 2020 at 13:54
  • @Bathsheba You mean as in using std::accumulate or std::for-each or something like that?
    – Ted Lyngmo
    Aug 24, 2020 at 13:55
  • Absolutely, yes!
    – Bathsheba
    Aug 24, 2020 at 13:56
  • 1
    @Bathsheba Added :)
    – Ted Lyngmo
    Aug 24, 2020 at 14:01
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
  • You were first to get the index correct. Oops. +1.
    – Bathsheba
    Aug 24, 2020 at 13:45
  • @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
    Aug 24, 2020 at 13:47
  • @MikeCAT: I can't resist putting in my preferred way -->.
    – Bathsheba
    Aug 24, 2020 at 13:47
  • updated the question and your example won't work now)
    – buster
    Aug 24, 2020 at 14:13
  • @buster added answer for the new question.
    – MikeCAT
    Aug 24, 2020 at 14:16
1

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

2
  • 1
    This works if you use an int, but will not work for a size_t type.
    – Bathsheba
    Aug 24, 2020 at 13:49
  • updated the question and your example won't work now)
    – buster
    Aug 24, 2020 at 14:13
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
  • thank, but I don't think it's worth writing this code --> :)
    – buster
    Aug 24, 2020 at 13:50
  • @buster: you don't like for (size_t i = 3; i --> 0;) {?
    – Bathsheba
    Aug 24, 2020 at 13:51
  • Another variant is for (size_t i = 2; i <= 2; --i) { but I don't like the repeat.
    – Bathsheba
    Aug 24, 2020 at 13:51
  • @Bathsheba it looks confusing to me, although it seems simple.
    – buster
    Aug 24, 2020 at 13:52
  • @buster: perhaps I've been doing this for too long now, but I find it natural, and no worse than function pointer declarations.
    – Bathsheba
    Aug 24, 2020 at 13:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.