A. Design a program in C using the “switch” command which will perform any one o
ID: 3586841 • Letter: A
Question
A. Design a program in C using the “switch” command which will perform any one of the following four calculations depending on the value of a “choice” operator:
a) one-time calculation of the equivalent resistance to a user-supplied number and values of resistors connected in series if the user types a value of 1 on the keyboard for the choice operator;
b) calculation in an infinite loop of the equivalent resistance to a user-supplied number and values of resistors connected in series if the user types a value of 2 on the keyboard for the choice operator;
c) one-time calculation of the equivalent resistance to an arbitrary number of resistors connected in series if the user types a value 3 for the choice operator; and calculation in an infinite loop of the equivalent resistance to an arbitrary number of resistors connected in series if the user types a value of 4 on the keyboard for the choice operator.
NOTE The formula for computing the equivalent resistance to n resistors with resistance values R1, R2, …, Rn is Req = R1 + R2 + … + Rn.
B. Draw a flowchart for the program in A above.
Explanation / Answer
Solution of A :
#include<stdio.h> //to use scanf() and printf()
int main()
{
int n,choice;
int j=1;
int req=0,Req=0;
int r[100];
printf("Enter your choice from 1 2 3 4 : ");
scanf("%d",&choice);
switch(choice) //switch using here
{
case 1:
{
printf("Enter how many resistors do u want to enter :");
scanf("%d",&n);
printf("Enter %d values of Resistors :",n);
for(int i=1;i<=n;i++)
{
scanf("%d",&r[i]);
req=req+r[i];
}
printf("Equivalent Resistance will be :%d",req);
break;
}
case 2:
{
printf("Enter how many resistors do u want to enter :");
scanf("%d",&n);
printf("Enter %d values of Resistors :",n);
while(n--)
{
scanf("%d",&r[j]);
req=req+r[j];
j++;
}
printf("Equivalent Resistance will be :%d",req);
break;
}
case 3: //arbitrary case (one-time calculation)
{
int r1[]={10,20,40,50,70,80};
int length=sizeof(r1)/sizeof(int);
Req=Req+r1[0]+r1[1]+r1[2]+r1[3]+r1[4]+r1[5];
printf("Equivalent Resistance will be :%d",Req);
break;
}
case 4: //arbitrary case (infinite loop calculation)
{
int k=0;
int r1[]={17,23,43,56,78,89};
int length=sizeof(r1)/sizeof(int);
while(length--)
{
req=req+r1[k];
k++;
}
printf("Equivalent Resistance will be :%d",req);
break;
}
default:
printf("Entered Incorrect choice..");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.