Question

In: Computer Science

Loops Write a simple C/C++ console application that will calculate the equivalent series or parallel resistance....

Loops

Write a simple C/C++ console application that will calculate the equivalent series or parallel resistance. Upon execution the program will request ether 1 Series or 2 Parallel then the number of resisters. The program will then request each resistor value. Upon entering the last resistor the program will print out the equivalent resistance. The program will ask if another run is requested otherwise it will exit.

Solutions

Expert Solution

The language used is C++.

Code:

#include<iostream>
using namespace std;

//This function calculates the equivalent series resistance
double seriesEquivalent(double resistors[], int numberOfResistors) {
double sum = 0;
//Equivalent series resistance is simply the sum of resistances.
for(int i=0;i<numberOfResistors;i++)
sum += resistors[i];

return sum;
}

//This function calculates the equivalent parallel resistance.
double parallelEquivalent(double resistors[], int numberOfResistors) {
double sum = 0;

for(int i=0;i<numberOfResistors;i++)
sum += 1/resistors[i];

//Equivalent resistance is the reciprocal of sum.
return (1/sum);
}

int main() {

while(true) {

//Get the required inputs from the user.
int input;
int numberOfResistors;
cout<<"Press 1 for Series and 2 for Parallel\n";
cin>>input;

if(input != 1 && input != 2){
cout<<"Wrong input. Try again";
continue;
}

cout<<"Enter the number of resistors:\n";
cin>>numberOfResistors;

double resistors[numberOfResistors];

cout<<"Enter the values of "<<numberOfResistors<<" resistors:\n";
for(int i=0;i<numberOfResistors;i++)
cin>>resistors[i];

if(input == 1) {
cout<<"\nEquivalent series resistance: "<<seriesEquivalent(resistors, numberOfResistors)<<endl;
}else if(input == 2) {
cout<<"\nEquivalent parallel resistance: "<<parallelEquivalent(resistors, numberOfResistors)<<endl;
}

char repeat;
cout<<"Press y to repeat and other key to exit: ";
cin>>repeat;

if(repeat != 'y' && repeat != 'Y')
break;
}
return 0;
}

Code Snippets:

Sample Output:


Related Solutions

Three resistors connected in parallel have an equivalent resistance of 1Ω
Three resistors connected in parallel have an equivalent resistance of 1Ω. If two of the resistance values are 2Ω and 6Ω, then the third resistance value must be9Ω.8Ω.6Ω.3Ω.2Ω.
write a c# console application app that reads all the services in the task manager and...
write a c# console application app that reads all the services in the task manager and automatically saves what was read in a Notepad txt file.  Please make sure that this program runs, also add some comments
C# Write a console application that takes the following passage and removes every instance of the...
C# Write a console application that takes the following passage and removes every instance of the word "not" using StringBuilder and prints the result out to the console: I do not like them In a house. I do not like them With a mouse. I do not like them Here or there. I do not like them Anywhere. I do not like green eggs and ham. I do not like them, Sam-I-am. Ensure that the resulting output reads normally, in...
You've been hired by Yogurt Yummies to write a C++ console application that calculates and displays...
You've been hired by Yogurt Yummies to write a C++ console application that calculates and displays the cost of a customer’s yogurt purchase. Use a validation loop to prompt for and get from the user the number of yogurts purchased in the range 1-9. Then use a validation loop to prompt for and get from the user the coupon discount in the range 0-20%. Calculate the following:         ● Subtotal using a cost of $3.50 per yogurt.         ● Subtotal...
You've been hired by Avuncular Addresses to write a C++ console application that analyzes and checks...
You've been hired by Avuncular Addresses to write a C++ console application that analyzes and checks a postal address. Prompt for and get from the user an address. Use function getline so that the address can contain spaces. Loop through the address and count the following types of characters:         ● Digits (0-9)         ● Alphabetic (A-Z, a-z)         ● Other Use function length to control the loop. Use functions isdigit and isalpha to determine the character types. Use formatted...
Write a C++ console application that allows your user to enter the total rainfall for each...
Write a C++ console application that allows your user to enter the total rainfall for each of 12 months into an array of doubles. The program should validate user input by guarding against rainfall values that are less than zero. After all 12 entries have been made, the program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest rainfall amounts.
2) You've been hired by Superfluous Stats to write a C++ console application that analyzes a...
2) You've been hired by Superfluous Stats to write a C++ console application that analyzes a string. Prompt for and read from the user a string that may contain spaces. Then prompt for and read from the user a single character. Print the following stats about the string and character inputs. Use formatted output manipulators (setw, left/right) to print the following rows about the string:           ● The string value.           ● The string length.           ● The spot, if...
Write a series of codes using WHILE loops C++ 1. Ask the user for a number...
Write a series of codes using WHILE loops C++ 1. Ask the user for a number and adds even numbers for 1 to the user entered number. 2. Write another piece of code that asks the user for 2 numbers and adds up the numbers between and including the numbers. 3. Write another piece of code that asks user for a file name and then add up all the numbers for the file.
In VB console write a Console Application that reads an input value for ??. You can...
In VB console write a Console Application that reads an input value for ??. You can assume that the user only inputs numeric values.
IN C++ Write a program that uses nested loops to collect data and calculate the average...
IN C++ Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask the user for the number of years. The outer loop will iterate once for each year. The inner loop will iterate 12 times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the ­­program should display the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT