C Programming Project Modification: (Please provide terminal output and comments
ID: 3739632 • Letter: C
Question
C Programming Project Modification: (Please provide terminal output and comments)
This C program functions and operates properly, but I would like someone to add a few different programming topics to this program.
Here are the topics that you can utilize, please make sure that their are (6) topics from this list that are used throughout the program.
Topic List:( I know some of the topics are already in use, I would like to you add topics that are not in use )
Strings
2D Arrays
File I/O
Command Line Arguments
Pass by Reference
Structures
Linked Lists (Pointers)
Sorting
Recursion
Stack/Queue
Header Files
Source code of Program:
#include <math.h>
//function prototype declrations
void displayRoots1(double root1);
void displayRoots2(double realPart,double imaginaryPart);
void displayRoots3(double root1, double root2);
//structure declration
struct Roots
{
//memebr variables
double a, b, c;
double root1, root2;
double realPart, imaginaryPart;
};
//function defintion for computeRoots function
double computeRoots(struct Roots R)
{
double determinant = R.b*R.b-4*R.a*R.c;
// condition for real and different roots
if (determinant > 0)
{
printf("Real and different roots. ");
R.root1 = (-R.b+sqrt(determinant))/(2*R.a);
R.root2 = (-R.b-sqrt(determinant))/(2*R.a);
displayRoots3(R.root1, R.root2);
}
//condition for real and equal roots
else if (determinant == 0)
{
printf("Real and equal roots. ");
R.root1 = R.root2 = -R.b/(2*R.a);
displayRoots1(R.root1);
}
// if roots are not real
else
{
printf("Imaginary roots. ");
R.realPart = -R.b/(2*R.a);
R.imaginaryPart = sqrt(-determinant)/(2*R.a);
displayRoots3(R.realPart, R.imaginaryPart);
}
}
//defintion for display functions
void displayRoots2(double root1, double root2)
{
printf("root1 = %.2lf and root2 = %.2lf ",root1 , root2);
}
void displayRoots1(double root1)
{
printf("root1 = root2 = %.2lf ", root1);
}
void displayRoots3(double realPart,double imaginaryPart)
{
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi ", realPart, imaginaryPart, realPart, imaginaryPart);
}
//start of the main function
int main()
{
//structure object declration
struct Roots R;
//variable declaration
double determinant, root1,root2;
//ask to input coefficients
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf",&R.a, &R.b, &R.c);
//function calling
computeRoots(R);
return 0;
}//end of the main program
Explanation / Answer
Topic List:
Please find the code below covering all these topics.
CODE
========================
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
//function prototype declrations
void displayRoots1(double root1);
void displayRoots2(double realPart,double imaginaryPart);
void displayRoots3(double root1, double root2);
//structure declration
struct Roots {
//memebr variables
double a, b, c;
double root1, root2;
double realPart, imaginaryPart;
};
//function defintion for computeRoots function
// Argument passed by Reference here
double computeRoots(struct Roots *R) {
double determinant = R->b*R->b-4*R->a*R->c;
// condition for real and different roots
if (determinant > 0){
printf("Real and different roots. ");
R->root1 = (-R->b+sqrt(determinant))/(2*R->a);
R->root2 = (-R->b-sqrt(determinant))/(2*R->a);
displayRoots3(R->root1, R->root2);
}
//condition for real and equal roots
else if (determinant == 0) {
printf("Real and equal roots. ");
R->root1 = R->root2 = -R->b/(2*R->a);
displayRoots1(R->root1);
}
// if roots are not real
else {
printf("Imaginary roots. ");
R->realPart = -R->b/(2*R->a);
R->imaginaryPart = sqrt(-determinant)/(2*R->a);
displayRoots3(R->realPart, R->imaginaryPart);
}
}
//defintion for display functions
void displayRoots2(double root1, double root2) {
// FILE I/O
FILE *fptr;
fptr = fopen("output.txt", "w");
if(fptr == NULL) {
printf("Error!");
exit(1);
}
fprintf(fptr, "root1 = %.2lf and root2 = %.2lf ",root1 , root2);
printf("root1 = %.2lf and root2 = %.2lf ",root1 , root2);
}
void displayRoots1(double root1) {
printf("root1 = root2 = %.2lf ", root1);
}
void displayRoots3(double realPart,double imaginaryPart) {
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi ", realPart, imaginaryPart, realPart, imaginaryPart);
}
//start of the main function
int main(int argc, char *argv[]) {
//structure object declration
struct Roots* R;
//variable declaration
double determinant, root1,root2;
// command line arguments
R->a = atof(argv[1]);
R->b = atof(argv[2]);
R->c = atof(argv[3]);
//function calling
computeRoots(R);
return 0;
}//end of the main program
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.