Theory/Algorithm
Question 4
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
4.4 Create a C++ source file called OLaw2 and save it in a file called OLaw2.cpp.
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.
Display the desired measurement.
Question 5
A student in Programming 1 wants to know the surface area and volume of a cylindrical shape.
Figure 5.1: Example of a cylinder
Volume = PI(R^²) x height
Area = 2PI(R^²) x height + 2PI(R^²)
5.1.1 Write a program that will assist the students:
5.1.2Create a C++ source file called Cylinder and save it in a file called Cylinder.cpp.
5.2 Create the following functions:
5.2.1 calcVolume() This function will receive three parameters and it must then calculate
and return the volume of a cylinder using the information given
above.5.2.2 calcArea() This function will receive three parameters and it must then calculate
and return the area of a cylinder using the information given above.5.2.3 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 height of the cylinder, and the radius of
the base of the cylinder.
Based on the users option :
o Do the relevant calculations by calling the correct
function.
Display the results of the desired measurement.
If an invalid option is selected an appropriate error message
must be displayed, see Figure 5.5.
NB: Use a switch for the selection
o This process must be repeated until there no calculations to
perform (N/n).5.3 Re-write program 5.2.
5.3.1 calcValues() This function will receive four parameters and it must then calculate
and the volume and the area of a cylinder using the information given
above.5.3.2 promptValues() This function will receive three parameters and it must then prompt
the user for the required input as indicated in Figure 5.2.5.3.3 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 height of the cylinder, and the radius of
the base of the cylinder.
Calculation the volume and area by calling the correct
function.
Display the results the volume and area.
Due date: 14 February 2021
#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();
//Main
int main()
{
getInput();
return 0;
}
//Voltage
double calcVoltage(double I, double R)
{
return I*R;
}
//Current
double calcCurrent(double U, double R)
{
return U/R;
}
//Resistance
double calcResistance(double U, double I)
{
return U/I;
}
//Circuit
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);
}
}
//Get user Input
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;
}
else 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;
}
else 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;
}
}
#include <iostream>
using namespace std;
int calcValues();
void promptValues();
double calcVolume(double PI, double R, double height);
double calcArea(double PI, double R, double height);
double calcValues(double PI, double height, double R);
int main()
{
promptValues();
}
//Calc values
double calcValues(double PI, double height, double R )
{
cout<<"Area is : "<<(2 * PI * (R * R)) + (2 * PI * (R * R))<<endl;
cout<<"Volume is : "<<PI * (R * R) * height<<endl;
}
double calcArea(double PI, double R, double height)
{
return (2 * PI * (R * R)) + (2 * PI * (R * R));
}
double calcVolume(double PI, double R, double height)
{
return PI * (R * R) * height;
}
void promptValues()
{
char c;
while(true)
{
cout << "Enter what type of calculation you need (A- Area, V -Volume , B- Both Area and Volume): ";
cin >> c;
if(c == 'a' || c == 'A' || c == 'v' || c == 'V' || c == 'b' || c == 'B') break;
cout << "That is incorrect character. Try again." << endl;
}
//Area
if(c == 'a' || c == 'A')
{
const double PI = 3.142;
cout << "Enter radius: ";
double R;
cin >> R;
cout << "Enter height: ";
double height;
cin >> height;
cout << "Area is: " << calcArea(PI, R, height) << endl;
return;
}
//Volume
else if(c == 'v' || c == 'V')
{
const double PI = 3.142;
cout << "Enter radius: ";
double R;
cin >> R;
cout << "Enter height: ";
double height;
cin >> height;
cout << "Voltage is: " << calcVolume( PI, R, height) << endl;
return;
}
else if(c == 'b' || c == 'B')
{
const double PI = 3.142;
cout << "Enter raidus: ";
double R;
cin >> R;
cout << "Enter height: ";
double height;
cin >> height;
cout << "Voltage is: " << calcValues( PI, height, R ) << endl;
return;
}
}
Comments
Leave a comment