Students in Programming 1 would like to understand the concept of Ohm’s law. You are required to write an application that will help students to calculate the voltage in an electric circuit using the formula below.
V = IR
Where V= voltage
I = Current
R- Resistance
Voltage
Current
0.62
14.89
13.21
16.55
18.62
9.47
6.58
18.32
12.15
3.98
Resistance
4.0
8.5
6.0
7.35
9.0
15.3
3.0
5.4
2.9
4.8
Write a C++ application that will help the students to understand the concepts:
4.1 Create a C++ called Circuit and save it in a file called Circuit.cpp.
4.2 Declare all necessary variables and constants.
4.3 Declare two parallel arrays named current, resistance and initialize them using the information given above.
4.4 Declare another parallel array called voltage populate by calling using default values.
4.5 Create a menu that will iterate until the user enters an invalid number. Create this menu by calling the function menus. There are five possible menu selections numbered from 1 to 7, if the user enters 8 or any other number the program must terminate.
NB: Make use of the switch statement for the menu selection.
Menu Option Description
1
Display all
2
Search volts:
The user must be prompted to enter the volts that he or she would like to search for.
The volts must then be searched in the corresponding array.
o If the value is found or not an appropriate message must be displayed
3
Search volts and indicate where it was found
The user must be prompted to enter the volts that he or she would like to search for.
The volts must then be searched in the corresponding array.
o If the value is found it must display the value, the index at which it was found as well as the number of eminent for that particular voltage or an appropriate message must be displayed
4
Find the highest voltage
Display the highest voltage.
5
Find highest voltage with corresponding details
Display the highest volts with their corresponding details
6
Display average voltage
Calculate and display average voltage.
7
Display number of measurements that are above the average voltage
8 or any other key
#include <iostream>
using namespace std;
void display(double *r, double *c, double *v)
{
for(int i = 0; i < 10; i++)
{
cout << "Current: " << c[i] << " Resistance: " << r[i] << " Voltage: " << v[i] << endl;
}
}
void search(double *v, double find)
{
for(int i = 0; i < 10; i++)
{
if(v[i] == find)
{
cout << "Find!" << endl;
return;
}
}
cout << "Not found!" << endl;
}
void searchIndex(double *v, double find)
{
for(int i = 0; i < 10; i++)
{
if(v[i] == find)
{
cout << "Find!" << " Index of element is: " << i << endl;
return;
}
}
cout << "Not found!" << endl;
}
void searchHighVoltage(double *v)
{
double max = v[0];
for(int i = 1; i < 10; i++)
{
if (v[i] > max) max = v[i];
}
cout << "The highest voltage is: " << max << endl;
}
void searchHighVoltageDetails(double *v)
{
double max = v[0];
int index = 0;
for(int i = 1; i < 10; i++)
{
if (v[i] > max)
{
max = v[i];
index = i;
}
}
cout << "The highest voltage is: " << max << " its index is: " << index << endl;
}
void averageVoltage(double *v)
{
int sum = 0;
for(int i = 0; i < 10; i++)
{
sum += v[i];
}
cout << "Average voltage is: " << sum/10 << endl;
}
void averageVoltageAbove(double *v)
{
int sum = 0;
for(int i = 0; i < 10; i++)
{
sum += v[i];
}
double average = sum/10;
int index = 0;
for(int i = 0; i < 10; i++)
{
if(v[i] > average) index++;
}
cout << "Number of measurements that are above the average volatge : " << index << endl;
}
int main()
{
double resistance[] = {4.0, 8.5, 6.0, 7.35, 9.0, 15.3, 3.0, 5.4, 2.9, 4.8};
double current[] = {0.62, 14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.98};
double voltage[10];
for(int i = 0; i < 10; i++)
{
voltage[i] = resistance[i] * current[i];
}
int choice = 0;
double find = 0;
for(;;)
{
cout << "*****MENU*****" << endl;
cout << "1. Display all circuit." << endl;
cout << "2. Search volts: (Enter a value)." << endl;
cout << "3. Search volts and indicate where it was found : (Enter a value)." << endl;
cout << "4. Find the highest voltage." << endl;
cout << "5. Find highest volatge with corresponding details" << endl;
cout << "6. Display average volatge" << endl;
cout << "7. Display number of measurements that are above the average volatge" << endl;
cout << "Enter 8 or any key to exit." << endl;
cout << "Enter your choice: ";
cin >> choice;
if(choice < 1 || choice > 7) break;
switch(choice)
{
case 1:
display(resistance, current, voltage);
break;
case 2:
cout << "Enter value which you want to find: ";
cin >> find;
search(voltage, find);
break;
case 3:
cout << "Enter value which you want to find: ";
cin >> find;
searchIndex(voltage, find);
break;
case 4:
searchHighVoltage(voltage);
break;
case 5:
searchHighVoltageDetails(voltage);
break;
case 6:
averageVoltage(voltage);
break;
case 7:
averageVoltageAbove(voltage);
break;
}
}
return 0;
}
Comments
Leave a comment