Question

In: Computer Science

My current code that is not working correctly #include<stdio.h> #include<math.h> int main() { //variable declaration int...

My current code that is not working correctly

#include<stdio.h>

#include<math.h>

int main() {

//variable declaration
int a,b,c,D,n;
double x1,x2;
double realPart, imagPart;

do {
// this set of code blocks prompts a message to the user and then read the integer entered and stores it as an integer
printf("Enter a value for a:\n");
scanf("%d",&a);
printf("Enter a value for b:\n");
scanf("%d",&b);
printf("Enter a value for c:\n");
scanf("%d",&c);
printf("You entered the Equation \n");
printf("%dx^2%+dx%+d=0\n",a,b,c);

D = b*b - 4*a*c;
  

if (D<0)
{
printf("Equation has two complex roots.\n");
realPart = -b/(double)(2*a);
imagPart = sqrt(-D)/(2*a);
printf("X= %.1lf%lfi or X= %.1lf%lfi\n", realPart, + imagPart, realPart, imagPart);
}
else { //real roots
x1 = (-b + sqrt(D))/(2*a);
x2 = (-b - sqrt(D))/(2*a);
if(x1==x2){
printf("Equation has two identical real roots.\n");
printf("x = %.2lf\n",x1);
}
else{
printf("X= %.1lf or X= %.2lf\n", x1, x2);
printf("Equation has two distinct real roots.\n");

}
  
}
printf("Another Equation? (Enter 0 to exit)...");
scanf("%d",&n);
}
while(n!=0);
}
WHAT THE CODE NEEDS TO DO IN C LANGUAGE ONLY

Write a program that determines whether a quadratic equation has one real root, two real roots, or imaginary roots. Then, if the roots are real, display the roots of the quadratic equation.

A quadratic equation is of the form ax^2+bx+c=0.

The discriminant of the equation is D=b^2-4ac. If D>0, then there are two distinct real roots. If D=0, then there are two identical real roots, and if D<0, then there are two imaginary roots.

The roots of the equation are found using the quadratic equation:

  • Two Real Roots, D>0
    • x1,x2 = ( -b ± √(b^2-4ac) ) / (2a)
  • One Real Root, D=0
    • x = (-b) / (2a)
  • Two Imaginary Roots, D<0
    • x1,x2 = ( -b±i√( | b^2-4ac | ) ) / (2a)
      i=√(-1)

Procedure

  • Include the usual (detailed) comment block including program name, author, date, inputs, outputs and description, followed by your preprocessor directives.
  • After declaring your variables, explain the program to the user by printing a description to the monitor.
  • Call a function that prompts the user for one of the values of the coefficients (a, b, or c) for the quadratic equation and returns the value entered, with error checking for a valid input (scanf returned a value). Note: this should be one function that gets called three times. The printf asking for the variable by name can be in main.
  • Call a function to calculate the discriminant D.
  • Use conditional statements to display the type of roots (one real, two real, or imaginary)
  • Depending on the type of roots, use the appropriate form of the quadratic equation to calculate the roots of the equation and display them. For imaginary roots, you will need to calculate the real and imaginary parts separately and print them with the imaginary number i as part of the format. (Note: this means that you are doing real math. You don't need to figure out how to do imaginary math in C. Try calculating the last example below by hand to understand. When printing the imaginary roots, treat "i" simply as a letter/character to be displayed.)
  • At the end of your program, offer the user the option to run again or exit.
  • Do not forget to comment, use blank lines, and employ correct indentation.

Solutions

Expert Solution

Corrected code :

#include<stdio.h>

#include<math.h>

int main() {

//variable declaration
int a,b,c,D,n;
double x1,x2;
double realPart, imagPart;

do {
        // this set of code blocks prompts a message to the user and then read the integer entered and stores it as an integer
        printf("Enter a value for a:\n");
        scanf("%d",&a);
        printf("Enter a value for b:\n");
        scanf("%d",&b);
        printf("Enter a value for c:\n");
        scanf("%d",&c);
        printf("You entered the Equation \n");
        printf("%dx^2%+dx%+d=0\n",a,b,c);

        D = b*b - 4*a*c;
          

        if (D<0)
        {
                printf("Equation has two complex roots.\n");
                realPart = -b/(double)(2*a);
                imagPart = sqrt(-D)/(2*a);
                printf("X= %.1lf+%lfi or X= %.1lf-%lfi\n", realPart,  imagPart, realPart, imagPart);
        }
        else{ //real roots
                        x1 = (-b + sqrt(D))/(2*a);
                        x2 = (-b - sqrt(D))/(2*a);

                if (D==0){ //identical real roots
                
                        printf("Equation has two identical real roots.\n");
                        printf("x = %.2lf\n",x1);
                        }
                else{ // 2 distict real roots
                        printf("X= %.1lf or X= %.2lf\n", x1, x2);
                        printf("Equation has two distinct real roots.\n");
                }
          }
        printf("Another Equation? (Enter 0 to exit)...");
        scanf("%d",&n);
}
while(n!=0);
}

I have made the required corrections to your code and it is now working correctly.

Output :

Complex roots:

Identical real roots:

Identical distinct roots:

Screenshot of the code is attached so that you can understand the intendations.


Related Solutions

my code is not printing the output #include <stdio.h> #include<stdbool.h> int main(){ // variable declarations   bool...
my code is not printing the output #include <stdio.h> #include<stdbool.h> int main(){ // variable declarations   bool binary[12] = {false,false, false, false,false,false,false,false,false,false,false, false};   int powerTwo[12] = {2048.1028,576, 256, 128, 64, 32, 16, 8 , 4, 2, 1};   int oneZero[9]= {0,0,0,0,0,0,0,0,0};   int tempVal = 0;   double decimal = 0;   int i = 0;   int j = 0;   // Intialization   printf("Starting the CPSC 1011 Decimal to Binary Converter!\n");   printf("Please enter a positive whole number (or EOF to quit): \n");   scanf("%lf", &decimal);   printf("%lf", decimal);...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main(...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main( int argc, char* argv[] ) { // Size of vectors int n = 10000; // Input vectors double *restrict a; double *restrict b; // Output vector double *restrict c; // Size, in bytes, of each vector size_t bytes = n*sizeof(double); /* Q1: Allocate memory for vector a (10 points)*/ /* Q2: Allocate memory for vector b (10 points)*/ /* Q3: Allocate memory for vector...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x = 3;     i = fun(x);     printf("%d\n", i);     return 0; } int fun(int i) {      int res = 0;      res = pow (i , 3.0);      return ( res); }
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter between 8 to 60 inches: "); scanf("%d",&diameter); // if(diameter>60 || diameter<=8){ // printf("Error! invalid input"); // exit(0); // } // else{ // float radius = diameter/2; // float volume = (4/3)*PI*radius*radius*radius; // printf("%.2f",volume); // } //check through the while loop if it is valid or in valid while(diameter>60 || diameter<=8){ printf("Invalid input Enter again: "); scanf("%d",&diameter); }    //caluclate the volume of sphere float...
#include <stdio.h> #include<math.h> int main (void). { int I, j, k, num_elem; double x[20], y[20],z[20]; FILE*infile,...
#include <stdio.h> #include<math.h> int main (void). { int I, j, k, num_elem; double x[20], y[20],z[20]; FILE*infile, *outfile; infile = fopen (“c6_3.IN”, “r”); outfile = fopen (“c6_3.OUT”, “w”); k = fscanf (infile, “%lf %lf”, &x[0], &y[0]); fprintf (outfile,”k= %d\n”,k); fprintf (outfile, “value of EOF = %d\n”, EOF); i =1; while ( fscanf(infile, “%lf %lf”, &x[i], &y[i]) != EOF) i++; num_elem =I; fprintf(outfile,” x[i] y[i] z[i]\n”); For (j=0; j<num_elem; j++) { Z[j] =sqrt(x[j] *x[j] +y[j]*y[j]); fprintf(outfile,”%7.1f %7.1f %7.1f\n”, x[j] , y[j], z[j]);...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc != 2) || (sscanf(argv[1],"%d",&count) != 1)) { fprintf(stderr,"Usage: %s <integer>\n", argv[0]); exit(1); } pid_t pid1, pid2; while (count > 0) { pid1 = fork(); if (pid1 > 0) { pid2 = fork(); count = count - 2; } else if (pid1 == 0) { count = count - 1; } } exit(0); } Question #1 [2 pts] If the command-line argument passed to this...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double fact (double); void main () { int angle_in_D; double term, angle_in_R; float sine = 0; unsigned int i = 1; double sign = 1; int n = 1000; printf ("Please enter an angle (Unit: Degree): "); scanf ("%d", &angle_in_D); angle_in_R = angle_in_D * M_PI / 180.0; do { term = pow(-1,(i-1)) * pow (angle_in_R, (2*i - 1)) / fact (2*i - 1); sine =...
#include <stdio.h> int main () { int value= 10; int value2= 5; value = value %2;...
#include <stdio.h> int main () { int value= 10; int value2= 5; value = value %2; printf("he FInal =value=%d\n", value); value +=3; printf("he FInal =value=%d\n", value); value ++; printf("he FInal =value=%d\n", value); value= ++value2; printf("he FInal =value=%d\n", value); value= value2--; printf("he FInal =value=%d\n", value); } what is output explain each print statement? exlain why?
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;     char fname[20];     //int sum = 0;     int i, j, k, tmp =0;     int num = 0;     int mass = 0;     int count = 0;     int fuel = 0;     int total = 0;     int M[1000];     char ch;     char buffer[32];     printf(" Input the filename to be opened : ");     scanf("%s",fname);     myFile = fopen(fname, "r");     if(myFile == NULL)     {         printf("Can't open file\n");     } while(1)     {         ch =...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating();...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating(); int main() { // declaring variables int size = 5; int jerseyNo[size]; int rating[size]; int i = 0, jno, rate; char option; /* Getting the inputs entered by the user * and populate the values into arrays */ for (i = 0; i < size; i++) { printf("Enter player %d's jersey number:", i + 1); jerseyNo[i] = getValidJerseyNumber(); printf("Enter player %d's rating:\n", i +...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT