Implement the quadratic equation in a function with the signature int quad(int a
ID: 3790038 • Letter: I
Question
Implement the quadratic equation in a function with the signature
int quad(int a, int b, int c)
The function should return one of three values indicating whether the roots of the quadratic with the passed coefficients are real, imaginary, or zero. The program should write the one line of output:
There are ___ real roots, ___ imaginary roots, and ___ zero roots.
Guidelines
Use the function int readArray(std::ifstream ifs, int a[], int sz)
Use a switch statement to analyze the return from quad().
2 5 2
8 5 2
9 0 0
1 3 0
9 6 3
3 9 1
8 5 2
1 4 10
9 8 1
3 6 8
9 10 7
3 2 5
4 8 1
4 10 6
8 5 9
4 1 3
9 1 9
8 5 1
10 5 8
4 1 10
3 2 0
2 0 2
4 6 10
10 7 10
10 2 8
4 10 8
1 3 8
5 5 7
6 6 2
4 3 7
4 2 6
1 8 9
5 4 7
5 4 0
5 4 0
1 8 6
2 10 7
1 1 8
4 5 5
3 5 1
2 0 5
4 7 3
9 0 3
7 3 2
7 5 6
4 9 7
3 4 3
3 9 2
7 1 0
3 0 2
9 8 2
4 10 4
1 10 1
6 0 5
3 5 9
3 4 1
9 5 7
9 4 6
1 1 3
4 7 8
8 10 7
10 7 8
4 1 7
7 7 3
5 3 1
4 4 10
7 7 0
10 3 9
4 5 6
5 2 2
10 8 4
1 9 0
10 0 6
10 2 3
2 10 8
8 4 4
7 1 5
2 1 10
9 0 0
3 7 10
2 0 6
6 6 10
7 5 6
5 8 9
8 2 1
9 1 3
7 5 10
3 0 10
5 6 1
1 3 5
1 0 0
3 7 6
8 9 9
6 9 2
1 6 8
5 2 10
5 1 8
8 7 8
5 10 10
4 9 4
7 0 4
8 8 0
10 3 8
8 9 7
5 6 0
1 9 3
10 7 4
9 7 4
5 1 5
9 7 0
9 8 1
6 3 9
7 4 3
7 9 4
1 4 7
6 8 6
9 4 1
3 3 6
6 3 8
6 3 0
3 9 7
3 9 10
2 2 3
4 4 9
8 9 9
4 3 4
9 9 4
10 0 6
1 3 3
10 5 7
10 7 9
5 0 9
6 3 5
7 5 3
10 9 3
8 2 1
9 6 8
2 7 1
1 8 1
Explanation / Answer
// C++ code
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
#include <iomanip>
using namespace std;
int quad(int a, int b, int c)
{
int d = (b*b - 4*a*c);
if( d == 0)
return 0;
else if(d > 0)
return 1;
else
return -1;
}
int readArray(ifstream& ifs, int a[], int sz)
{
int i = 0;
while (true)
{
ifs >> a[i];
i++;
if(ifs.eof())
break;
}
return i;
}
int main()
{
int zero = 0, real = 0, imaginary = 0;
ifstream inFile;
// Open a file
inFile.open("1.txt");
if (inFile.fail())
{
cout << "File does not exist" << endl;
cout << "Exit program" << endl;
return 1;
}
int sz = 1000;
int array[sz] = {0};
sz = readArray(inFile, array, sz);
int a,b,c;
for (int i = 0; i <= sz-3; i=i+3)
{
a = array[i];
b = array[i+1];
c = array[i+2];
int d = quad(a,b,c);
cout << "a: " << a << " b: " << b << " c: " << c << " ";
cout << "There are ";
switch(d)
{
case 0:
cout << "Zero roots ";
zero++;
break;
case 1:
cout << "Real Roots ";
real++;
break;
case -1:
cout << "Imaginary roots ";
imaginary++;
break;
}
}
cout << " There are " << real << " real roots, " << imaginary << " imaginary roots, and " << zero << " zero roots. ";
return 0;
}
/*
input.txt
2 5 2
8 5 2
9 0 0
1 3 0
9 6 3
3 9 1
8 5 2
1 4 10
9 8 1
3 6 8
9 10 7
3 2 5
4 8 1
4 10 6
8 5 9
4 1 3
9 1 9
8 5 1
10 5 8
4 1 10
3 2 0
2 0 2
4 6 10
10 7 10
10 2 8
4 10 8
1 3 8
5 5 7
6 6 2
4 3 7
4 2 6
1 8 9
5 4 7
5 4 0
5 4 0
1 8 6
2 10 7
1 1 8
4 5 5
3 5 1
2 0 5
4 7 3
9 0 3
7 3 2
7 5 6
4 9 7
3 4 3
3 9 2
7 1 0
3 0 2
9 8 2
4 10 4
1 10 1
6 0 5
3 5 9
3 4 1
9 5 7
9 4 6
1 1 3
4 7 8
8 10 7
10 7 8
4 1 7
7 7 3
5 3 1
4 4 10
7 7 0
10 3 9
4 5 6
5 2 2
10 8 4
1 9 0
10 0 6
10 2 3
2 10 8
8 4 4
7 1 5
2 1 10
9 0 0
3 7 10
2 0 6
6 6 10
7 5 6
5 8 9
8 2 1
9 1 3
7 5 10
3 0 10
5 6 1
1 3 5
1 0 0
3 7 6
8 9 9
6 9 2
1 6 8
5 2 10
5 1 8
8 7 8
5 10 10
4 9 4
7 0 4
8 8 0
10 3 8
8 9 7
5 6 0
1 9 3
10 7 4
9 7 4
5 1 5
9 7 0
9 8 1
6 3 9
7 4 3
7 9 4
1 4 7
6 8 6
9 4 1
3 3 6
6 3 8
6 3 0
3 9 7
3 9 10
2 2 3
4 4 9
8 9 9
4 3 4
9 9 4
10 0 6
1 3 3
10 5 7
10 7 9
5 0 9
6 3 5
7 5 3
10 9 3
8 2 1
9 6 8
2 7 1
1 8 1
output:
a: 2 b: 5 c: 2 There are Real Roots
a: 8 b: 5 c: 2 There are Imaginary roots
a: 9 b: 0 c: 0 There are Zero roots
a: 1 b: 3 c: 0 There are Real Roots
a: 9 b: 6 c: 3 There are Imaginary roots
a: 3 b: 9 c: 1 There are Real Roots
a: 8 b: 5 c: 2 There are Imaginary roots
a: 1 b: 4 c: 10 There are Imaginary roots
a: 9 b: 8 c: 1 There are Real Roots
a: 3 b: 6 c: 8 There are Imaginary roots
a: 9 b: 10 c: 7 There are Imaginary roots
a: 3 b: 2 c: 5 There are Imaginary roots
a: 4 b: 8 c: 1 There are Real Roots
a: 4 b: 10 c: 6 There are Real Roots
a: 8 b: 5 c: 9 There are Imaginary roots
a: 4 b: 1 c: 3 There are Imaginary roots
a: 9 b: 1 c: 9 There are Imaginary roots
a: 8 b: 5 c: 1 There are Imaginary roots
a: 10 b: 5 c: 8 There are Imaginary roots
a: 4 b: 1 c: 10 There are Imaginary roots
a: 3 b: 2 c: 0 There are Real Roots
a: 2 b: 0 c: 2 There are Imaginary roots
a: 4 b: 6 c: 10 There are Imaginary roots
a: 10 b: 7 c: 10 There are Imaginary roots
a: 10 b: 2 c: 8 There are Imaginary roots
a: 4 b: 10 c: 8 There are Imaginary roots
a: 1 b: 3 c: 8 There are Imaginary roots
a: 5 b: 5 c: 7 There are Imaginary roots
a: 6 b: 6 c: 2 There are Imaginary roots
a: 4 b: 3 c: 7 There are Imaginary roots
a: 4 b: 2 c: 6 There are Imaginary roots
a: 1 b: 8 c: 9 There are Real Roots
a: 5 b: 4 c: 7 There are Imaginary roots
a: 5 b: 4 c: 0 There are Real Roots
a: 5 b: 4 c: 0 There are Real Roots
a: 1 b: 8 c: 6 There are Real Roots
a: 2 b: 10 c: 7 There are Real Roots
a: 1 b: 1 c: 8 There are Imaginary roots
a: 4 b: 5 c: 5 There are Imaginary roots
a: 3 b: 5 c: 1 There are Real Roots
a: 2 b: 0 c: 5 There are Imaginary roots
a: 4 b: 7 c: 3 There are Real Roots
a: 9 b: 0 c: 3 There are Imaginary roots
a: 7 b: 3 c: 2 There are Imaginary roots
a: 7 b: 5 c: 6 There are Imaginary roots
a: 4 b: 9 c: 7 There are Imaginary roots
a: 3 b: 4 c: 3 There are Imaginary roots
a: 3 b: 9 c: 2 There are Real Roots
a: 7 b: 1 c: 0 There are Real Roots
a: 3 b: 0 c: 2 There are Imaginary roots
a: 9 b: 8 c: 2 There are Imaginary roots
a: 4 b: 10 c: 4 There are Real Roots
a: 1 b: 10 c: 1 There are Real Roots
a: 6 b: 0 c: 5 There are Imaginary roots
a: 3 b: 5 c: 9 There are Imaginary roots
a: 3 b: 4 c: 1 There are Real Roots
a: 9 b: 5 c: 7 There are Imaginary roots
a: 9 b: 4 c: 6 There are Imaginary roots
a: 1 b: 1 c: 3 There are Imaginary roots
a: 4 b: 7 c: 8 There are Imaginary roots
a: 8 b: 10 c: 7 There are Imaginary roots
a: 10 b: 7 c: 8 There are Imaginary roots
a: 4 b: 1 c: 7 There are Imaginary roots
a: 7 b: 7 c: 3 There are Imaginary roots
a: 5 b: 3 c: 1 There are Imaginary roots
a: 4 b: 4 c: 10 There are Imaginary roots
a: 7 b: 7 c: 0 There are Real Roots
a: 10 b: 3 c: 9 There are Imaginary roots
a: 4 b: 5 c: 6 There are Imaginary roots
a: 5 b: 2 c: 2 There are Imaginary roots
a: 10 b: 8 c: 4 There are Imaginary roots
a: 1 b: 9 c: 0 There are Real Roots
a: 10 b: 0 c: 6 There are Imaginary roots
a: 10 b: 2 c: 3 There are Imaginary roots
a: 2 b: 10 c: 8 There are Real Roots
a: 8 b: 4 c: 4 There are Imaginary roots
a: 7 b: 1 c: 5 There are Imaginary roots
a: 2 b: 1 c: 10 There are Imaginary roots
a: 9 b: 0 c: 0 There are Zero roots
a: 3 b: 7 c: 10 There are Imaginary roots
a: 2 b: 0 c: 6 There are Imaginary roots
a: 6 b: 6 c: 10 There are Imaginary roots
a: 7 b: 5 c: 6 There are Imaginary roots
a: 5 b: 8 c: 9 There are Imaginary roots
a: 8 b: 2 c: 1 There are Imaginary roots
a: 9 b: 1 c: 3 There are Imaginary roots
a: 7 b: 5 c: 10 There are Imaginary roots
a: 3 b: 0 c: 10 There are Imaginary roots
a: 5 b: 6 c: 1 There are Real Roots
a: 1 b: 3 c: 5 There are Imaginary roots
a: 1 b: 0 c: 0 There are Zero roots
a: 3 b: 7 c: 6 There are Imaginary roots
a: 8 b: 9 c: 9 There are Imaginary roots
a: 6 b: 9 c: 2 There are Real Roots
a: 1 b: 6 c: 8 There are Real Roots
a: 5 b: 2 c: 10 There are Imaginary roots
a: 5 b: 1 c: 8 There are Imaginary roots
a: 8 b: 7 c: 8 There are Imaginary roots
a: 5 b: 10 c: 10 There are Imaginary roots
a: 4 b: 9 c: 4 There are Real Roots
a: 7 b: 0 c: 4 There are Imaginary roots
a: 8 b: 8 c: 0 There are Real Roots
a: 10 b: 3 c: 8 There are Imaginary roots
a: 8 b: 9 c: 7 There are Imaginary roots
a: 5 b: 6 c: 0 There are Real Roots
a: 1 b: 9 c: 3 There are Real Roots
a: 10 b: 7 c: 4 There are Imaginary roots
a: 9 b: 7 c: 4 There are Imaginary roots
a: 5 b: 1 c: 5 There are Imaginary roots
a: 9 b: 7 c: 0 There are Real Roots
a: 9 b: 8 c: 1 There are Real Roots
a: 6 b: 3 c: 9 There are Imaginary roots
a: 7 b: 4 c: 3 There are Imaginary roots
a: 7 b: 9 c: 4 There are Imaginary roots
a: 1 b: 4 c: 7 There are Imaginary roots
a: 6 b: 8 c: 6 There are Imaginary roots
a: 9 b: 4 c: 1 There are Imaginary roots
a: 3 b: 3 c: 6 There are Imaginary roots
a: 6 b: 3 c: 8 There are Imaginary roots
a: 6 b: 3 c: 0 There are Real Roots
a: 3 b: 9 c: 7 There are Imaginary roots
a: 3 b: 9 c: 10 There are Imaginary roots
a: 2 b: 2 c: 3 There are Imaginary roots
a: 4 b: 4 c: 9 There are Imaginary roots
a: 8 b: 9 c: 9 There are Imaginary roots
a: 4 b: 3 c: 4 There are Imaginary roots
a: 9 b: 9 c: 4 There are Imaginary roots
a: 10 b: 0 c: 6 There are Imaginary roots
a: 1 b: 3 c: 3 There are Imaginary roots
a: 10 b: 5 c: 7 There are Imaginary roots
a: 10 b: 7 c: 9 There are Imaginary roots
a: 5 b: 0 c: 9 There are Imaginary roots
a: 6 b: 3 c: 5 There are Imaginary roots
a: 7 b: 5 c: 3 There are Imaginary roots
a: 10 b: 9 c: 3 There are Imaginary roots
a: 8 b: 2 c: 1 There are Imaginary roots
a: 9 b: 6 c: 8 There are Imaginary roots
a: 2 b: 7 c: 1 There are Real Roots
a: 1 b: 8 c: 1 There are Real Roots
There are 34 real roots, 102 imaginary roots, and 3 zero roots.
*/