Question

In: Computer Science

How would I write a code from this following word problem? 3. Use the pow() and...

How would I write a code from this following word problem?

3. Use the pow() and sqrt() functions to compute the roots of a quadratic equation. Enter the three coefficients with a single input statement. Warning: if you give sqrt() a negative value, the function cannot produce a valid answer.

Solutions

Expert Solution

Note These both are mathematical function so you need to #include <math.h> header file in your code in order to run a error free code

pow() function is used to calculate power of a number i.e we want a^b then pow will be like

Syntax ----> pow(a,b)

Example you want 2^3 so you will write pow(2,3) and ans will be 8

Sqrt() Function is used to find square root of a number And it is used as

Syntax --- >sqrt(number)

For example you root 4 value so write as

sqrt(4) ans will be 2

To calculate roots of a quadratic equation we use quadratic formula i.e

x1=-b + sqrt(b^2- 4ac) /2a

x2=x1=-b - sqrt(b^2- 4ac) /2a

C++ code for finding roots of a number

#include<iostream.h>

#include<math.h> // so that we can use sqrt and pow function

using namespace std;

int main()

{

float a ,b ,c ,D; // These are coefficients of the quadratic equation ax^2+b*x+c

float root1,root2;

cout<"Enter the three coefficients of the Equation a,b c";

cin>>a>>b>>c;

// Now calculate D for the equation i.e discriminant which is D=pow(b,2)-4*a*c

//Three cases are there

//1. D=0 means equation having equal roots

// 2. D>0 means equation having real and distinct roots

// 3. D<0 the case that you said function will not give a valid ans as roots are imaginary in this case

// Applying above 3 in code

D=pow(b,2)-4*a*c;

//Check for D

if(D>0 || D=0) // Case 1 and Case 2 in Case 1 root 1=root 2

{

root1=( -b + sqrt(D) ) /2a;

root2 = (-b - sqrt(D) ) /2a;

cout<<"First Root is "<<root1<<endl;

cout<<"Second Root is "<<root2<<endl;

}

else // this is the case when D<0 and we are giving a negative value to sqrt() function  

{

cout<<"Value of D is negative so Function" cannot produce a valid answer as roots are imaginary";

}

return 0 ;


}


Related Solutions

How would I code the following in assembly language? Use the Keil programming environment to code...
How would I code the following in assembly language? Use the Keil programming environment to code the C8051F330 SiLabs 8051 micro controller. All the documentation is available on efundi. The program shall be done in assembler and you shall use the DJNZ instruction to generate a delay time to flash the LED. The LED shall flash in the following sequence: 1. On for 50mS, 2. Off for 400mS, 3. On for 50mS, 4. Off for 1.5S, 5. Repeat the sequence...
How would I write a C++ code that gives me the following out put:    Enter...
How would I write a C++ code that gives me the following out put:    Enter count of male students: 14 Enter count of female students: 17 Students registered Total:    31 Male:     14   45.16% Female:   17   54.84%    Enter count of male students: 7 Enter count of female students: 2 Students registered Total:     9 Male:      7   77.78% Female:    2   22.22%            */
Write a passage and use Mathlab to display a matrix showing the code word and probability...
Write a passage and use Mathlab to display a matrix showing the code word and probability of occurrence of each character. thanks
Define the following statistical problem for the problem, what data would I collect and how would...
Define the following statistical problem for the problem, what data would I collect and how would I display the data for: A recent proposal is to roll back fuel efficiency standards for newer automobiles. The main argument is for safety and saving lives. The argument is: Cars that are more fuel efficient cost more and hence people will keep their cars longer thus missing out on safety features of new cars. This will lead to an increase in fatalities. I...
PLEASE DO IN C++ AND USE REPL TO WRITE CODE The following problem statement is based...
PLEASE DO IN C++ AND USE REPL TO WRITE CODE The following problem statement is based on a problem in the C++ text by Friedman & Koffman: The results of a survey of the households in your township are available for public scrutiny. Each record (struct-type entity) contains input data for one household, including a four-digit integer identification number the annual income for the household the number of household members. Assuming that no more than 25 households were surveyed, write...
Write a 750-word essay on a principle from the AICPA principles section of the Code of...
Write a 750-word essay on a principle from the AICPA principles section of the Code of Professional Conduct Provisions. Include the following in your essay: Identify 1 of the 6 principles from the AICPA principles section of the Code of Professional Conduct Provisions. Identify several rules from the rules section and show how they relate to the selected principle. Evaluate the selected principle based on a deontological ethics perspective and a Christian worldview. Your essay must cite at least 2...
Write MIPS assembly code for the following C code. for (i = 10; i < 30;...
Write MIPS assembly code for the following C code. for (i = 10; i < 30; i ++) { if ((ar[i] > b) || (ar[i] <= c)) ar[i] = 0; else ar[i] = a; }
I just wrote Python code to solve this problem: Write a generator that will return a...
I just wrote Python code to solve this problem: Write a generator that will return a sequence of month names. Thus gen = next_month('October') creates a generator that generates the strings 'November', 'December', 'January' and so on. If the caller supplies an illegal month name, your function should raise a ValueError exception with text explaining the problem. Here is my code: month_names = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] def next_month(name: str) -> str:...
How would I get this java code to work and with a main class that would...
How would I get this java code to work and with a main class that would demo the rat class? class rat { private String name; private String specialAbility; private int TotalHealth; private int shieldedHealth; private int cooldown; public rat() { } public rat(String n,String SA,int TH,int SH,int cd) { name=n; specialAbility=SA; TotalHealth=TH; shieldedHealth=SH; cooldown=cd; } public void setname(String n) { name=n; } public String getname() { return name; } public void setability(String SA) { specialAbility=SA; } public String getability()...
Using python as the coding language please write the code for the following problem. Write a...
Using python as the coding language please write the code for the following problem. Write a function called provenance that takes two string arguments and returns another string depending on the values of the arguments according to the table below. This function is based on the geologic practice of determining the distance of a sedimentary rock from the source of its component grains by grain size and smoothness. First Argument Value Second Argument Value Return Value "coarse" "rounded" "intermediate" "coarse"...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT