4.1.1 Students in Computer Science would like to understand the concept of Ohm’s law. You
are required to write an application that will allow them to calculate the relation
between Voltage, Current and Resistance using the formulas below.
V = IR
Current (I) = V/R
Resistance= V/I
(where I is the current, V - Voltage and R is the resistance)
4.1.1 Write a program that will assist the students:
4.1.2 Create a C++ source file called OLaw and save it in a file called OLaw.cpp.
4.2 Create the following functions:
4.2.1 calcVoltage() This function must calculate the voltage in a circuit using the
information given above.
4.2.2 calcCurrent() This function must calculate the current charge using the information
given above.
4.2.3 calcResistance() This function must calculate the resistance of the flow using the
information given above.
4.2.4 main() NB: The functions must be implemented above the main.
Add all necessary pre-processor directives.
Declare all constants and necessary variables.
Prompt the user for the calculation h
#include <iostream>
#include <cmath>
using namespace std;
double calcVoltage(double current, double resistance) {
double voltage;
voltage = current * resistance;
return voltage;
}
double calcCurrent(double voltage, double resistance) {
double current;
current = voltage / resistance;
return current;
}
double calcResistance(double voltage, double current) {
double resistance;
resistance = voltage / current;
return resistance;
}
int main() {
double voltage;
double current;
double resistance;
int choose;
int loop = 1;
for ( ; loop; ) {
cout << "================================================================================================\n";
cout << "Hello! This program will allow you to calculate the relation between Voltage, Current and Resistance. \n\n";
cout << "Please choose what phisical value you would like to calculate (by choosing a number):\n";
cout << "1. Calculate the voltage in a circuit\n2. Calculate the current charge\n3. Calculate the resistance of the flow\n4. Exit program\n";
cout << "================================================================================================\n";
cout << "\nPlease choose one: ";
cin >> choose;
if ( (choose < 1) || (choose > 4) ) {
cout << "\nError: You entered a wrong number, please try again!\n\n";
continue;
}
if ( choose == 4 ) {
loop = 0;
continue;
}
if ( choose == 1) {
cout << "\nEnter the current charge: ";
cin >> current;
cout << "Enter the resistance: ";
cin >> resistance;
voltage = calcVoltage(current, resistance);
cout << "The voltage in a circuit is " << voltage << "\n\n";
continue;
}
if ( choose == 2) {
cout << "\nEnter the voltage: ";
cin >> voltage;
cout << "Enter the resistance: ";
cin >> resistance;
current = calcCurrent(voltage, resistance);
cout << "The current charge is " << current << "\n\n";
continue;
}
if ( choose == 3) {
cout << "\nEnter the voltage: ";
cin >> voltage;
cout << "Enter the current charge: ";
cin >> current;
resistance = calcResistance(voltage, current);
cout << "The resistance of the flow is " << resistance << "\n\n";
continue;
}
}
cout << "\nThank you for using this program!\n";
return 0;
}
Comments
Leave a comment