Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Convert the following C program into a C++ program. For example, change scanf()

ID: 3536986 • Letter: C

Question

Convert the following C program into a C++ program. For example, change scanf() to cin, and

printf() to cout. Replace the pointers by references. The new program should generate the same

output as the original one.

#include <stdio.h>

#include <math.h>

void getData( int *a, int *b, int *c ) ;

void printResults(int numRoots, int a, int b, int c, double root1, double root2);

int quadratic( int a, int b, int c, double *pRoot1, double *pRoot2 ) ;

int main ( void )

{

int a ;

int b ;

int c ;

int numRoots ;

double root1 ;

double root2 ;

char again = 'Y' ;

printf( "Solve quadratic equations. " ) ;

while ( again == 'Y' || again == 'y' )

{

getData ( &a, &b, &c ) ;

numRoots = quadratic ( a, b, c, &root1, &root2 ) ;

printResults ( numRoots, a, b, c, root1, root2 ) ;

printf( " Do you have another equation (Y/N): " ) ;

scanf ( " %c", &again ) ;

}

return 0 ;

}

/* Read coefficients for quadratic equation. */

void getData ( int *a, int *b, int *c )

{

printf( "Please enter coefficients a, b, & c: " ) ;

scanf ( "%d%d%d", a, b, c ) ;

return ;

}

1

Class: CS360 Object Oriented Programming in C++

Subject: Homework 1 - From C to C++

/* Compute the roots for a quadratic equation. */

int quadratic ( int a, int b, int c, double *pRoot1, double *pRoot2 )

{

int result ;

double discriminate ;

double root ;

if ( a == 0 && b == 0 )

result = -1 ;

else

{

if ( a == 0 )

{

*pRoot1 = -c / (double) b ;

result = 1 ;

}

else

{

discriminate = b * b - ( 4 * a * c ) ;

if ( discriminate >= 0 )

{

root = sqrt( discriminate ) ;

*pRoot1 = ( -b + root ) / ( 2 * a ) ;

*pRoot2 = ( -b - root ) / ( 2 * a ) ;

result = 2 ;

}

else

result = 0 ;

}

}

return result ;

}

/* Prints the factors for the quadratic equation. */

void printResults(int numRoots, int a, int b, int c, double root1, double root2)

{

printf( "Your equation: %dx**2 + %dx + %d ", a, b, c ) ;

switch ( numRoots )

{

case 2: printf( "Roots are: %6.3f & %6.3f ",

root1, root2 ) ;

break ;

case 1: printf( "Only one root: %6.3f ",

root1 ) ;

break ;

case 0: printf( "Roots are imaginary. " ) ;

break ;

default: printf( "Invalid coefficients " ) ;

break ;

}

}

2

Explanation / Answer

//here is your modified version of c++ code


#include <iostream>

#include <math.h>

using namespace std;


void getData( int &a, int &b, int &c ) ;


void printResults(int numRoots, int a, int b, int c, double root1, double root2);


int quadratic( int a, int b, int c, double &pRoot1, double &pRoot2 ) ;


int main ( void )

{

int a ;

int b ;

int c ;

int numRoots ;

double root1 ;

double root2 ;

char again = 'Y' ;


cout<<"Solve quadratic equations. " ;

while ( again == 'Y' || again == 'y' )

{

getData (a, b, c ) ;

numRoots = quadratic ( a, b, c, root1, root2 ) ;

printResults ( numRoots, a, b, c, root1, root2 ) ;

cout<<" Do you have another equation (Y/N): ";

cin>>again;

}

return 0 ;

}

/* Read coefficients for quadratic equation. */

void getData ( int &a, int &b, int &c )

{

cout<<"Please enter coefficients a, b, & c: " ;

cin>>a>>b>>c;

return ;

}


/*


Class: CS360 Object Oriented Programming in C++

Subject: Homework 1 - From C to C++

Compute the roots for a quadratic equation.

*/

int quadratic ( int a, int b, int c, double &pRoot1, double &pRoot2 )

{

int result ;

double discriminate ;

double root ;

if ( a == 0 && b == 0 )

result = -1 ;

else

{

if ( a == 0 )

{

pRoot1 = -c / (double) b ;

result = 1 ;

}

else

{

discriminate = b * b - ( 4 * a * c ) ;

if ( discriminate >= 0 )

{

root = sqrt( discriminate ) ;

pRoot1 = ( -b + root ) / ( 2 * a ) ;

pRoot2 = ( -b - root ) / ( 2 * a ) ;

result = 2 ;

}

else

result = 0 ;

}

}

return result ;

}

/* Prints the factors for the quadratic equation. */

void printResults(int numRoots, int a, int b, int c, double root1, double root2)

{

cout<<"Your equation: "<<a<<"x**2 + "<<b<<"x + "<<c<<endl;

switch ( numRoots )

{

case 2: cout<<"Roots are: "<<root1<<" "<<root2;

break ;


case 1: cout<<"Only one root: "<<root1 <<endl;

break ;

case 0: cout<<"Roots are imaginary. " ;

break ;

default: cout<<"Invalid coefficients " ;

break ;

}

}