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

//** Use C and use only use <stdio.h> **// Write a C program that solves single-

ID: 3638863 • Letter: #

Question

//** Use C and use only use <stdio.h> **//

Write a C program that solves single-variable linear equations. The program should prompt the user to enter a linear equation of the form

aY + b = c

where a, b, and c are real numbers of type double. The program should then output the value of Y that solves the equation, if such a value exists (see the note about verifying that a in nonzero). If the input provided by the user is not valid, the program should terminate with an appropriate error message.

Notes:
The character Y should appear in the input as a capital letter. Introduce this character into the
program as a symbolic constant using #define, as follows:

#define VARIABLE NAME `Y'

When reading the equation using scanf(), you can use the %c conversion specification to read
the character typed-in by the user, then compare this character with VARIABLE NAME.
There should be no spaces in the input between the number a and the character Y. However, there may or may not be spaces around the + and = characters.
If there is additional text after the equation, but the equation itself is valid, ignore the additional
text and solve the equation as described above. For an example of this, see the second sample run.
Verify that the value of a is not zero. If this is not the case, you should print an appropriate error
message and terminate the program using return 1. See the third sample run.
Print the value of Y that solves the equation with three digits of accuracy after the decimal point.
Here are six sample runs of this program, assuming that the executable file is called linear solver.

1) /home/userXYZ/ECE15/Lab2> linear solver
Enter a linear equation: 3Y+5=2
Y = -1.000
2) /home/userXYZ/ECE15/Lab2> linear solver
Enter a linear equation: 2Y + -12 = 0.5 bla blah...
Y = 6.250
3) /home/userXYZ/ECE15/Lab2> linear solver
Enter a linear equation: 0Y+2=1
Invalid equation!
4) /home/userXYZ/ECE15/Lab2> linear solver
Enter a linear equation: 2 Y + 5 = 2
Invalid input!
5) /home/userXYZ/ECE15/Lab2> linear solver
Enter a linear equation: 3Y-2=1
Invalid input!
6) /home/userXYZ/ECE15/Lab2> linear solver
Enter a linear equation: 2y + 12 = 0.5
Invalid input!

Explanation / Answer

#include #include #define Y 'Y' int main() {int a=0,b=0,c=0,i=0,sign=1,addsub=1; double dec=0,mult=1,sol; char in[30]; printf("Enter a linear equation: "); gets(in); while(in[i]!=Y) {if(in[i]=='-') sign=-1; else {a=a*mult+(in[i]-48); mult*=10; } i++; } a*=sign; sign=1; mult=1; i++; while(in[i]!='+'&&in[i]!='-') {i++; } if(in[i]=='-') addsub=-1; i++; while(in[i]==' ') {i++; } if(in[i]=='-') {addsub*=-1; i++; } while(in[i]!='='&&in[i]!=' ') {b=b*mult+(in[i]-48); mult*=10; i++; } i++; while(in[i]==' ') {i++; } b*=addsub; if(in[i]=='=') i++; while(in[i]==' ') {i++; } sign=1; mult=1; if(in[i]=='-') {sign=-1; i++; } while(in[i]!='.'&&in[i]!=''&&in[i]!=' ') {c=c*mult+(in[i]-48); mult*=10; i++; } if(in[i]=='.') {i++; mult=1; dec=.1; while(in[i]>=48&&in[i]