Question

In: Computer Science

my code is not printing the output #include <stdio.h> #include<stdbool.h> int main(){ // variable declarations   bool...

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);

}


Solutions

Expert Solution

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


Related Solutions

My current code that is not working correctly #include<stdio.h> #include<math.h> int main() { //variable declaration int...
My current code that is not working correctly #include<stdio.h> #include<math.h> int main() { //variable declaration int a,b,c,D,n; double x1,x2; double realPart, imagPart; do { // this set of code blocks prompts a message to the user and then read the integer entered and stores it as an integer printf("Enter a value for a:\n"); scanf("%d",&a); printf("Enter a value for b:\n"); scanf("%d",&b); printf("Enter a value for c:\n"); scanf("%d",&c); printf("You entered the Equation \n"); printf("%dx^2%+dx%+d=0\n",a,b,c); D = b*b - 4*a*c;    if (D<0)...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating();...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating(); int main() { // declaring variables int size = 5; int jerseyNo[size]; int rating[size]; int i = 0, jno, rate; char option; /* Getting the inputs entered by the user * and populate the values into arrays */ for (i = 0; i < size; i++) { printf("Enter player %d's jersey number:", i + 1); jerseyNo[i] = getValidJerseyNumber(); printf("Enter player %d's rating:\n", i +...
C CODE PLZ! All instructions for what to do in code #include <stdio.h> int main(int argc,...
C CODE PLZ! All instructions for what to do in code #include <stdio.h> int main(int argc, char **argv) { int n, k, l, r, t, d, i; char str[65]; /* Make the user enter a non-negative integer */ printf("Please enter a non-negative integer: "); scanf("%d", &n); while (n < 0) { printf("Sorry, your input is incorrect.\n"); printf("Please enter a non-negative integer: "); scanf("%d", &n); } /* Convert the integer to reversed binary: e.g. 6 gets converted to 011 */ if...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main(...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main( ) {                         for (int i=1; i<=2; i++)                                     printf("%d", fac(i)); } int fac(int x) {                         x = (x>1) ? x + fac(x-1) : 100);                         return x; }
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main(...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main( int argc, char* argv[] ) { // Size of vectors int n = 10000; // Input vectors double *restrict a; double *restrict b; // Output vector double *restrict c; // Size, in bytes, of each vector size_t bytes = n*sizeof(double); /* Q1: Allocate memory for vector a (10 points)*/ /* Q2: Allocate memory for vector b (10 points)*/ /* Q3: Allocate memory for vector...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter between 8 to 60 inches: "); scanf("%d",&diameter); // if(diameter>60 || diameter<=8){ // printf("Error! invalid input"); // exit(0); // } // else{ // float radius = diameter/2; // float volume = (4/3)*PI*radius*radius*radius; // printf("%.2f",volume); // } //check through the while loop if it is valid or in valid while(diameter>60 || diameter<=8){ printf("Invalid input Enter again: "); scanf("%d",&diameter); }    //caluclate the volume of sphere float...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x = 3;     i = fun(x);     printf("%d\n", i);     return 0; } int fun(int i) {      int res = 0;      res = pow (i , 3.0);      return ( res); }
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc != 2) || (sscanf(argv[1],"%d",&count) != 1)) { fprintf(stderr,"Usage: %s <integer>\n", argv[0]); exit(1); } pid_t pid1, pid2; while (count > 0) { pid1 = fork(); if (pid1 > 0) { pid2 = fork(); count = count - 2; } else if (pid1 == 0) { count = count - 1; } } exit(0); } Question #1 [2 pts] If the command-line argument passed to this...
What’s the output of following code fragment: #include<stdio.h> #include<signal.h> void response (int sig_no) { printf("25"); }...
What’s the output of following code fragment: #include<stdio.h> #include<signal.h> void response (int sig_no) { printf("25"); } void response2 (int sig_no) { printf("43"); }     int main() {      int id = getpid();      signal(SIGUSR1, response); signal(SIGKILL, response2); for(int i=0; i<4; i++) { sleep(1); if (i % 3 == 0) { kill(id, SIGUSR1); } else { kill(id, SIGKILL); } } return 0; }
#include <stdio.h> int main () { int value= 10; int value2= 5; value = value %2;...
#include <stdio.h> int main () { int value= 10; int value2= 5; value = value %2; printf("he FInal =value=%d\n", value); value +=3; printf("he FInal =value=%d\n", value); value ++; printf("he FInal =value=%d\n", value); value= ++value2; printf("he FInal =value=%d\n", value); value= value2--; printf("he FInal =value=%d\n", value); } what is output explain each print statement? exlain why?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT