In: Computer Science
my code is not printing the output
#include <stdio.h>
#include<stdbool.h>
int main(){
// variable declarations
bool binary[12] = {false,false, false, false,false,false,false,false,false,false,false, false};
int powerTwo[12] = {2048.1028,576, 256, 128, 64, 32, 16, 8 , 4, 2, 1};
int oneZero[9]= {0,0,0,0,0,0,0,0,0};
int tempVal = 0;
double decimal = 0;
int i = 0;
int j = 0;
// Intialization
printf("Starting the CPSC 1011 Decimal to Binary Converter!\n");
printf("Please enter a positive whole number (or EOF to quit): \n");
scanf("%lf", &decimal);
printf("%lf", decimal);
while(j < 100){
++j;
if(feof(stdin)){
printf("Thank you for using the CPSC 1011 Decimal to Binary Generator. GoodBye!");
break;
}
if((decimal < 0) && ((int)(10 * decimal)% 10 != 0)){
printf("Sorry, that was not a positive whole number.");
}
else{
while(i >= 0){
if (decimal > powerTwo[i]){
binary[i] = true;
++i;
}
}
i = 0;
tempVal = decimal;
while(i >= 0){
if(binary[i]== true){
tempVal = tempVal - powerTwo[i];
if(tempVal > 0){
oneZero[i] = 1;
}
else{
oneZero[i] = 0;
tempVal = tempVal + powerTwo[i];
}
}
}
i = 0;
while (i < 10){
printf("%d\n",oneZero[i]);
}
printf(" (base -2)!\n");
}
}
return(0);
}
Please look at my code and in case of indentation issues check the screenshots.
------------main.c----------------
#include <stdio.h>
#include <stdbool.h>
int main()
{
// variable declarations
bool binary[12] = { false, false, false, false, false,
false, false, false, false, false, false, false};
int powerTwo[12] = { 2048, 1028, 576, 256, 128, 64,
32, 16, 8, 4, 2, 1 };
int oneZero[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
int tempVal = 0;
double decimal = 0;
int i = 0;
int j = 0;
// Intialization
printf("Starting the CPSC 1011 Decimal to Binary
Converter!\n");
printf("Please enter a positive whole number (or EOF
to quit): \n");
scanf("%lf", &decimal);
//read decimal
//printf("%lf", decimal);
while (j < 100)
{
++j;
if (feof(stdin))
{
printf("Thank
you for using the CPSC 1011 Decimal to Binary Generator.
GoodBye!\n");
break;
}
if ((decimal < 0)|| ((int)(10
*decimal) % 10 != 0))
{
printf("Sorry,
that was not a positive whole number.\n");
}
else
{
i = 0;
while (i <
12)
{
if (decimal > powerTwo[i])
{
binary[i] = true;
}
++i;
}
i = 0;
tempVal =
decimal;
//oneZero[]
while (i <
12)
{
if (binary[i] == true)
{
if (tempVal - powerTwo[i]
>= 0) //check if subtracting
power2, gives non negative result
{
tempVal =
tempVal - powerTwo[i]; //perform subtraction mark 1 at
the oneZero[i]
oneZero[i]
= 1;
}
else
{
oneZero[i]
= 0;
//tempVal
= tempVal + powerTwo[i];
}
}
++i;
}
i = 0;
while (i <
12)
{
printf("%d", oneZero[i]);
i++;
}
printf(" (base
-2)!\n");
}
printf("\nStarting the CPSC 1011
Decimal to Binary Converter!\n");
printf("Please enter a positive
whole number (or EOF to quit): \n");
scanf("%lf",
&decimal); //read next
decimal
}
return (0);
}
--------------Screenshots-------------------
------------Output-----------------
-------------------Updated------------------------------------------------
#include <stdio.h>
#include <stdbool.h>
int main()
{
// variable declarations
bool binary[12] = { false, false, false, false, false,
false, false, false, false, false, false, false};
int powerTwo[12] = { 2048, 1028, 576, 256, 128, 64,
32, 16, 8, 4, 2, 1 };
int oneZero[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
int tempVal = 0;
double decimal = 0;
int i = 0;
int j = 0;
// Intialization
printf("Starting the CPSC 1011 Decimal to Binary
Converter!\n");
printf("Please enter a positive whole number (or EOF
to quit): \n");
scanf("%lf", &decimal);
//read decimal
//printf("%lf", decimal);
while (j < 100)
{
++j;
if (feof(stdin))
{
printf("Thank
you for using the CPSC 1011 Decimal to Binary Generator.
GoodBye!\n");
break;
}
if ((decimal < 0)|| ((int)(10
*decimal) % 10 != 0))
{
printf("Sorry,
that was not a positive whole number.\n");
}
else
{
i = 0;
while (i <
12)
{
if (decimal >= powerTwo[i])
{
binary[i] = true;
}
++i;
}
i = 0;
tempVal =
decimal;
while (i <
12)
{
if (binary[i] == true)
{
if (tempVal - powerTwo[i]
>= 0) //check if subtracting
power2, gives non negative result
{
tempVal =
tempVal - powerTwo[i]; //perform subtraction mark 1 at
the oneZero[i]
oneZero[i]
= 1;
}
else
{
oneZero[i]
= 0;
//tempVal
= tempVal + powerTwo[i];
}
}
++i;
}
i = 0;
int start =
false;
while (i <
12)
{
if(oneZero[i] == 1)
start = true;
if (start || i == 11)
printf("%d",
oneZero[i]);
i++;
}
printf(" (base
-2)!\n");
}
printf("\nStarting the CPSC 1011
Decimal to Binary Converter!\n");
printf("Please enter a positive
whole number (or EOF to quit): \n");
scanf("%lf",
&decimal); //read next
decimal
}
return (0);
}
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs
down.
Please Do comment if you need any clarification.
I will surely help you.
Thankyou