Answer to Question #161335 in C++ for Kefilwe Mamabolo

Question #161335

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 he/she would like to see

(I/I, V/v, R/r)(See Figure 4.1).

Based on the option selected by the user 

o The user must be prompted for the related data as 

shown in Figure 4.2 – Figure 4.4.

o The program must then calculate the related 

measurements based on the option by calling the 

correct function.

o Display the desired measurement.

 If an invalid option is selected an appropriate error message 

must be displayed, see Figure 4.5.

NB: Use a switch for the selection

o This process must be repeated until there no calculations to 

perform (N/n).

4.3 Rewrite 4.2 as follows:

4.3.1 Create a C++ source file called OLaw2 and save it in a file called OLaw2.cpp.

4.4 Create the following functions: 

4.3.1 calcCicuirt() This function must calculate the voltage, current and the resistance 

in a circuit using the information given above.

4.3.2 getInput() This function will prompt the user for the required input as indicated 

in Figure 4.1.

4.3.3 main() NB: The functions must be implemented below the main.

Add all necessary pre-processor directives.

 Declare all constants and necessary variables.

 Prompt the user for the input values by calling the correct 

function.

 The program must then calculate the required 

measurements by calling the correct function.


1
Expert's answer
2021-02-05T01:39:17-0500
#include <iostream>
//******************************************
using namespace std;
//******************************************
double calcVoltage(double I, double R);
double calcCurrent(double U, double R);
double calcResistance(double I, double U);
double calcCicuirt(double A, double B, char c);
void getInput();
//******************************************
int main()
{
	getInput();
	return 0;
}
//******************************************
double calcVoltage(double I, double R)
{
	return I*R;
}
//******************************************
double calcCurrent(double U, double R)
{
	return U/R;
}
//******************************************
double calcResistance(double U, double I)
{
	return U/I;
}
//******************************************
double calcCicuirt(double A, double B, char c)
{
	switch(c)
	{
	case 'v':
	case 'V':
		return calcVoltage(A, B);
	case 'i':
	case 'I':
		return calcCurrent(A, B);
	case 'r':
	case 'R':
		return calcResistance(A, B);
	}
}
//******************************************
void getInput()
{
	char c;
	while(true)
	{
		cout << "Enter what type of calculation you need (I, R, V): ";
		cin >> c;
		if(c == 'r' || c == 'R' || c == 'v' || c == 'V' || c == 'i' || c == 'I') break;
		cout << "That is incorrect character. Try again." << endl;
	}
	if(c == 'i' || c == 'I')
	{
		cout << "Enter voltage: ";
		double V;
		cin >> V;
		cout << "Enter resistance: ";
		double R;
		cin >> R;
		cout << "Current is: " << calcCicuirt(V, R, c) << endl;
		return;
	}
	if(c == 'v' || c == 'V')
	{
		cout << "Enter current: ";
		double I;
		cin >> I;
		cout << "Enter resistance: ";
		double R;
		cin >> R;
		cout << "Voltage is: " << calcCicuirt(I, R, c) << endl;
		return;
	}
	if(c == 'r' || c == 'R')
	{
		cout << "Enter current: ";
		double I;
		cin >> I;
		cout << "Enter voltage: ";
		double V;
		cin >> V;
		cout << "Voltage is: " << calcCicuirt(V, I, c) << endl;
		return;
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog