Untitled

mail@pastecode.io avatar
unknown
plain_text
24 days ago
740 B
6
Indexable
Never
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;


//Q2

double quad(double a, double b, double c, double &r1, double &r2);
//input:a,b,c
//output: r1,r2
//call-by-value: a,b,c
//call-by-reference: r1,r2

int main(){
 double a,b,c,r1,r2;
 
cout << "\ninput a:";
cin >> a;
cout << "\ninput b:";
cin >> b;
cout << "\ninput c:";
cin >> c;

r1 =quad(a,b,c,r1,r2);
r2 =quad(a,b,c,r1,r2);

cout << "\nr1 =" << r1;
cout << "\nr2 =" << r2;
 
    
    
}


double quad(double a, double b, double c, double &r1, double &r2){

double d = pow(b,2) - 4*a*c;
r1 = -b + sqrt(d)/ (2*a);

r2 = -b - sqrt(d)/ (2*a);

if( a == 0 || d < 0 ) {
    cout << "\nerror the root doesn't exist\n";
    return 1;
    
} else return 0;
    
}
Leave a Comment