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.
#include <iostream>
usingnamespace 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<<" ";
}
}
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.
#include <iostream>
usingnamespace 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<<" ";
}
}
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.
#include <iostream>
usingnamespace 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.
#include <iostream>
usingnamespace 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.
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++
}
#include <iostream>
usingnamespace 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<<" ";
}
}
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.
#include <iostream>
usingnamespace 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++; }
elsebreak;
}
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)<<" ";
}
}