Create a calculator program as follows. Create a menu function ( function should
ID: 3606711 • Letter: C
Question
Create a calculator program as follows.
Create a menu function ( function should display the menu options and return the user selection with the following options:
1 to calculate the number of real solutions for a quadratic equation
2 to calculate the resistance in series for two or more resistors
3 to calculate the resistance in parallel for two or more resistors
4 to calculate power drop across resistors in series
5 to calculate power drop across resistors in parallel
6 to exit program
Repeatedly display the menu after each calculation until option 6 is entered.
Option 1:
Ask the user for and store the three numerical coefficients of a quadratic equation and store them in three float variable.
Write a function named “root” which takes the three coefficients as arguments and return the number of real roots as 2, 1, or 0. Call the function “root” and pass the user inputs as arguments, then, output a statement indicating the number of roots according to the function return values.
For a quadratic equation in the form ax2 + bx + c = 0, calculate the value of sqrt(b2 4ac) and if it is:
Negative zero solutions
Zero one solution
Positive two solutions
Option 2:
Ask the user for the resistor value one resistor at a time.
Write and use a function called “res_series” that takes two resistors in series and return the total resistance. Rseries = R1 + R2 + R3 + …..
Keep adding the resistor values by calling “res-series” until the user enters zero or negative value, then display the total resistance.
Option 3:
Ask the user for the resistor value one resistor at a time.
Write and use a function called “res_parallel” that takes two resistors in parallel and return the total resistance. Rparallel = 1 / R1 + 1 / R2 + 1 / R3 + …..
Keep adding the resistor values by calling “res-parallel” until the user enters zero or negative value, then display the total resistance.
Option 4:
Ask the user for and store the voltage across the resistors. Ask the user for the series resistance one value at a time and use the function “res_series” to calculate the total resistance.
Create and use a function named “current” that takes two parameters, one for voltage and the other for resistance, this function returns the current according to the following OHM’s law equation: V = I × R.
Create and use a function named “power” that takes two parameters, one for voltage and the other for current, this function returns the power drop according to the Power rule: P = I × V.
Output the power drop to the screen.
Option 5:
Ask the user for and store the voltage across the resistors. Ask the user for the parallel resistance one value at a time and use the function “res_parallel” to calculate the total resistance.
Use the function “current” to calculate the current.
Use the function “power” to calculate the power drop.
Output the power drop to the screen.
Option Z or z: exit the program.
Explanation / Answer
Here is the C program to do that -
#include<stdio.h>
#include<stdlib.h>
int root(float a,float b,float c)
{
float D =b*b-4*a*c;
if(D>0)
{
return 2;
}
else if(D==0)
{
return 1;
}
else
{
return 0;
}
}
float res_series()
{
float sum=0 , R;
printf("Enter the values of registers ");
do
{
scanf("%f",&R);
if(R>0)
{
sum+=R;
}
else
{
return sum;
}
}while(R>0);
}
float res_parallel()
{
float sum=0 , R;
printf("Enter the values of registers ");
do
{
scanf("%f",&R);
if(R>0)
{
sum+=1/R;
}
else
{
return (1/sum);
}
}while(R>0);
}
int main()
{
int ch ;
char cha;
float a, b, c ;
int n ;
float V,P;
do
{
printf("1 to calculate the number of real solutions for a quadratic equation 2 to calculate the resistance in series for two or more resistors 3 to calculate the resistance in parallel for two or more resistors 4 to calculate power drop across resistors in series 5 to calculate power drop across resistors in parallel 6 to exit program Enter yout choice ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the coeficients of quardratic equation as a , b , c ");
scanf("%f%f%f",&a,&b,&c);
n=root(a,b,c);
printf("Number of solutions for the quardratic equations are %d ",n);
break;
case 2:
printf("Resistance in series is %f ",res_series());
break;
case 3:
printf("Resistance in parallel is %f ",res_parallel());
break;
case 4:
printf("Enter voltage ");
scanf("%f",&V);
P=(V*V)/res_series();
printf("Power is %f ",P);
break;
case 5:
printf("Enter voltage ");
scanf("%f",&V);
P=(V*V)/res_parallel();
printf("Power is %f ",P);
break;
case 6:exit(0);
default : printf("Enter a valid choice ");
}
printf("Do you want to conitnue ? Enter z or Z to exit ");
scanf("%c",&cha);
}
while(ch!='Z'||ch!='z');
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.