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

Write a C program that solves single-variable linear equations. The program shou

ID: 3623315 • Letter: W

Question

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.

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 specication 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.

Sample runs:

Enter a linear equation: 3Y+5=2
Y = -1.000


Enter a linear equation: 2Y + -12 = 0.5 bla blah...
Y = 6.250


Enter a linear equation: 0Y+2=1
Invalid equation


Enter a linear equation: 2 Y + 5 = 2
Invalid input


Enter a linear equation: 3Y-2=1
Invalid input


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]<=57)
      {dec=dec*(in[i]-48)*mult;
       mult/=10;
        i++;   
        }
    }
dec+=c;
if(a==0)
    {printf("cannot do division by 0 ");
    system("pause");
    return 1;
    }
sol=(dec-b)/(double)a;
printf("Y=%.3f ",sol);
getch();
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote