Question

CAN ANYONE HELP ME PLEASE! THUMBS UP WILL BE GIVEN!! THANKS2.1 Use a text editor to enter the program on textbook page 47 (the code is given here for your convenience). Use GNU GCC to compile, debug (find and fix any syntax errors), and execute the program and fix any semantic errors, for example, by adding break statements in the required places and by fixing incorrect characters copied into the programming environment, if any, and by type-changing to print a floating point number. The code is supposed to perform one math operation in each switch-case. The ‘ch variable is assigned a new math operator before each switch-case. Submit the corrected program as hw01q2.c [8 points] /* This C program demonstrates the switch statement without using breaks. */ #include <stdio.h> main) char ch int a 10, b20; double f; printf(ch-%c , ch); switch (ch) ( case +: f- a + b; printf(f-%d , case -: f- a - b; printf(f-%d , case *: f- a * b; printf(f-%d , case /: f- a / b; printf(f-%d , default: printf(invalid operator ); f); f); f); f); printf(ch-%c , ch); switch (ch) ( case +: f- a + b; printf(f-%d , case -: f- a - b; printf(f-%d , case *: f- a * b; printf(f-%d , case /: f- a / b; printf(f-%d , default: printf(invalid operator ); f); f); f); f); printf(ch-%c , ch); switch (ch) ( case +: f- a + b; printf(f-%d , case -: f- a - b; printf(f-%d , f); fcase : f - a * b; printf(f-%d\n, case ./. : f -a / b; printf(f-%d\n, default: printf(invalid operator\n); f); f); ch printf(ch-%c\n, ch); switch (ch) case +: f a + b; printf( f-%d\n, case -: f a - b; printf(f-%d\n, case : f - a * b; printf(f-%d\n, case ./: f -a / b; printf(f-%d\n, default: printf(invalid operator\n); f); f); f); f); printf(ch-%c\n, ch); switch (ch) { case +: f a + b; printf(f-%d\n, case -: f a - b; printf(f-%d\n, case : f a * b; printf(f-%d\n, case /. : f a / b; printf(f-%d\n, default: printf(invalid operator\n); f); f); f); f);

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* I think you want to change the provided code so that it prints only one math operation per switch statement, if this isn't the case do let me know in the comment section. */

#include <stdio.h>

int main() {
   char ch = '+';
   int a = 10, b = 20;
   double f;
   switch (ch) {
       case '+': f = a + b; printf("f = %f ", (float)f); break;
       case '-': f = a - b; printf("f = %f ", (float)f); break;
       case '*': f = a * b; printf("f = %f ", (float)f); break;
       case '/': f = a / b; printf("f = %f ", (float)f); break;

       default: printf("invalid operator ");
   }
   ch = '-';
   printf("ch = %c ", ch);
   switch (ch) {
       case '+': f = a + b; printf("f = %f ", (float)f); break;
       case '-': f = a - b; printf("f = %f ", (float)f); break;
       case '*': f = a * b; printf("f = %f ", (float)f); break;
       case '/': f = a / b; printf("f = %f ", (float)f); break;
       default: printf("invalid operator ");
   }
   ch = '*';
   printf("ch = %c ", ch);
   switch (ch) {
       case '+': f = a + b; printf("f = %f ", (float)f); break;
       case '-': f = a - b; printf("f = %f ", (float)f); break;
       case '*': f = a * b; printf("f = %f ", (float)f); break;
       case '/': f = a / b; printf("f = %f ", (float)f); break;
       default: printf("invalid operator ");
   }
   ch = '/';
   printf("ch = %c ", ch);
   switch (ch) {
       case '+': f = a + b; printf("f = %f ", (float)f); break;
       case '-': f = a - b; printf("f = %f ", (float)f); break;
       case '*': f = a * b; printf("f = %f ", (float)f); break;
       case '/': f = a / b; printf("f = %f ", (float)f); break;
       default: printf("invalid operator ");
   }
   ch = '%';
   printf("ch = %c ", ch);
   switch (ch) {
       case '+': f = a + b; printf("f = %f ", (float)f); break;
       case '-': f = a - b; printf("f = %f ", (float)f); break;
       case '*': f = a * b; printf("f = %f ", (float)f); break;
       case '/': f = a / b; printf("f = %f ", (float)f); break;
       default: printf("invalid operator ");
   }

   return 0;
}

hw01q2.c 1 #include<stdio.h> 3 int main() char ch int a-10, b20; double f; switch (ch) 6 8 9 10 case +: f a + b; printf(f %f\n, case : f a - b; printf(f %f\n, case *: f a * b; printf(f %f\n, case /: f a / b; printf(f %f\n, default: printf ( invalid operator\n) (float ) f ) ; (float ) f); (float ) f ) ; (float ) f); break; break; break; break; 12 13 14 15 16 17 18 19 20 21 printf(ch-sc\n, ch) switch (ch) case +: f a + b; printf(f %f\n, case : f a - b; printf(f %f\n, case *: f a * b; printf(f %f\n, case /: f a / b; printf(f %f\n, default: printf ( invalid operator\n) (float ) f); (float ) f ) ; (float ) f ); (float ) f); break; break; break; break; 24 25 26 27 28 29 30 31 32 printf(ch-sc\n, ch) switch (ch) case +: f a + b; printf(f %f\n, case f a - b; printf(f %f\n, case *: f = a * b; printf(f =%f\n, case /: f a / b; printf(f %f\n, default: printf ( invalid operator\n) (float ) f ) ; (float ) f ) ; (float ) f ); (float ) f); break; break; break; break; printf(ch-sc\n, ch) switch (ch) 34 35 36 37 38 39 40 41 42 43 case +: f a + b; printf(f %f\n, case f a - b; printf(f %f\n, case *: f a * b; printf(f %f\n, case /: f a / b; printf(f %f\n, default: printf ( invalid operator\n) (float ) f ) ; (float ) f); (float ) f ); (float ) f ) ; break; break; break; break; printf(ch-sc\n, ch) switch (ch) case +: f a + b; printf(f %f\n, case : f a - b; printf(f %f\n, case *: f a * b; printf(f %f\n, case /: f a / b; printf(f %f\n, default: printf ( invalid operator\n) (float ) f ) ; (float ) f); (float ) f ) ; (float ) f); break; break; break; break; 45 46 47 48 49 50 51 52 return 0

OUTPUT

adnanli@adnanli~/Desktop$ make hw01q2 adnanli@adnanli/Desktop$ ./hw01q2 f 30.000000 ch f - 10.000000 f = 200.000000 ch=1 f = 0.000000 invalid operator adnanli@adnanli:-/Desktops

Add a comment
Know the answer?
Add Answer to:
CAN ANYONE HELP ME PLEASE! THUMBS UP WILL BE GIVEN!! THANKS 2.1 Use a text editor...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here....

    #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here. */ int GetNumOfNonWSCharacters(const char usrStr[]) { int length; int i; int count = 0; char c; length=strlen(usrStr); for (i = 0; i < length; i++) { c=usrStr[i]; if ( c!=' ' ) { count++; } }    return count; } int GetNumOfWords(const char usrStr[]) { int counted = 0; // result // state: const char* it = usrStr; int inword = 0; do switch(*it)...

  • I need help solving this assignment, you are given a full piece of code that does not run correctly. Some of the bugs ca...

    I need help solving this assignment, you are given a full piece of code that does not run correctly. Some of the bugs can be found at compile time, while others are found at run time. Access the UserMenu.cpp code file (in the zip file in Start Here) and use debugging practices and the debugger in Visual Studio to find each bug. Fix the bug and write a comment in the code giving a description of how you found the...

  • 6-1 Test and debug the Invoice Application i need help with this please.. all help is...

    6-1 Test and debug the Invoice Application i need help with this please.. all help is appreciated Source code Java: import java.util.Scanner; import java.text.NumberFormat; public class InvoiceApp { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String choice = "y"; while (!choice.equalsIgnoreCase("n")) { // get the input from the user System.out.print("Enter customer type (r/c): "); String customerType = sc.next(); System.out.print("Enter subtotal: "); double subtotal = sc.nextDouble(); // get the discount percent double discountPercent = 0.0; switch(customerType) {...

  • please help me write function definition of this code ..thanks /* Test change_case function. */ #include...

    please help me write function definition of this code ..thanks /* Test change_case function. */ #include "nodep.h" //suppress deprecation warnings #include <stdio.h> //printf(), scanf() #include <conio.h> //getch() #define _CRT_SECURE_NO_DEPRECATE 1 #define _CRT_NONSTDC_NO_DEPRECATE 1 #define ESC 27 #define LETTER_a 97 #define LETTER_z 122 #define LETTER_A 65 #define LETTER_Z 90 //function prototype: char change_case(char cIn); int main(void)    {    char cInput, cOutput;    printf("Test change_case function...\n\n");    printf("(Press ESC to exit, any other key to keep testing)\n");    do       {       printf("\n\nEnter letter: ");       cInput = _getche();      ...

  • int main() { int a, Band3 = 0, Band1, Band2, b, c = 1; float t;...

    int main() { int a, Band3 = 0, Band1, Band2, b, c = 1; float t; while (c) { Band3 = 0; printf(" Enter Resistance Value: "); scanf_s("%d", &a); printf("\n Enter Tolerance Value: "); scanf_s("%f", &t); b = t * 100; while (a % 10 == 0) { Band3++; a = a / 10; } Band2 = a % 10; a = a / 10; Band1 = a % 10; printf("\n Band1 = %d, Band2 = %d, Band3 = %d...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

  • please help me fix this. I'm getting it all mixed up in user defined functions. here's...

    please help me fix this. I'm getting it all mixed up in user defined functions. here's the question. This week we'll write a C program that lets the user play the game of Rock, Paper, Scissors against the computer. Your program will call a user-defined function named display_rules that displays the rules of the game. It then proceeds to play the game. To determine the winner, your program will call another user- defined function called determine_winner that takes two integer...

  • Could use some help with this, Instructions: You will need to add an input statement for...

    Could use some help with this, Instructions: You will need to add an input statement for the grade variable to this code. Also add a for loop so that you can enter more than one grade. Code the for loop so that at least 7 grades can be entered. #include <iostream> #include <string> using namespace std; void main() {      char grade;           for (int x = 0; x < 7; x++)      {            cout << "Enter a...

  • The first project involves modifying the attached lexical analyzer and the compilation listing ge...

    The first project involves modifying the attached lexical analyzer and the compilation listing generator code. You need to make the following modifications to the lexical analyzer, scanner.l: 1. A second type of comment should be added that begins with // and ends with the end of line. As with the existing comment, no token should be returned. 2. The definition for the identifiers should be modified so that underscores can be included, however, consecutive underscores, leading and trailing underscores should...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT