
Concept explainers
There is an error in this code, and its making it stop completing the program properly after the do-while loop in main. My problem is that for some reason coeffs[C_INDEX] isn't reading in properly, and it just gives some random output, but a and b work fine. How can I fix this?
#include <iostream>
#include <cmath>
using namespace std;
const int X1_INDEX= 1;
const int X2_INDEX= 0;
const int A_INDEX = 0;
const int B_INDEX = 1;
const int ROOTS = 2;
const int C_INDEX = 3;
const int COEFFS = 3;
void GetCoefficients(double& a, double& b, double& c);
// Get the coefficients for the Quadratic equation.
// @param a, b, c reads the coefficients
static bool SolveQuadratic(double a, double b, double c,double *px1, double *px2);
// Solve the quadratic equation represented by a, b, c as coefficients
// @param a, b, c are the coefficients
// @param px1, px2 are the pointers to the roots
// @return true if the roots are real or false when imaginary.
static void DisplayRoots(double x1, double x2, bool isReal);
// Display the roots using isReal to determine how to print.
int main() {
char retry;
do {
// Using arrays to store coeffs and solutions.
double coeffs[COEFFS], solutions[ROOTS];
double *px1 = &solutions[X1_INDEX]; // Use pointers in SolveQuadratic
double *px2 = &solutions[X2_INDEX];
bool isReal = true;
// Read the coefficients
GetCoefficients(coeffs[A_INDEX], coeffs[B_INDEX], coeffs[C_INDEX]);
// Solve the equation
isReal = SolveQuadratic(coeffs[A_INDEX], coeffs[B_INDEX], coeffs[C_INDEX],
px1, px2);
// Print the results.
DisplayRoots(solutions[X1_INDEX], solutions[X2_INDEX], isReal);
cout << "Try again (y or Y): ";
cin >> retry;
} while (retry == 'Y' || retry == 'y');
return 0;
}
// GetCoefficient reads the coefficients
// @param a, b, c reads the coefficients
void GetCoefficients(double& a, double& b, double& c)
{
cout << "Enter coefficients a, b and c: ";
cin >> a >> b >> c;
}
// SolveQuadratic solves the quadratic equation and returns the roots
// @param a, b, c are the coefficients
// @param px1, px2 are the pointers to the roots
// @return true if the roots are real or false when imaginary.
bool SolveQuadratic(double a, double b, double c, double* px1, double* px2)
{
double discriminant = b * b - 4 * a*c;
// Two different roots for this equation
if (discriminant > 0) {
*px1 = (-b + sqrt(discriminant)) / (2 * a);
*px2 = (-b - sqrt(discriminant)) / (2 * a);
return true;
}
// One root satisfies both
else if (discriminant == 0) {
*(px1) = (-b + sqrt(discriminant)) / (2 * a);
*px2 = *px1;
return true;
}
// Read part in px1, imaginary part in px2.
else {
*px1 = -b / (2 * a);
*px2 = sqrt(-discriminant) / (2 * a);
return false;
}
}
//
// DisplayRoots - display 2 roots using information of real and imaginary.
// @param x1 - always contains a root.
// @param x2 - only contains a root when different or isReal false.
// @param isReal - tells whether roots are real or imaginary.
static void DisplayRoots(double x1, double x2, bool isReal)
{
if (isReal) {
if (x1 != x2) {
cout << "Roots are real and different." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}
else if (x1 == x2) {
cout << "Roots are real and same." << endl;
cout << "x1 = x2 =" << x1 << endl;
}
}
else {
cout << "Roots are complex and different." << endl;
cout << "x1 = " << x1 << "+" << x2 << "i" << endl;
cout << "x2 = " << x1 << "-" << x2 << "i" << endl;
}
}

Trending nowThis is a popular solution!
Step by stepSolved in 2 steps with 1 images

- Modify the swap_string function in this task6.c to swap two strings. task6.c Swapping string Only modify swap_string() function and swap_string() call in the main, nothing else should be changed. */ #include <stdio.h> #include <string.h> void swap_string(char *s1, char *s2){ char *tmp = s1; s1=s2; s2=tmp; return; } int main() { char *string1 = "UNIX rules!"; char *string2 = "Windows drools!"; swap_string(string1, string2); printf("String 1: %s\n", string1); printf("String 2: %s\n", string2); return0; }arrow_forwardpython: def character_gryffindor(character_list):"""Question 1You are given a list of characters in Harry Potter.Imagine you are Minerva McGonagall, and you need to pick the students from yourown house, which is Gryffindor, from the list.To do so ...- THIS MUST BE DONE IN ONE LINE- First, remove the duplicate names in the list provided- Then, remove the students that are not in Gryffindor- Finally, sort the list of students by their first name- Don't forget to return the resulting list of names!Args:character_list (list)Returns:list>>> character_gryffindor(["Scorpius Malfoy, Slytherin", "Harry Potter,Gryffindor", "Cedric Diggory, Hufflepuff", "Ronald Weasley, Gryffindor", "LunaLovegood, Ravenclaw"])['Harry Potter, Gryffindor', 'Ronald Weasley, Gryffindor']>>> character_gryffindor(["Hermione Granger, Gryffindor", "Hermione Granger,Gryffindor", "Cedric Diggory, Hufflepuff", "Sirius Black, Gryffindor", "JamesPotter, Gryffindor"])['Hermione Granger, Gryffindor', 'James…arrow_forwardIn python don't import librariesarrow_forward
- PLEASE DO NOT USE BUILT IN FILES OR FUNCTIONS! Write a program in c++ and make sure it works, that reads a list of students (first names only) from a file. It is possible for the names tobe in unsorted order in the file but they have to be placed in sorted order within the linked list.The program should use a doubly linked list.Each node in the doubly linked list should have the student’s name, a pointer to the next student, and apointer to the previous student. Here is a sample visual. The head points to the beginning of the list. Thetail points to the end of the list.When inserting consider all the following conditions:if(!head){ //no other nodes}else if (strcmp(data, head->name)<0){ //smaller than head}else if (strcmp(data, tail->name)>0){ //larger than tail}else{ //somewhere in the middle} When deleting a student consider all the following conditions:student may be at the head, the tail or in the middleBelow, you will find a sample of what the…arrow_forwardin the C++ version please suppose to have a score corresponding with probabilities at the end and do not use the count[] function. Please explain the detail when coding. DO NOT USE ARRAY. The game of Pig The game of Pig is a dice game with simple rules: Two players race to reach 100 points. Each turn, a player repeatedly rolls a die until either a 1 ("pig") is rolled or the player holds and scores the sum of the rolls (i.e. the turn total). At any time during a player's turn, the player is faced with two decisions: roll - if the player rolls 1: the player scores nothing and it becomes the opponents turn. 2 - 6: the number is added to the player's turn total and the player's turn continues. hold - The turn total is added to the player's score and it becomes the opponent's turn. This game is a game of probability. Players can use their knowledge of probabilities to make an educated game decision. Assignment specifications Hold-at-20 means that the player will choose to roll…arrow_forward.arrow_forward
- Using c++ Contact list: Binary Search A contact list is a place where you can store a specific contact with other associated information such as a phone number, email address, birthday, etc. Write a program that first takes as input an integer N that represents the number of word pairs in the list to follow. Word pairs consist of a name and a phone number (both strings). That list is followed by a name, and your program should output the phone number associated with that name. Define and call the following function. The return value of FindContact is the index of the contact with the provided contact name. If the name is not found, the function should return -1 This function should use binary search. Modify the algorithm to output the count of how many comparisons using == with the contactName were performed during the search, before it returns the index (or -1). int FindContact(ContactInfo contacts[], int size, string contactName) Ex: If the input is: 3 Frank 867-5309 Joe…arrow_forwardHowever, the number of integers must be a power of 2. In main.c, key[ ] has 16 integers. But as you should alreadyknow, the program will not sort if one integers is removed or added. What you are going to do is modify the mergesort.c module so that a non-power of 2 number of integers can be sorted. When you modify a module, indicate by way of comment, what was changed, the date the change was made. You will also need to add or remove an integer to/from key[] in module sort158a.c.You are NOT allowed to change merge.c, mergesort.h or wrt.c filesThe mergesort function in file mergesort.c MUST call the merge function which it currently does.The main function in sort158a.c you can only change the number of integers in the key[] arrayBelow are 3 examples of output. The first with an array of 15 integers. The second with an array of 16 (power of 2) integers and the third with 17 integers.Example of Output with 15 integersBefore mergesort: 4 3 1 67 55 4 -5 37 7 4 2 9 1 -1 6After mergesort: -5…arrow_forwardOCaml Code: Please read the attached instructions carefully and show the correct code with the screenshot of the output. I really need help with this assignment.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





