In: Computer Science
Write a C program that does the following
In this part, you will write more complicated functions. They will require parameters and return values. The purpose is to give you experience with these components, and to show you how functions can be used to break your code down into smaller parts. You will also get some more experience with iterating through arrays.Open repl project Lab: User-Defined Functions 2. Write a program that does the following:
1.(20 pts.) Allows the user to enter a sequence of up to 100 resistance values. The method of obtaining the input is up to you, but the values must be stored in an array.
2.(20 pts.) Calculates the equivalent series resistance of the resistance values. This calculation must be done in a user-defined function that accepts the array of resistance values and the number of values in the array as parameters, and returns the equivalent series resistance.
3.(20 pts.) Calculates the equivalent parallel resistance of the resistance values. This calculation must be done in a user-defined function that accepts the array of resistance values and the number of values in the array as parameters, and returns the equivalent parallel resistance.
4.(20 pts.) Calls these two functions from the main function, and prints their return values(the equivalent series and parallel resistances).
Program:
#include<stdio.h>
float findSeries(float a[], int n);
float findParallel(float a[], int n);
int main()
{
float a[100];
int i=1,n;
float res=0, R=0, parallel = 0;
printf("Enter the number of Resistance: ");
scanf("%d",&n);
while(i <= n) {
printf("Enter the value for %d resistor: ",i);
scanf("%f",&a[i]);
i++;
}
printf("Equivalent Series Resistance: %.2f
Ohm\n",findSeries(a,n));
printf("Equivalent Parallel resitance: %.2f
Ohm",findParallel(a,n));
return 0;
}
float findSeries(float a[], int n)
{
int i =1;
float series = 0;
while(i <= n)
{
series += a[i];
i++;
}
return series;
}
float findParallel(float a[], int n)
{
int i =1;
float parallel = 0;
while(i <= n)
{
parallel += 1.0/a[i];
i++;
}
parallel += 1.0 / parallel;
return parallel;
}
Note: Refer the screenshots for further
clarification.
Output: