fixed my errors, compiles but doesn't run the way i want it to.
k...here is what im trying to do..never really gave a proper description of my program :P
Trying to write a program to calculate the resistance connected either in series or parallel. Using arrays in solving the problem/
Prompt the user to select series or parallel.
Promtp the user to enter the number of resistors connected.
Read/get the value of each of the resistors
calculate the resistance.
Output the resistance according to the following:
Print Output Format: Print total resistance followed by the values of each resistor.
For series: (Example)
Total series resistance is 2.123 kOhm for 3 resistors:
1000 kOhm, 1000 kOhm, 123 kOhm
For parallel: (Example)
Total series resistance is 500 Ohm for 2 resistors:
1000 Ohm, 1000 Ohm
Avoid division by 0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char type;
//declare a character type variable
double total_resistance=0;
//initialize double total_resistance=0
double resistor_value=0;
//initialize double resistor_value=0
int number_of_resistors_connected=0;
//declare number_of_resistors_connected=0
double *r; //A pointer to hold the address of your dynamically-allocated "array"
while (true)
{
cout << "Please input 's' for series OR 'p' for resistors parallel"
<< endl;
cin >> type;
cout << "Please enter the number of resistors connected" << endl;
cin >> number_of_resistors_connected;
if (!cin || (number_of_resistors_connected <= 0)) {
cout << "Invalid input. Must be a positive integer." << endl;
}
r = new double[number_of_resistors_connected];// Storage to hold the resistors' values
if (type == 's') {
total_resistance = 0.0;
for (int i = 0; i < number_of_resistors_connected; i++) {
cout << "Input value of series resistor number " << i + 1 << endl;
cin >> r[i];
if (!cin) {
cout << "Invalid input." << endl;
}
total_resistance += r[i];
}
}
if (type == 'p') {
total_resistance = 0.0;
for (int i = 0; i < number_of_resistors_connected; i++) {
cout << "Input value of parallel resistor number " << i + 1 << endl;
cin >> r[i];
if (!cin) {
cout << "Invalid input." << endl;
}
total_resistance += r[i];
}
}
}
{
int array [] = {1, 2, 3, 4, 5, 6};
int length = sizeof(array) / sizeof(int);
for(int i = 0; i < length; i++) {
cout << "Resistor: " << length<< endl;
}
if (total_resistance<1000) //if total_resistance<1000
{
double x=0; //declare x=0
x=total_resistance;
//declare c = total resistance
cout<<"The total resistance is less than 1000 and its value is "<<x<<" Ohm"<<endl; //prompt message on screen
}
else //false statement
{
total_resistance=total_resistance/1000;
//total_resistance=total_resistance/1000
cout<<fixed<<showpoint<<setprecision(3);
cout<<"The total resistance is greater than 1000 and its value is "<<total_resistance<<" kOhm"<<endl;
//prompt message on screen
}
delete [] r;
return 0;
//indicate program ended successfully
}
}
| |