|
Course Index Dr. Margush's page |
Modify the game (Fig 5.8) to use unsigned 16-bit integers for all of the guessing. The user may enter numbers in the range 0 through 65535. Use the atoi and itoa macros for IO. Let me know if you have problems.
Convert this to assembly language (use 32-bit integers):
unsigned int a, b;
cout << "Enter two positive numbers: ";
cin >> a >> b;
unsigned int gcd=a, rem=b;
while (rem != 0){
unsigned int d = gcd;
gcd = rem;
rem = d % gcd;
}
cout << "The gcd of " << a << " and
" << b << " is " << gcd;
Convert this to assembly language:
char x;
do {
unsigned int v = 0;
cout << "Enter a binary number (max 32 bits):
";
do {//you may need to input a string and process digits from
memory
cin >> x;
if (x!='0' && x!= '1') break;
x -= '0';
v = v*2 + x;
}while(true);
//<<<<<<<<<<<<<<forgot the infinite
loop condition
cout << "You entered " << v <<
"\n";
cout << "Play again? (y/n): ";
cin >> x;
}while (x == 'y');
Translate to assembly:
const int S=10000; //try different values of S
unsigned char p[S] = {0};
int n=2;
while (n<S){
if (p[n]==0)
for (int i=2*n; i<S; i+=n) p[i]=1;
n++;
}
int count = 0;
for (int j=2; j<S; j++)
if (p[j]==0){
count++;
cout << j << endl; //<<<<<<<<this
line was corrected
}
cout << count << " values displayed\n";