Question

KKKL1164 4. The C program presented below has some syntax and logic errors. Analyse it and highlight the errors, explaining h

help

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

Code with errors:

#include <stdio.h>
#include <math.h>

int main()
{
   const int a, b, c;
   double doro, disc; // instead of double we may user float as all the format specifiers in code are float type, but if you use double its fine too. 
   
   printf("Lets find the root(s) of a quadratic equation ");
   printf("ax^2 + bx + c.\n");
   printf("Please enter the coefficient a: ");
   scanf("%d", &c); // This is a logical error as we are asking user for value of a and storing the input in variable c
   printf("Please enter the coefficient b: ");
   scanf("%d", &b);
   printf("Please enter the coefficient c: ");
   scanf("%d", &a); // This is a logical error as we are asking user for value of c and storing the input in variable a
   printf("The equation is %dx^2 + %dx + %d = 0\n", a, b, c);
   
   disc = (b * b) - (4.9 * a * c); // formula for discriminant is wrong
   doro = -b / (2.0 * a);
   printf("The discriminant is = %.0f\n", disc); // %.0f will display the value without decimal
   
   if (disc > 0); // if is not terminated with ;, i.e no ; needed after if (condition) {
      printf("The equation has two real roots:\n");
      printf("R1 = %.2f\n", (-b + sqrt(disc)) / (2.0 * a));
      printf("R1 = %.2f\n", (+b - sqrt(disc)) / (2.0 * a)); // formula for root is incorrect
   }
   else if (disc < 0) {
      printf("The equation has two complex roots:\n");
      printf("R1 = %.1f + %.1fi\n", doro, sqrt(-disc) / (2 * a));
      printf("R1 = %.1f - %.1fi\n", doro, sqrt(-disc) / (2 * a));
   }
   // the {} are not closed for else statement
   else { // disc = 0
      printf("The equation has a single root %.1f\n", doro);
   
   return 0;
}

correct code:

#include <stdio.h>
#include <math.h>

int main()
{
   const int a, b, c;
   float doro, disc;

   printf("Lets find the root(s) of a quadratic equation ");
   printf("ax^2 + bx + c.\n");
   printf("Please enter the coefficient a: ");
   scanf("%d", &a);
   printf("Please enter the coefficient b: ");
   scanf("%d", &b);
   printf("Please enter the coefficient c: ");
   scanf("%d", &c);
   printf("The equation is %dx^2 + %dx + %d = 0\n", a, b, c);

   disc = (b * b) - (4.0 * a * c);
   doro = -b / (2.0 * a);
   printf("The discriminant is = %.1f\n", disc);

   if (disc > 0) {
      printf("The equation has two real roots:\n");
      printf("R1 = %.2f\n", (-b + sqrt(disc)) / (2.0 * a));
      printf("R1 = %.2f\n", (-b - sqrt(disc)) / (2.0 * a));
   }
   else if (disc < 0) {
      printf("The equation has two complex roots:\n");
      printf("R1 = %.1f + %.1fi\n", doro, sqrt(-disc) / (2.0 * a));
      printf("R1 = %.1f - %.1fi\n", doro, sqrt(-disc) / (2.0 * a));
   }
   else { // disc = 0
      printf("The equation has a single root %.1f\n", doro);
   }

   return 0;
}


o/p:
Lets find the root(s) of a Please enter the coefficient a: 1 Please enter the coefficient b: 2 Please enter the coefficient cLets find the root(s) of a Please enter the coefficient a: 1 Please enter the coefficient b: 0 Please enter the coefficient cLets find the root (s) of a quadratic equation ax 2 + bx + c. Please enter the coefficient a: 5 Please enter the coefficient

For help please comment.

Thank you

Add a comment
Know the answer?
Add Answer to:
help KKKL1164 4. The C program presented below has some syntax and logic errors. Analyse it...
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
  • In Python. The two roots of a quadratic equation ax^2 + bx + c = 0...

    In Python. The two roots of a quadratic equation ax^2 + bx + c = 0 can be obtained using the following formula: r1 = (-b + sqrt(b^2 - 4ac) / (2a) and r2 = (-b - sqrt(b^2 - 4ac) / (2a) b^2 - 4ac is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one root. If it is negative, the equation has no...

  • /* •   The following code consists of 5 parts. •   Find the errors for each part...

    /* •   The following code consists of 5 parts. •   Find the errors for each part and fix them. •   Explain the errors using comments on top of each part. */ #include <stdio.h> #include <string.h> int main( ) { ////////////////////////////////////////////////////////////////////////// //////////////        Part A. (5 points)                ////////////////// int g(void) { printf("%s", Inside function g\ n " ); int h(void) {       printf(" % s ", Inside function h\ n "); } } printf("Part A: \n "); g(); printf(" \n"); ////////////////////////////////////////////////////////////////////////// //////////////       ...

  • Using C++ For this program you are going to create a class that helps to solve...

    Using C++ For this program you are going to create a class that helps to solve the quadratic equation. The equation has the following form: ax2 + bx + c = 0 The roots of the equation are: x1 = -b + sqrt(b2 - 4 * a * c) and x2 = -b - sqrt(b2 - 4 * a * c) The phrase in the parenthesis is called the discriminant. If the value of the discriminant is positive, the equation...

  • Java Programming Question. I am writing a code to calculate the roots of the quadratic equation...

    Java Programming Question. I am writing a code to calculate the roots of the quadratic equation based on the values of the coefficients A, B, and C. I am supposed to enter 5 values for each coefficient, and the output should be five different answers (see example) using a for loop and if else statements. However, when I run my code, I am only able to enter one value for each coefficient and the output is one answer repeated five...

  • Write a complete C program. Define a structure type element_t to represent one element from the...

    Write a complete C program. Define a structure type element_t to represent one element from the periodictable of elements. Components should include the atomic number (aninteger); the name, chemical symbol, and class (strings); a numeric field forthe atomic weight; and a seven-element array of integers for the number ofelectrons in each shell. The following are the components of an element_t structure for sodium.11 Sodium Na alkali_metal 22.9898 2 8 1 0 0 0 0 Have the user enter the data...

  • Identify the errors in this C program (struct - calculating volume, area of cylinder). Explain. #include...

    Identify the errors in this C program (struct - calculating volume, area of cylinder). Explain. #include <stdio.h> #define PI 3.14 typedef struct cylinder{    float r,h;    float areacircle=2*PI*r;    float areacylinder;    float volumecylinder;    areacylinder=2*PI*r*h+2*PI*r*r;    volumecylinder=PI*r*r*h; }; int main() {    float start,numcyls;    printf("How many cylinders do you want? Enter a positive integer.\n");    scanf("%f",&numcyls);    if(numcyls<1||ceilf(numcyls)!=numcyls)    while()    {printf("The number you have entered is not a positive integer. Please try again.\n");        printf("Remember,...

  • C++ HELP I need help with this program. I have done and compiled this program in...

    C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. I am using printf and scanf. Instead of printf and scanf use cin and cout to make the program run. after this please split this program in three files 1. bill.h = contains the class program with methods and variables eg of class file class bill { } 2. bill.cpp = contains the functions from class...

  • Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int...

    Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int isFull(int *, int); void removeAt(int *, int); void printList(int *, int); int main() { int *a; int arraySize=0,l=0,loc=0; int choice; while(1) { printf("\n Main Menu"); printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of...

  • C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. But I need to split this program in three files 1. bill.h = contains the...

    C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. But I need to split this program in three files 1. bill.h = contains the class program with methods and variables 2. bill.cpp = contains the functions from class file 3. main.cpp = contains the main program. Please split this program into three files and make the program run. I have posted the code here. #include<iostream>...

  • So I have a question in regards to my program. I'm learning to program in C...

    So I have a question in regards to my program. I'm learning to program in C and I was curious about the use of functions. We don't get into them in this class but I wanted to see how they work. Here is my program and I would like to create a function for each of my menu options. I created a function to display and read the menu and the option that is entered, but I would like to...

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