Homework 9 Design a program in C which will compute, using a one-dimensional arr
ID: 2249520 • Letter: H
Question
Homework 9
Design a program in C which will compute, using a one-dimensional array, the equivalent resistance to a user-specified number N of resistors connected either in series or in parallel, with the values of these resistors also being user-specified. The switch command together with a choice operator is to be used for selecting between the case of resistors connected in series and the case of resistors connected in parallel. When executed, the program will do the following in sequence:
1. Ask the user to type on the keyboard a value for the choice operator (select choice=1 for the case of resistors connected in series, and choice=2 for the case of resistors connected in parallel).
2. Ask the user to type on the keyboard a value for the number N of resistors connected either in series or in parallel.
3. Ask the user to type on the keyboard the values R[0], R[1], R[2],.., R[N-1] of these N resistors.
4. Display on the console a table consisting of two columns, the first one consisting of resistor numbers in the format R[0], R[1], R[2],..,R[N-1], and the second column consisting of the user-inputted values of these resistors.
5. Compute and display on the console the value of the equivalent resistance req to the N resistors connected in series which is given by the formula
req = R[0] + R[1] + R[2] + … + R[N-1], or the value of the equivalent resistance req to the N resistors connected in parallel which may be computed using the formula req=1/geq, where geq=G[0]+G[1]+G[2]+…+G[N-1], with the conductance elements having the values G[0]=1/R[0], G[1]=1/R[1], G[2]=1/R[2],…,G[N-1]=1/R[N-1}.
Your design is to be in the form of four separate programs:
Program A: This design will perform a one-shot computation of req without utilizing a prototype function.
Explanation / Answer
#include <stdio.h>
int main()
{
int ch, n;
int k;
float r[100], R_ser, R_par, g_par, Req;
printf(“Enter your choice (1 if series connected, 2 if parallel connected): “);
scanf(“%d”,&ch);
while(n<1 || n>2)
{
printf(“Invalid choice”);
printf(“Enter your choice (1 if series connected, 2 if parallel connected): “);
scanf(“%d”,&ch);
}
printf(“Enter the number of resistors, N(1 to 100): “);
scanf(“%d”,&n);
while(n>100 || n<=0)
{
printf(“Invalid input”);
printf(“Enter the number of resistors, N(1 to 100): “);
scanf(“%d”,&n);
}
printf(“Enter the values of the resistors”);
for(k = 0; k < n; k++)
{
printf("R-%d ", k+1);
scanf("%f", &r[k]);
}
/*Displaying the resistor values */
for(k = 0; k < n; k++)
{
printf("R-%d %f ", k+1,r[k]);}
/*Finding equivalent resistance*/
R_ser = 0;
R_par =0;
if (ch==1)
{ for(k = 0; k < n; k++)
R_ser+=r[k];
Req = R_ser;}
else
{ for(k = 0; k < n; k++)
g_par+=(1/r[k]);
R_par = 1/g_par;
Req = R_par;
}
printf(“Equivalent Resistance = %f,”Req);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.