need help with an assignment pasted below. Specifically, what I first need help
ID: 3751734 • Letter: N
Question
need help with an assignment pasted below. Specifically, what I first need help with is creating functions in C (I will write to latex when finished).
These functions will generate random roots according to specific parameters, and then write a quadratic equation resulting in those roots. My program is so far formatted like this:
void realRoots()
{
}
void complexRoots()
{
}
void repeatingRoots()
{
}
The best method to me seemed to be to begin by randomly generating the roots, and then work backwards, which i began with:
void realQuadratic()
{
int x1 = 0;
int a, b, c
while(x1==0)
{
x1 = rand()%11 -5; //This generates an X1 root between -5 and 5 as long as its not 0, which is forbidded by the problem
}
while(x2!=x1)
{
x2 = rand()%11-5
}
This is where I pull a blank. Im not sure how to work backgrounds in the program. I need to generate a quadratic equation and be able to output it using those roots. I can use any 2 roots on paper to work backgrounds into a quadartic equation (by setting both X terms = to 0 and foiling them) but doing this on a program seems a bit confusing because of the variable nature of B and C. I also am not sure how to approach the imaginary roots function. Help with either or both of these would be stupendous, with as much detail explained as possible for the sake of learning. Thanks so much!
PS - Int main() will do nothing more than seed random according to(time(NULL)) and call the functions which perform all duties (including generating numbers and printing in each function)
}
Explanation / Answer
Sicen quadratic equations will be having 3 different scenarios,so I would suggest to use either If_else,switch case instead of using 3 individual method.I have used here simple If_else statement.(note: I have consider upto 4 decimal places)
#include<stdio.h>
#include<math.h>
int main(){
Float a,b,c,d,R1,R2,real,imaginary;
Printf("enter coefficient of quadratic equations");
Scanf("%f%f%f,&a,&b,&c);
If(a==0)
{
Printf("please enter number other than 0 for coefficient of a");
Break;
}
Else{
d= b*b-(4*a*c);
||Now we need to check all possible combinations
||Real and different roots
If(d>0){
R1 = (-b +sqrt(d))/(2*a);
R2 = (-b -sqrt(d))/(2*a);
if(R1>(-5) & R1<5)
Printf("root1= %.4f",R1);
if(R2>(-5) & R2<5)
Printf("root2=%.4f",R2);
}
||Real and equal roots
Else if(d==0)
{
R1=R2= -b/(2*a);
if(R1>(-5) & R2<5)
Printf("root1=root2= %.4f",R1);
}
||Imaginary roots
Else
{
real = -b/(2*a);
imaginary = sqrt(-d)/(2*a);
if(real>(-5) & real<5)
{ if(imaginary>(-5) &imaginary<5)
{
Printf("root1=%.4f+%.4fi",real, imaginary);
Printf("root2=%.4f-%.4fi",real, imaginary);
}
}
}
}
Return 0;
}
|| If you still want to use your method then just add my logic for different cases.
|| Try not to use use rand() as it will return values in rangesonly betwwen 0 to rand_max(32767)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.