Empty Output in Random Generator

random generator: take a random 4-digit number, then square it, then divide it by 100, then take (minus) the remainder of division after divided by 10,000. Count number of "random generate" until the process loops.
Example: 4100 -> 8100 -> 6100 -> 2100 -> 4100.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include <iostream>
using namespace std;
int main () 
{
    int a;
    cin>>a;
    for (int i=0;i<a;i++)
    {  int b;
       cin>> b;   
       int c=b; 
       int loops =0;
       b-=1; //to ensure the do while loop
       do
       { b+=1; //to undo the minus
       b*=b; //Multiply it by itself (i.e. raise to power 2) to get value consisting of 8 digits
       b/=100;           //To truncate the 8-digit value, divide it by 100 and then take remainder of division by 10000.
       int d=b%10000;
       b-=d;
       loops++;  
       } while (b!=c);               //To get more values, repeat from step 2.
       cout<<loops<<" ";            
    }
}
Get rid of lines 12 and 14.

You're asking for input before the for loop. Why? [edit: Nevermind. I see why. But you aren't prompting for any input so I wouldn't be surprised if you did the same thing I did and didn't input a second number because you thought the first was sufficient.]

It's pretty clear your description and implementation of the algorithm for transforming the number is not correct with respect to the series of numbers used for an example. If you were to remove the (minus) it would be closer.

Last edited on
trying to make the function work at least once and then continuing doesn't work either:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;
int main () 
{
    int a;
    cin>>a;
    for (int i=0;i<a;i++)
    {  int b;
       cin>> b;   
       int c=b; 
       int loops =0;
        if (b==c)
        {b*=b; //Multiply it by itself (i.e. raise to power 2) to get value consisting of 8 digits
       b/=100;           //To truncate the 8-digit value, divide it by 100 and then take remainder of division by 10000.
       int d=b%10000;
       b-=d;
       loops++;  
        }
       do{b*=b; //Multiply it by itself (i.e. raise to power 2) to get value consisting of 8 digits
       b/=100;           //To truncate the 8-digit value, divide it by 100 and then take remainder of division by 10000.
       int d=b%10000;
       b-=d;
       loops++;  
       }while (b!=c);               //To get more values, repeat from step 2.
       cout<<loops<<" ";            
    }
}
cire wrote:
t's pretty clear your description and implementation of the algorithm for transforming the number is not correct with respect to the series of numbers used for an example. If you were to remove the (minus) it would be closer.
removing minus doesn't help; still an empty output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;
int main () 
{
    int a;
    cin>>a;
    for (int i=0;i<a;i++)
    {  int b;
       cin>> b;   
       int c=b; 
       int loops =0;
        if (b==c)
        {b*=b; //Multiply it by itself (i.e. raise to power 2) to get value consisting of 8 digits
       b/=100;           //To truncate the 8-digit value, divide it by 100 and then take remainder of division by 10000.
       b=b%10000;
       loops++;  
        }
       do{b*=b; //Multiply it by itself (i.e. raise to power 2) to get value consisting of 8 digits
       b/=100;           //To truncate the 8-digit value, divide it by 100 and then take remainder of division by 10000.
       b=b%10000;
       loops++;  
       }while (b!=c);               //To get more values, repeat from step 2.
       cout<<loops<<" ";            
    }
}
removing minus doesn't help; still an empty output:

You remember what I asked about getting input before the for loop and inside the for loop? If you only provide one input, you're not going to get a result, because the program is waiting for the second input.

Lines 12 through 17 are entirely unnecessary.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
int main () 
{
    int a, b;
    cin>>a;
    for (int i=0;i<a;i++)
    {  b=0; 
       cin>> b;   
       int c=b; 
       int loops =0;
       do{b*=b; //Multiply it by itself (i.e. raise to power 2) to get value consisting of 8 digits
       b/=100;           //To truncate the 8-digit value, divide it by 100 and then take remainder of division by 10000.
       b=b%10000;
       loops++;  
       }while (b!=c);               //To get more values, repeat from step 2.
       cout<<loops<<" ";            
    }
}

I'm not sure what you mean, and I don't know where I'd put my second input.
Last edited on
I'm not sure what you mean, and I don't know where I'd put my second input.

Run the program. For input, instead of say "4100", put "1 4100". The 1 will be read on line 6, and will be used to limit the for loop on line 7. The 4100 will be read on line 9...
Could it be because b is very rarely going to equal c because you are using integer variables that when are assigned a decimal they just take the whole number and cut off the decimal.
the problem automatically inputs how many numbers I have to read.
Sample input: 13
9762 2057 6808 779 6382 5640 5546 6810 2665 5959 9511 1687 8090

And also, the problem presumes that b will eventually get to c. The correct solution for the above input, is the following: 107 103 108 108 100 99 105 104 101 106 109 107 98
Ah, the problem looks for ANY kind of cycle, making this much much harder. So now I'm stuck on how to check every single value for looping. I think I could have a loop inside a loop, one where I increase the array value to check for every value in my array, and the other to increase the array value so as to see if it is equal to the current array. In pseudo-code, I suppose it would look like this:
for i=0; i<array_length; i++
{numb=0
array[numb]
for i=0; i<array_length; i++
numbb=1
array[numbb]
loop++
if array[numb]=array[numbb]
break;
else numbb++
numb++
}
I tried this but still got an empty output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;
int main () 
{
    int a, b;
    cin>>a;
    int array[a];
    int numb=0;
    for (int i=0;i<a;i++)
    {  cin>> b;   
       int loops =0;
       int numbb=1; 
       do 
       {array[numb]=b; //checking first number 
             for (int i=0; i<150;i++) //answer is never over 150
             { b*=b; //Multiply it by itself (i.e. raise to power 2) to get value consisting of 8 digits
               b/=100;           //To truncate the 8-digit value, divide it by 100 and then take remainder of division by 10000.
               b=b%10000;
              array[numbb]=b;
              if (array[numb]!=array[numbb])
                {loops++;
                numbb++; }
             }
        numb++; //check every single number in the array (increase value)
       } while (array[numb]!=array[numbb]); //To get more values, repeat from step 2.
      cout<<loops<<" ";            
    }
}
I tried this but still got an empty output

You keep saying that, and I think nobody knows exactly what you meany by "empty output."

Your code is incredibly hard to read with your brace placement, inconsistent indentation and uninspiring near-identical variable names.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <unordered_set>

int main()
{
    std::size_t nums;
    std::cin >> nums;

    for (std::size_t i = 0; i < nums; ++i)
    {
        int value;
        std::cin >> value;

        std::unordered_set<int> encountered;

        while (encountered.count(value) == 0)
        {
            encountered.insert(value);
            value = (value * value / 100) % 10000;
        }

        std::cout << encountered.size() << ' ';
    }
}
Last edited on
Okay, this is much more organized and actually gets extremely close to the answer, only omitting the final answer and also being off by no more than 3 on some answers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;
int main () 
{
    int a, b;
    cin>>a;
    int array[a];
    for (int i=0;i<a+1;i++)
    {  cin>> b;   
       int numb=0; int numbb=1; 
       do 
       {array[numb]=b; //checking first number 
        for (int i=0; i<125;i++) //answer is never over 125
        { b*=b; //Multiply it by itself (i.e. raise to power 2) to get value consisting of 8 digits
               b/=100;           //To truncate the 8-digit value, divide it by 100 and then take remainder of division by 10000.
               b=b%10000;
              array[numbb]=b;
              if (array[numb]!=array[numbb])
                {numbb++; }
                else 
                break;
             }
        numb++; //check every single number in the array (increase value)
       } while (array[numb]!=array[numbb]); //To get more values, repeat from step 2.
      numb++;
      cout<<(numb)<<" ";            
    }
}

Input: 11
3017 5690 4877 4514 4983 6186 167 5368 8935 3234 3185
Output: 98 102 102 102 102 102 106 102 102 98
expected: 101 104 102 104 102 102 109 104 102 99 98
Hmmmm......
Topic archived. No new replies allowed.