Question
write a C programming code for the following prompt. please use stucture concept and test if the code works perfectly.
Project Description: In this project, you will write a program to calculate the roots of a quadratic equation. Structure conc
0 0
Add a comment Improve this question Transcribed image text
Answer #1


Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you

#include <stdio.h>
#include <math.h>
typedef struct {
   double a;
   double b;
   double c;
}coefficientType;

typedef struct {
   double realRoots;
   double root1;
   double root2;
} rootsType;

rootsType computeRoots(coefficientType coeff);
void displayRoots(rootsType roots);


int main(){
   coefficientType coeff;
   rootsType roots;
  
   printf("Enter the coffiecients of the quadratic equation: \n");
  
   do{
       printf("a: ");
       scanf("%lf", &coeff.a);
   }while(coeff.a == 0);
  
   printf("b: ");
   scanf("%lf", &coeff.b);
  
   printf("c: ");
   scanf("%lf", &coeff.c);
  
   roots = computeRoots(coeff);
   displayRoots(roots);

}

rootsType computeRoots(coefficientType coeff){
   double D = coeff.b * coeff.b - 4 * coeff.a * coeff.c;
   rootsType roots;
  
   if(D < 0){
       roots.realRoots = 0;
       D = -D;
       roots.root1 = -coeff.b/(2*coeff.a);
       roots.root2 = sqrt(D)/ (2*coeff.a);
   }
   else{
       roots.realRoots = 1;
       roots.root1 = (-coeff.b + sqrt(D))/(2*coeff.a);
       roots.root2 = (-coeff.b - sqrt(D))/(2*coeff.a);
   }
   return roots;
}

void displayRoots(rootsType roots){
   if(roots.realRoots == 1){
       printf("The equation has real roots\n");
       printf("Root1: %.4lf\n", roots.root1);
       printf("Root2: %.4lf\n", roots.root2);
   }
   else{
       printf("The equation has complex roots\n");
       printf("Root1: %.4lf + %.4lf i\n", roots.root1, roots.root2);
       printf("Root2: %.4lf - %.4lf i\n", roots.root1, roots.root2);
   }
  
}

amoeba-2:Test2 rajis ./a.out Enter the coffiecients of the quadratic equation: a: 2 b: 8 C: 3 The equation has real roots Roo

Add a comment
Know the answer?
Add Answer to:
write a C programming code for the following prompt. please use stucture concept and test if...
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
  • I need help writing the test case in C++ code can you help? This is apart...

    I need help writing the test case in C++ code can you help? This is apart of my professors pseudo code for the project. // TEST CASE 1 // // DESCRIPTION // Coefficients a and c are > 0 and b < 0 so the equation should be output as: // // p(x) = 122.50000x^2 - 6.70000x + 3.00000 = 0 // // where we note that the operator printed before b is subtraction and we actually output -b rather...

  • A quadratic equation is generally represented as, ax^2 + bx + c The root(s) of the...

    A quadratic equation is generally represented as, ax^2 + bx + c The root(s) of the above quadratic equation is computed using the following formula, root1 = (-b + sqrt(D))/ 2a root2 = (-b - sqrt(D))/2a Where D is the discriminant of the quadratic equation and is computed as, D = b^2 - 4ac Given the value of D, the roots of a quadratic equation can be categorized as follows, D > 0 : Two distinct real roots D =...

  • The roots of the quadratic equation ax2 + bx + c = 0, a following formula:...

    The roots of the quadratic equation ax2 + bx + c = 0, a following formula: 0 are given by the In this formula, the term i2 - 4ac is called the discriminant. If b4ac 0 then the equation has a single (repeated) root. If -4ac > 0, th equation complex roots. Write a program that prompts the user to input the value of a (the coefficient of ), b (the coefficient of x), and c (the n has two...

  • CODE MUST BE IN C++ Objective Create a program that provides the solution to a quadratic...

    CODE MUST BE IN C++ Objective Create a program that provides the solution to a quadratic equation. Background: A quadratic equation can be generalized by equation below: f ( x )=ax2+ bx +c Closed form solutions can be found for the zeros of a quadratic function conveniently. That is the locations where the function is equal to zero can be found by the following equation: x=− b± √ b −4ac 2a Note that depending on the sign of the expression...

  • Use C++, visual studio format. Please Write a program to solve the quadratic equation and find...

    Use C++, visual studio format. Please Write a program to solve the quadratic equation and find only the real and equal roots. the program should test for three types of roots tell the users the roots type and then calculate the real roots. the user enters the values of a ,b and c. you calculate r1 and r2. c

  • DQuestion 19 28 pts Problem 2. Quadratic Equations A quadratic equation has the form: a bc0 The two solutions are given by the formula: 2a Write a program with a loop that a) solves quadratic equatio...

    DQuestion 19 28 pts Problem 2. Quadratic Equations A quadratic equation has the form: a bc0 The two solutions are given by the formula: 2a Write a program with a loop that a) solves quadratic equations with coefficients read from the terminal, b) visualizes the corresponding quadratic function az2 brc0using the matplotlib module. Instructions Write all code for this problem in a file p2.py 1. The program consists of a main whtle loop (an infinite loop) in which the user...

  • This is a C++ programming question. Please provide the correct, workable code. Use the following three...

    This is a C++ programming question. Please provide the correct, workable code. Use the following three programs to help solve the problem. Provide comments throughout the code. Problem to solve: Code 1: Complex.h #pragma once #ifndef COMPLEX_H #define COMPLEX_H class Complex { private:    double real;    double imag; public:    // initialize the complex number to 0.0    Complex() : real(0.0), imag(0.0) {}    // initialize the complex number at declaration or new    Complex(double r, double i) :...

  • C program that uses pointers as function arguments to do the following: To exemplify pointers, we...

    C program that uses pointers as function arguments to do the following: To exemplify pointers, we will be doing quadratics. Remember that a quadratic expression is of the form: ax2 + bx + c where a. b, c are constant and a is not 0. You will scan in the values a. b. and c. With these values, you will write three functions: quadraticFormula quadraticVertex quadratic Info The first function will perform the quadratic equation to find the roots of...

  • Object Oriented Programming Please write the code in C++ Please answer the question in text form...

    Object Oriented Programming Please write the code in C++ Please answer the question in text form 9.5 (complex Class) Create a class called complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form where i is V-I Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in...

  • c++ language TECH 1211 Computer Programming Name Test 2 Hands-On-Program B Spring 2020 Write a program...

    c++ language TECH 1211 Computer Programming Name Test 2 Hands-On-Program B Spring 2020 Write a program that uses the quadratic formula to solve quadratic equations. Background Given the format for a quadratic equations: aX? +BX+C =0 The coefficient of the X term is the number a. The coefficient of the X term is the number b. The value of c is any non-zero constant. These values can be positive, negative or zero. You can find the solution to this equation...

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