Question

In: Computer Science

Where is the infinite loop in the code? Input for the code 1 2.5 2 40200000...

Where is the infinite loop in the code?

Input for the code 1 2.5 2 40200000 1 0 2 80400000 2 ffffffff 3

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int count = 31;
void toBinary(int num, int n){
for(int i = 1; i < n; i++){
if((int)(num/pow(2,(n-i))) > 0){
num = num - pow(2,(n-i));
printf("1");
count--;
}else{
printf("0");
count--;
}
}
}

char checkSign (int sign)
{
char new_sign;
if (sign == 0)
{
new_sign = '+';
return new_sign;
}
else
{
new_sign = '-';
return new_sign;
}
}
void oddCheck(int num){
if(num%2 == 0){
printf("0\n");
count--;
}else{
printf("1\n");
count--;
}
}
void dec2IEEE() {
float dec_num;
short sign;
short dec_exp;
printf("\nPlease enter a decimal number: ");
scanf("%f", &dec_num);

/*if dec_num is 0 print out 32-bit IEEE of 0 (32 zeroes)*/
if (dec_num == 0.0f) {
for (int i = 0; i < 31; ++i) {
printf("0");
}
printf("\n");
}
/*check dec_num sign*/
if (dec_num > 0) {
sign = 0;
} else {
sign = 1;
dec_num = abs(dec_num);
}
printf("\nSign : %d\n", sign);
/*find dec_expo*/
dec_exp = floor(log(dec_num) / log(2));
dec_num = dec_num / pow(2, dec_exp);

/*ensure that there is a 1 in front of the decimal*/
while(dec_num < 1.0){
dec_num = dec_num * 2;
dec_exp--;
}

/*create binary exponent first line creates copy of the value of dec_exp + 127*/
printf("%d\n", dec_exp);
printf("Exponent: ");
int exp_copy = dec_exp += 127;
toBinary(dec_exp, 8);
oddCheck(exp_copy);

/*create significand in binary*/
dec_num -= 1;
int power = 0;
while(dec_num != (float)((int)(dec_num))){
dec_num *= 2;
power++;
}
int dec_num_copy = dec_num;
printf("Significand: ");
toBinary(dec_num, power);
oddCheck(dec_num_copy);
while(count > 0){
printf("0");
count--;
}
}

void IEEE2Dec(){
int ieee_num;
int ieee_sign;
int ieee_exp;
float ieee_sig;
float ieee_dec;

printf("\nPlease Enter IEEE number: ");
scanf("%x", &ieee_num);
if(abs(ieee_num) == 0x00000000){
printf("You entered 0\n");
return;
}
if(ieee_num == 0x7f800000){
printf("You entered infinity\n");
return;
}
if(ieee_num == 0xff800000){
printf("You entered negative infinity\n");
return;
}
if((ieee_num & 0x7fffffff) > 0x7f800000){
printf("You entered NAN\n");
return;
}
ieee_sign = floor (ieee_num / pow (2, 31));
printf ("Sign: %c", checkSign (ieee_sign));

ieee_exp = floor ((ieee_num & 0x7F800000) / pow (2, 23));
ieee_exp -= 127;
printf ("\nExponent: %d", ieee_exp);

ieee_sig = (ieee_num & 0x007fffff) / pow (2, 23);
if (ieee_exp < 0)
{
ieee_dec = ieee_sig;
printf ("\nDenormalized Decimal: %c%f*2^(%d)", checkSign (ieee_sign),
ieee_dec, ieee_exp);
return;
}
ieee_sig += 1;
printf ("\nNormalized Decimal: %f", ieee_sig);
ieee_dec = ieee_sig * pow (2, ieee_exp);
printf ("\nDecimal Format: %c%f", checkSign (ieee_sign), ieee_dec);
}
int main() {
int true = 1;
while(true){
printf("Please Enter an Option \n 1. Decimal to IEEE \n 2. IEEE to Decimal \n 3. Exit\n");
int option;
scanf("%d", &option);

switch(option){

case 1:
dec2IEEE();
break;

case 2:
IEEE2Dec();
break;

case 3:
true = 0;
break;
}
}
return 0;
}

Solutions

Expert Solution

// an infinte loop is the one which never ends

// in this code while loop in the main function is the inifinite loop makrked in bold

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int count = 31;
void toBinary(int num, int n){
for(int i = 1; i < n; i++){
if((int)(num/pow(2,(n-i))) > 0){
num = num - pow(2,(n-i));
printf("1");
count--;
}else{
printf("0");
count--;
}
}
}

char checkSign (int sign)
{
char new_sign;
if (sign == 0)
{
new_sign = '+';
return new_sign;
}
else
{
new_sign = '-';
return new_sign;
}
}
void oddCheck(int num){
if(num%2 == 0){
printf("0\n");
count--;
}else{
printf("1\n");
count--;
}
}
void dec2IEEE() {
float dec_num;
short sign;
short dec_exp;
printf("\nPlease enter a decimal number: ");
scanf("%f", &dec_num);

/*if dec_num is 0 print out 32-bit IEEE of 0 (32 zeroes)*/
if (dec_num == 0.0f) {
for (int i = 0; i < 31; ++i) {
printf("0");
}
printf("\n");
}
/*check dec_num sign*/
if (dec_num > 0) {
sign = 0;
} else {
sign = 1;
dec_num = abs(dec_num);
}
printf("\nSign : %d\n", sign);
/*find dec_expo*/
dec_exp = floor(log(dec_num) / log(2));
dec_num = dec_num / pow(2, dec_exp);

/*ensure that there is a 1 in front of the decimal*/
while(dec_num < 1.0){
dec_num = dec_num * 2;
dec_exp--;
}

/*create binary exponent first line creates copy of the value of dec_exp + 127*/
printf("%d\n", dec_exp);
printf("Exponent: ");
int exp_copy = dec_exp += 127;
toBinary(dec_exp, 8);
oddCheck(exp_copy);

/*create significand in binary*/
dec_num -= 1;
int power = 0;
while(dec_num != (float)((int)(dec_num))){
dec_num *= 2;
power++;
}
int dec_num_copy = dec_num;
printf("Significand: ");
toBinary(dec_num, power);
oddCheck(dec_num_copy);
while(count > 0){
printf("0");
count--;
}
}

void IEEE2Dec(){
int ieee_num;
int ieee_sign;
int ieee_exp;
float ieee_sig;
float ieee_dec;

printf("\nPlease Enter IEEE number: ");
scanf("%x", &ieee_num);
if(abs(ieee_num) == 0x00000000){
printf("You entered 0\n");
return;
}
if(ieee_num == 0x7f800000){
printf("You entered infinity\n");
return;
}
if(ieee_num == 0xff800000){
printf("You entered negative infinity\n");
return;
}
if((ieee_num & 0x7fffffff) > 0x7f800000){
printf("You entered NAN\n");
return;
}
ieee_sign = floor (ieee_num / pow (2, 31));
printf ("Sign: %c", checkSign (ieee_sign));

ieee_exp = floor ((ieee_num & 0x7F800000) / pow (2, 23));
ieee_exp -= 127;
printf ("\nExponent: %d", ieee_exp);

ieee_sig = (ieee_num & 0x007fffff) / pow (2, 23);
if (ieee_exp < 0)
{
ieee_dec = ieee_sig;
printf ("\nDenormalized Decimal: %c%f*2^(%d)", checkSign (ieee_sign),
ieee_dec, ieee_exp);
return;
}
ieee_sig += 1;
printf ("\nNormalized Decimal: %f", ieee_sig);
ieee_dec = ieee_sig * pow (2, ieee_exp);
printf ("\nDecimal Format: %c%f", checkSign (ieee_sign), ieee_dec);
}
int main() {
int true = 1;
while(true){
printf("Please Enter an Option \n 1. Decimal to IEEE \n 2. IEEE to Decimal \n 3. Exit\n");
int option;
scanf("%d", &option);

switch(option){

case 1:
dec2IEEE();
break;

case 2:
IEEE2Dec();
break;

case 3:
true = 0;
break;
}
}
return 0;
}


Related Solutions

Code in python Write a while loop code where it always starts form 2. Then it...
Code in python Write a while loop code where it always starts form 2. Then it randomly chooses a number from 1-4. If the number 4 is hit then it will write “TP” if the number 1 is hit then it will write”SL”. It will rerun the program every time the numbers 1 and 5 are hit. The code should also output every single number that is randomly chosen. 2 of the same numbers can't be chosen back to back...
Write a program in java that deliberately contains an endless or infinite while loop. The loop...
Write a program in java that deliberately contains an endless or infinite while loop. The loop should generate multiplication questions with single-digit random integers. Users can answer the questions and get immediate feedback. After each question, the user should be able to stop the questions and get an overall result. See Example Output. Example Output What is 7 * 6 ? 42 Correct. Nice work! Want more questions y or n ? y What is 8 * 5 ? 40...
I need these written in shell code 1.nested loop. e.g. 1*2 + 2*3 + 3*4 +...
I need these written in shell code 1.nested loop. e.g. 1*2 + 2*3 + 3*4 + ...(n-1)*n. (Only nested loops) 2.Fibonacci numbers.
create a program using IDLE where you will gather input from the user using a loop....
create a program using IDLE where you will gather input from the user using a loop. The user needs to provide you with a list of test scores for two categories (two different classrooms). Test scores must be integer values between 0 and 10. You need to process each score so that you can output the following: Number of test scores entered for classroom A. Number of test scores entered for classroom B. Average of test scores entered for classroom...
create a program using IDLE where you will gather input from the user using a loop....
create a program using IDLE where you will gather input from the user using a loop. The user needs to provide you with a list of test scores for two categories (two different classrooms). Test scores must be integer values between 0 and 10. You need to process each score so that you can output the following: Number of test scores entered for classroom A. Number of test scores entered for classroom B. Average of test scores entered for classroom...
Study the following code with a while-loop and convert it to a for-loop (fill in the...
Study the following code with a while-loop and convert it to a for-loop (fill in the blanks). int i=4, result=1; while(i>1) { result *= i; i--; } The following for-loop performs the same functionality: int result=1; for (__________ i=4; i _________1;____________) { result *= i; }
We will extend project 2 by wrapping our input and output in a while loop. Repeatedly...
We will extend project 2 by wrapping our input and output in a while loop. Repeatedly do the following: Prompt the user for their name, get and store the user input. Prompt the user for their age, get and store the user input. We will assume that the user will enter a positive integer and will do no error checking for valid input. Determine and store a movie ticket price based on the user's age. If their age is 12...
(True or False) The following is an infinite loop. int main() { bool flag = false;...
(True or False) The following is an infinite loop. int main() { bool flag = false; int num = 0; while(!flag); { cin >> num; if(num == -1) { flag = true; } } return 0; }
Find a loop invariant (I) for the following while loop with the post-condition {Q: S=1}, where...
Find a loop invariant (I) for the following while loop with the post-condition {Q: S=1}, where S is an integer and the operator / represents an integer division. Show your work step by step to receive full credit. while (S > 1) do S = S / 2; }
CODE WITH ARDUINO: With an RGB Led, Use the iterative loop (for loop) to make red,...
CODE WITH ARDUINO: With an RGB Led, Use the iterative loop (for loop) to make red, blue, and green. Use ‘analogWrite’ command inside the 'for loop.' Use the value ranges from 0-255 for this command. So, to activate any of the pins, the value should be 255. To turn off 0. pattern: First for loop Red light – delay – Second for loop for blue light – delay - Third for loop for green light - delay. (The resulting lights...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT