3

I'm a new programmer, and want to generate array of number with 3-digits from number list (2,5,8). I have created a code, but the output is not my expected result. This is my simple code:

int main()
{
    int arr[3]={2,5,8};

    int d=3;
    int times=1;

    for (int a:arr){
        int sum = a;
        for (int i=1; i<d; i++){
            times *= 10;
            sum += a*times;
        }
        cout<<sum<<endl;
        sum=0;
    }

    return 0;
}

My expected the result of 222,555 and 888, but the actual output is 222,55005 and 8800008.

jowwyss79
  • 33
  • 6
  • Just to clarify, you want the result to be each of the input digits repeated 3 times? – Steve May 23 '19 at 07:44
  • 2
    You forgot to reset `times`, stick a `times = 1;` in your outer loop. – Blaze May 23 '19 at 07:45
  • 2
    Shouldn't you reset the `times` variable for each number in the array? – vahancho May 23 '19 at 07:45
  • 1
    @vahancho Yes, I shouldn't my reset the ```times``` variable. Thank you for reminding me. – jowwyss79 May 23 '19 at 07:53
  • 1
    @Blaze You're right, I'm forget about that. Thank you for reminding me. – jowwyss79 May 23 '19 at 07:54
  • 1
    @jowwyss79 no problem, glad it helped. Raffallo wrapped that into an answer, so make sure to upvote it and select it as the accepted answer. – Blaze May 23 '19 at 07:55
  • @Steve Actually, I want to generate all number with 3-digit from list (2,5,8), that are 222,258,285,522,... But when I tried to generate simple result, I can't get the expected result. – jowwyss79 May 23 '19 at 07:58

4 Answers4

4

It will help probably. You forget to reset times variable

int main()
{
    int arr[3]={2,5,8};

    int d=3;
    int times=1;

    for (int a:arr){
        int sum = a;
        for (int i=1; i<d; i++){
            times *= 10;
            sum += a*times;
        }
        cout<<sum<<endl;
        times = 1;   //<---added
        sum=0;
    }
    return 0;
}
Raffallo
  • 651
  • 8
  • 19
3

For your current code to generate 222, 555, 888, you forget to reinit times.

You might have created a sub function for clarification:

int mul_by_111(int n) // { return 111 * n; }
{
    int sum = a;
    int times = 1;
    for (int i = 1; i < 3; ++i) {
        times *= 10;
        sum += a * times;
    }
    return sum;
}

int main()
{
    int arr[] = {2, 5, 8};
    for (int a:arr){
        std::cout << mul_by_111(a) << std::endl;
    }
}

If you want the cartesian product to display 222, 225, 228, 522, .., 888

you might (naively) do it with 3 loops:

int main()
{
    int arr[] = {2, 5, 8};
    for (int a:arr){
        for (int b:arr){
            for (int c:arr){
                std::cout << a << b << c << std::endl;
            }
        }
    }
}

Some libraries as range-v3 propose cartesian_product to allow even simpler:

for (auto t : ranges::view::cartesian_product(arr, arr, arr)) {
    std::cout << std::get<0>(t) << std::get<1>(t) << std::get<2>(t) << std::endl;
    // std::apply([](auto... args){ (std::cout << ... << args) << std::endl; }, t); // C++17
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302
0

Using a std::stringstream it becomes trivial:

#include <sstream>
#include <iostream>

int main()
{
  std::stringstream ss;
  ss << 2 << 5 << 8; 
  int result{};
  ss >> result;
  std::cout << result << std::endl;
}

Update:

To make the permutation part of algorithm you may use std::next_permutation:

std::string s("258");
do {
    std::cout << s << '\n';
} while(std::next_permutation(s.begin(), s.end()));
darune
  • 10,480
  • 2
  • 24
  • 62
  • First I also thought to using `stringstream` for my problem. But Is it can generate all number with 3-digit from list (2,5,8), like 285,288,282 etc. ? – jowwyss79 May 23 '19 at 08:11
  • @jowwyss79 I updated the answer per your request. does it make sense ? – darune May 23 '19 at 08:39
  • Yes, it does. Thank you for your updating and it helped me. – jowwyss79 May 23 '19 at 08:55
  • 1
    It seems op wants cartesian product (OP also wants `222`), whereas `std::next_permutation` only provides permutations, so: 258, 285, 528, 582, 825, 852. – Jarod42 May 23 '19 at 09:00
0

The problem is rather the scope of variables. Just keep them in the block and all is well:

int main()
{
    int arr[3]={2,5,8};

    int d=3;

    for (int a:arr){
        int sum = a;
        int times=1;
        for (int i=1; i<d; i++){
             times *= 10;
             sum += a*times;
        }
        cout<<sum<<endl;
    }
    return 0;
}

The variables sum and times will initialize on blockentry. No need to reset them.

Thomas G.
  • 145
  • 7