Advertisement
khaiwen1111

mw lab

Jan 6th, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <string>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7.  
  8. void display_header(void);
  9. void display_result(double atime, double ampere, double voltage);
  10. void display_max_result(double m_vol, double m_cur, double m_time);
  11.  
  12. int main()
  13. {
  14.     int j, counter;
  15.     double r, v, i_max, f, t, max_current, max_time, max_voltage = 0, i, time, trig, deg, atime;
  16.     const double pi = 3.14159265359;
  17.     char again;
  18.     do
  19.     {
  20.         cout << "Enter your resistance value,R: ";
  21.         cin >> r;
  22.         cout << "Enter your Imax value,Im  (in Ampere): ";
  23.         cin >> i_max;
  24.         cout << "Enter your frequency,f (in frequency): ";
  25.         cin >> f;
  26.  
  27.         display_header();
  28.  
  29.  
  30.         for (t = 0; t <= 10; t++)
  31.         {
  32.             time = 100 * (t / f);
  33.             atime = time * 0.001;
  34.             trig = 2 * pi * f * (atime);
  35.             i = i_max * sin(trig);
  36.             v = i * r;
  37.  
  38.             display_result(time, i, v);
  39.  
  40.             if (v > max_voltage)
  41.             {
  42.                 max_voltage = v;
  43.                 max_time = time;
  44.                 max_current = i;
  45.             }
  46.         }
  47.  
  48.         display_max_result(max_voltage, max_voltage, max_time);
  49.         cout.flush();
  50.         cin.ignore();
  51.         cout << endl << "Do you wish to continue ? (Y/N)";
  52.         cin >> again;
  53.     } while (again == 'Y' || again == 'y');
  54.  
  55.     return 0;
  56.  
  57. }
  58. void display_header(void)
  59. {
  60.     cout << showpoint << right;
  61.     cout << endl << setw(12) << "Time(ms)" << setw(15) << "Current(A)" << setw(15) << "Voltage(V)" << endl;
  62. }
  63. void display_result(double atime, double ampere, double voltage)
  64. {
  65.     cout << fixed;
  66.     cout << setw(12) << setprecision(2) << atime << setw(15) << setprecision(4) << ampere << setw(15) << setprecision(4) << voltage << endl;
  67. }
  68. void display_max_result(double m_vol, double m_cur, double m_time)
  69. {
  70.     cout << endl << "Vmax is: " << m_vol << "V occurs with current " << m_cur << " at time " << m_time;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement