Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main()
- {
- cout << "This is a problem about calculating currents and voltages of resistors." << endl;
- cout << "First, give me the number of resistors." << endl;
- int n;
- cin >> n;
- cout << "Now, press 1 for in series connection or 2 for parallel connection." << endl;
- int option;
- cin >> option;
- float matrix[n];
- float rwhole;
- float Vs;
- switch (option){
- case 1:
- rwhole=0;
- for (int i=0; i <n; i++){
- cout << "Give me the resistance of resistor " << (i+1) << " in Ohm:" <<endl;
- int r1;
- cin >> r1;
- matrix[i]= r1;
- rwhole+= matrix[i];
- }
- cout << "The whole resistance of the " << n << " resistors is: " << rwhole << " Ohm."<< endl << endl <<endl;
- cout << "Now, I will calculate the currents and voltages of the resistors." << endl;
- cout << "I want to tell me the voltage of battery-source." << endl;
- cin >> Vs;
- cout << "About current intensity: " << endl;
- cout << "I= " ;
- for (int j=0; j<n; j++){
- cout << "I" << (j+1) << " =";
- }
- cout << Vs/rwhole << " Amber(A)." << endl;
- cout << "About each resistor's voltage: " << endl;
- for(int z=0; z <n; z++){
- cout << "V" << (z+1) << "= " << matrix[z]* Vs/ rwhole << " Volts(V)." << endl;
- }
- break;
- case 2:
- rwhole=0;
- for (int i=0; i <n; i++){
- cout << "Give me the resistance of resistor " << (i+1) << " in Ohm:" <<endl;
- int r1;
- cin >> r1;
- matrix[i]= r1;
- }
- int temp=0;
- for (int a=0; a<n; a++){
- temp+= 1/ matrix[a];
- }
- rwhole= 1/temp;
- cout << "The whole resistance of the " << n << " resistors is: " << rwhole << " Ohm."<< endl << endl <<endl;
- cout << "Now, I will calculate the currents and voltages of the resistors." << endl;
- cout << "I want to tell me the voltage of battery-source." << endl;
- cin >> Vs;
- cout << "About voltages: " << endl;
- cout << "V= " ;
- for (int j=0; j<n; j++){
- cout << "V" << (j+1) << " =";
- }
- cout << Vs << " Volts(V)." << endl;
- cout << "About each resistor's current intensity: " << endl;
- for(int z=0; z<n; z++){
- cout << "I" << (z+1) << "= " << Vs/ matrix[z] << " Ambers(A)." << endl;
- }
- break;
- default:
- ;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement