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...
The code is in C programming language pls convert it into python. Thanks. Program --> #include...
The code is in C programming language pls convert it into python. Thanks. Program --> #include <stdio.h> #include <stdlib.h> void main() { //declare variables FILE *fileptr; char filename[15]; char charRead; char filedata[200],searchString[50]; int i=0,j=0,countNoOfWord=0,count=0; //enter the filename to be opened printf("Enter the filename to be opened \n"); scanf("%s", filename); /* open the file for reading */ fileptr = fopen(filename, "r"); //check file exit if (fileptr == NULL) { printf("Cannot open file \n"); exit(0); } charRead = fgetc(fileptr); //read the string...
create case 4 #include <stdio.h> int main(void) { int counter; int choice; FILE *fp; char item[100];...
create case 4 #include <stdio.h> int main(void) { int counter; int choice; FILE *fp; char item[100]; while(1) { printf("Welcome to my shopping list\n\n"); printf("Main Menu:\n"); printf("1. Add to list\n"); printf("2. Print List\n"); printf("3. Delete List\n"); printf("4. Remove an item from the List\n"); printf("5. Exit\n\n"); scanf("%i", &choice); switch(choice) { case 1: //add to list //get the input from the user printf("Enter item: "); scanf("%s", item); //open the file fp = fopen("list.txt","a"); //write to the file fprintf(fp, "\n%s", item); //close the file...
#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) {...
C Code! dictionary.c, start by downloading the boilerplate1 code from Blackboard. Read the code, then complete...
C Code! dictionary.c, start by downloading the boilerplate1 code from Blackboard. Read the code, then complete it at all places indicated by TODO. For this second assignment, you do not receive precise specifications. This is on purpose: in the software industry, you’ll find a lot of cases when you need to finish code started by others, while not being given precise instructions on how the code works or is supposed to work. The comments in the code will help you,...
#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...
Error compiler. need fix code for febonacci Iterative like 0 1 1 2 3 5 8...
Error compiler. need fix code for febonacci Iterative like 0 1 1 2 3 5 8 13 21 34 55 ....... and also need add code complexity time if you want! here code.c --------------------------------------------------------------------------------------------------------------------- #include <stdio.h> #include <math.h> int fibonacciIterative( int n ) { int fib[ n + 1 ]; int i; fib[ 0 ] = 0; fib[ 1 ] = 1; for ( i = 2; i < n; i++ ) { fib[ i ] = fib[ i -...
don't understand why this code outputs 3. Why doesn't it output 32?? #include <stdio.h> #include <ctype.h>...
don't understand why this code outputs 3. Why doesn't it output 32?? #include <stdio.h> #include <ctype.h> int main() { printf("%d\n", parseint("32")); return 0; } int parseint(char *str) { int i=0; while(str[i] != '\0'){ return (str[i] - '0'); i++; } return 1;    }
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT