Write all answers for Question 2 in C++ syntax.
3.2
3.2.1 Write a C++ program that will randomize three values and determine and display
which one is the highest and which one is the lowest. Range (From 1 to 350)
3.2.2 Re-write 3.2.1 by using two functions, one called determineLarge and one
called determineSmall.
3.2.3 Re-write 3.2.1 by using a single function determineLargeSmall.
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).
3.2.1
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main () {
srand(time(NULL));
int number1, number2, number3;
// initilaize variables to random numbers [1, 350]
number1 = 1 + rand () % 350;
number2 = 1 + rand () % 350;
number3 = 1 + rand () % 350;
cout << "Number1: " << number1 << "\tNumber2: " << number2 << "\tNumber3: " << number3 << endl;
int max, min;
if (number1 > number2) {
max = number1;
min = number2;
}
else {
max = number2;
min = number1;
}
if (max < number3) {
max = number3;
}
if (min > number3) {
min = number3;
}
cout << "The minimum number among three numbers: [" << number1 << " " << number2 << " " << number3 << "] => " << min << endl;
cout << "The maximum number among three numbers: [" << number1 << " " << number2 << " " << number3 << "] => " << max << endl;
}
3.2.2
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int determineLarge (int a, int b, int c) {
int max;
if (a > b) {
max = a;
}
else {
max = b;
}
if (max < c) {
max = c;
}
return max;
}
int determineSmall (int a, int b, int c) {
int min;
if (a > b) {
min = b;
}
else {
min = a;
}
if (min > c) {
min = c;
}
return min;
}
int main () {
srand(time(NULL));
int number1, number2, number3;
// initilaize variables to random numbers [1, 350]
number1 = 1 + rand () % 350;
number2 = 1 + rand () % 350;
number3 = 1 + rand () % 350;
cout << "Number1: " << number1 << "\tNumber2: " << number2 << "\tNumber3: " << number3 << endl;
cout << "The minimum number among three numbers: [" << number1 << " " << number2 << " " << number3 << "] => " << determineSmall(number1, number2, number3) << endl;
cout << "The maximum number among three numbers: [" << number1 << " " << number2 << " " << number3 << "] => " << determineLarge(number1, number2, number3) << endl;
}
3.2.3
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void determineLargeSmall (int number1, int number2, int number3) {
int max, min;
if (number1 > number2) {
max = number1;
min = number2;
}
else {
max = number2;
min = number1;
}
if (max < number3) {
max = number3;
}
if (min > number3) {
min = number3;
}
cout << "The minimum number among three numbers: [" << number1 << " " << number2 << " " << number3 << "] => " << min << endl;
cout << "The maximum number among three numbers: [" << number1 << " " << number2 << " " << number3 << "] => " << max << endl;
}
int main () {
srand(time(NULL));
int number1, number2, number3;
// initilaize variables to random numbers [1, 350]
number1 = 1 + rand () % 350;
number2 = 1 + rand () % 350;
number3 = 1 + rand () % 350;
cout << "Number1: " << number1 << "\tNumber2: " << number2 << "\tNumber3: " << number3 << endl;
determineLargeSmall (number1, number2, number3);
return 0;
}
Comments
Leave a comment