Question

C CODE PLZ! Need all TO DO sections finished thanks #include <stdio.h> int main(int argc, char...

C CODE PLZ! Need all TO DO sections finished thanks

#include <stdio.h>


int main(int argc, char **argv) {
  const int BUF_LEN = 128;
  char str[BUF_LEN];
  int i;
  char c;
  int is_binary;
  int d, n;

  /* Get the user to enter a string */
  printf("Please enter a string made of 0s and 1s, finishing the entry by pressing Enter.\n");
  for (i=0; i<BUF_LEN-1; i++) {
    scanf("%c", &c);
    if (c == '\n') {
      break;
    }
    str[i] = c;
  } 
  str[i] = '\0';

  /* Check if the user string contains nothing but 0s and 1s */
  is_binary = 1;

  /* TODO */
  
  /* Proceed only of the string contains only 0s and 1s */
  if (is_binary) {
    /* Convert the string of 0s and 1s to an integer number n */
    n = 0;

    /* TODO */

    printf("The binary number %s is %d in decimal.\n", str, n);
  } else {
    printf("The string you entered, \"%s\", contains characters other than 0 and 1.\n", str);
  }

  return 0;
}

Homework Answers

Answer #1
//header files
#include <stdio.h>
//included this header file as pow function is in this header files
#import <math.h>
//main function
int main(int argc, char **argv) {
    //string length
  const int BUF_LEN = 128;
  //array of characters str having capacity of BUF_LEN
  char str[BUF_LEN];
  //declaring variables
  int i;
  char c;
  int is_binary;
  int d, n;

  /* Get the user to enter a string */
  printf("Please enter a string made of 0s and 1s, finishing the entry by pressing Enter.\n");
  for (i=0; i<BUF_LEN-1; i++) {
    scanf("%c", &c);
    if (c == '\n') {
      break;
    }
    str[i] = c;
  }
  str[i] = '\0';

  /* Check if the user string contains nothing but 0s and 1s */
  is_binary = 1;

  /* TODO */
//i is the number of characters in character array
//j going 0 to i-1
  for(int j=0;j<i;j++)
  {
      //if the character at index j is neither 0 nor 1
      if(str[j]!='0' && str[j]!='1')
        {   //set is_binary to 0 which indicates that the string does not represent binary number
            is_binary=0;
            //go out of the loop
            break;
        }

  }//loop ends

  /* Proceed only of the string contains only 0s and 1s */
  //if is_binary is 1
  if (is_binary) {
    /* Convert the string of 0s and 1s to an integer number n */
    n = 0;

    /* TODO */
    //set d to 0 which points to the position of the first bits from the right side
     d=0;
    //j going from i-1 to 0 that means converting bits(from right side) to decimal and adding the results
    for(int j=i-1;j>=0;j--)
    {
        //use this formula
        //pow(2,d) which is 2 to the power d
        //(int) will convert the character(either 0 or 1) to its ASCII value(integer)
        //ASCII value of 1 is 49 and for 0 is 48 so here i am subtracting 48 from the ASCII value to get the real bit value
        //1-0=1 that is 49-48=1
        //0-0=0 that is 48-48 =0
        //((int)str[j]-48) means the bit value at index j
       n=n+pow(2,d)*((int)str[j]-48);
        //incrementing position
        d++;
    }
    //printing the decimal value
    printf("The binary number %s is %d in decimal.\n", str, n);
  }
  //if is_binary is 0
  else {
    //print this message
    printf("The string you entered, \"%s\", contains characters other than 0 and 1.\n", str);
  }

  return 0;
}
//main ends


Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char *b[]) { 6 int c = atoi(a[0]); 7 for (int i = 0; i < c && b[i]; ++i) { 8 printf("%s", b[i]+2); 9 } 10 } 11 12 void main(int argc, char *argv[]) { 13      14 switch (argc) { 15 case 1: 16 for (int i = 0; environ[i]; ++i) {    17 printf("%s\n", environ[i]); 18 } 19 break; 20 default: 21 output(argv +...
C CODE PLZ! All instructions are in sections of code #include <stdio.h> /* TODO: Define 3...
C CODE PLZ! All instructions are in sections of code #include <stdio.h> /* TODO: Define 3 functions input, gcd and lcm in such a way that the main function below compiles correctly and has the correct behavior. The input function prompts the user to enter a non-negative integer. If the user enters a negative integer, the function prints a "sorry" statement and prompts the user again. It keeps on prompting until the user enters a non-negative number. The input function...
#include #include #define MAX_SIZE 1300 // Maximum string size int main() { char str[MAX_SIZE]; char tosearch[MAX_SIZE];...
#include #include #define MAX_SIZE 1300 // Maximum string size int main() { char str[MAX_SIZE]; char tosearch[MAX_SIZE]; printf("Enter any string: "); gets(str); printf("Enter word to search occurrences: "); gets(tosearch); int cursor = 0; int i = 0; int stringLen = 0; int searchLen = 0; int count1; int count2; stringLen = strlen(str); searchLen = strlen(tosearch); count1 = 0; count2 = 0; for (cursor = 0; cursor <= stringLen; ) { if (str[cursor] == tosearch[i]) { count1++; if (count1 == searchLen) {...
#include <unistd.h> #include <fcntl.h> #include <stdlib.h> #include <stdio.h> int main(int argc, char *argv[]){ int fd1, fd2;...
#include <unistd.h> #include <fcntl.h> #include <stdlib.h> #include <stdio.h> int main(int argc, char *argv[]){ int fd1, fd2; char buffer[100]; long int n1; if(((fd1 = open(argv[1], O_RDONLY)) == -1) || ((fd2 = open(argv[2], O_CREAT|O_WRONLY|O_TRUNC,0700)) == -1)){ perror("file problem "); exit(1); } while((n1=read(fd1, buffer, 512) > 0)) if(write(fd2, buffer, n1) != n1){ perror("writing problem "); exit(3); } // Case of an error exit from the loop if(n1 == -1){ perror("Reading problem "); exit(2); } close(fd2); exit(0); } There is an issue with this...
Construct a flowchart based on this code and write its equivalent algorithms. #include <stdio.h> int main()...
Construct a flowchart based on this code and write its equivalent algorithms. #include <stdio.h> int main() { int x,y; float result; char ch; //to store operator choice printf("Enter first number: "); scanf("%d",&x); printf("Enter second number: "); scanf("%d",&y); printf("Choose operation to perform (+,-,*,/): "); scanf(" %c",&ch); result=0; switch(ch) { case '+': result=x+y; break; case '-': result=x-y; break; case '*': result=x*y; break; case '/': result=(float)x/(float)y; break; case '%': result=x%y; break; default: printf("Invalid operation.\n"); } printf("Result: %d %c %d = %.2f\n",x,ch,y,result); // Directly...
Please explain what will be printed out at LINE1 and LINE2? #include <stdio.h> #include <types.h> int...
Please explain what will be printed out at LINE1 and LINE2? #include <stdio.h> #include <types.h> int data[4] = {1,3,5,7}; void *runner (void *param); int main(int argc,char *argv[]) { pid_t pid; pthread_t tid; pid = fork(); if (pid==0){ pthread_create(&tid,NULL,runner,NULL); pthread_join(tid,NULL); for(int i=0,i<4,i++) printf("from child process, the values at i is %d",data[i]); } else if (pid>0){ wait(NULL); for(int i=0;i<4,i++) printf("from parent process, the values at i is %d",data[i]); } } void *runner(void *param){ for(int i=0;i<4,i++) data[i]+=3*i; pthread_exit(0); } } } } }
would someone kindly convert the following C code to java? thanks so much # include <stdio.h>...
would someone kindly convert the following C code to java? thanks so much # include <stdio.h> int main(void) { int changeOwed; int check; char invisibleChar; int count = 0; int numQ=0, numD=0, numN=0, numP=0; do{ printf("How much change is owed (in cents)?\n"); check = scanf("%d", &changeOwed); // returns 1 if the input is a number and returns 0 otherwise //Get's rid of any extra invisible characters if the input is a char/String do{ scanf("%c",&invisibleChar); }while(invisibleChar != '\n'); }while(check == 0...
Convert this C++ to JavaScript and run in browser. #include <cstdlib> #include <ctime> #include <sstream> #include...
Convert this C++ to JavaScript and run in browser. #include <cstdlib> #include <ctime> #include <sstream> #include <iostream> using namespace std; /* * */ class Dice{ private: static const int MAXDICE=6; static const int MINDICE=1; int faceVal; public: Dice(int); void setFace(int); int getFace(); string toString(); }; Dice::Dice(int faceVal) { if(faceVal<MINDICE&&faceVal>MAXDICE) { setFace(1); } else { this->faceVal=faceVal; } } void Dice::setFace(int faceVal) { this->faceVal=faceVal; } int Dice::getFace() { return faceVal; } string Dice::toString() { stringstream ss; ss<<faceVal; string str="face value is ";...
Translate the following C program to Pep/9 assembly language. #include <stdio.h.> int main() { int numitms,j,data,sum;...
Translate the following C program to Pep/9 assembly language. #include <stdio.h.> int main() { int numitms,j,data,sum; scanf("%d", &numitms); sum=0; for (j=1;j<=numitms;j++) { scanf("%d", &data); sum+=data; } printf("sum: %d\n",sum); return0; } Please test the answer using Pep/9. Thank you so much!
q : explain the code for a beginner in c what each line do Question 2....
q : explain the code for a beginner in c what each line do Question 2. The following code defines an array size that sums elements of the defined array through the loop. Analyze the following code, and demonstrate the type of error if found? What we can do to make this code function correctly ? #include <stdio.h> #define A 10 int main(int argc, char** argv) { int Total = 0; int numbers[A]; for (int i=0; i < A; i++)...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT