Advertisement
makispaiktis

RESISTORS

Apr 15th, 2018 (edited)
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     cout << "This is a problem about calculating currents and voltages of resistors." << endl;
  8.     cout << "First, give me the number of resistors." << endl;
  9.     int n;
  10.     cin >> n;
  11.  
  12.     cout << "Now, press 1 for in series connection or 2 for parallel connection."  << endl;
  13.     int option;
  14.     cin >> option;
  15.  
  16.     float matrix[n];
  17.     float rwhole;
  18.     float Vs;
  19.  
  20.     switch (option){
  21.  
  22.     case 1:
  23.  
  24.       rwhole=0;
  25.       for (int i=0; i <n; i++){
  26.  
  27.        cout << "Give me the resistance of resistor " << (i+1) << " in Ohm:" <<endl;
  28.        int r1;
  29.        cin >> r1;
  30.        matrix[i]= r1;
  31.        rwhole+= matrix[i];
  32.       }
  33.  
  34.  
  35.       cout << "The whole resistance of the " << n << " resistors is: " << rwhole << " Ohm."<< endl << endl <<endl;
  36.  
  37.       cout << "Now, I will calculate the currents and voltages of the resistors." << endl;
  38.       cout << "I want to tell me the voltage of battery-source." << endl;
  39.       cin >> Vs;
  40.       cout << "About current intensity: " << endl;
  41.       cout << "I= " ;
  42.  
  43.       for (int j=0; j<n; j++){
  44.         cout << "I" << (j+1) << " =";
  45.       }
  46.  
  47.     cout << Vs/rwhole << " Amber(A)." << endl;
  48.     cout << "About each resistor's voltage: " << endl;
  49.  
  50.     for(int z=0; z <n; z++){
  51.  
  52.         cout << "V" << (z+1) << "= "  << matrix[z]* Vs/ rwhole << " Volts(V)." << endl;
  53.     }
  54.     break;
  55.  
  56.  
  57.     case 2:
  58.  
  59.     rwhole=0;
  60.     for (int i=0; i <n; i++){
  61.  
  62.        cout << "Give me the resistance of resistor " << (i+1) << " in Ohm:" <<endl;
  63.        int r1;
  64.        cin >> r1;
  65.        matrix[i]= r1;
  66.        }
  67.  
  68.       int temp=0;
  69.       for (int a=0; a<n; a++){
  70.  
  71.         temp+= 1/ matrix[a];
  72.       }
  73.  
  74.       rwhole= 1/temp;
  75.       cout << "The whole resistance of the " << n << " resistors is: " << rwhole << " Ohm."<< endl << endl <<endl;
  76.  
  77.       cout << "Now, I will calculate the currents and voltages of the resistors." << endl;
  78.       cout << "I want to tell me the voltage of battery-source." << endl;
  79.       cin >> Vs;
  80.  
  81.       cout << "About voltages: " << endl;
  82.       cout << "V= " ;
  83.  
  84.       for (int j=0; j<n; j++){
  85.         cout << "V" << (j+1) << " =";
  86.       }
  87.  
  88.     cout << Vs << " Volts(V)." << endl;
  89.  
  90.     cout << "About each resistor's current intensity: " << endl;
  91.  
  92.     for(int z=0; z<n; z++){
  93.  
  94.         cout << "I" << (z+1) << "= "  << Vs/ matrix[z] << " Ambers(A)." << endl;
  95.     }
  96.  
  97.     break;
  98.  
  99.     default:
  100.     ;
  101.     }
  102.  
  103.     return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement