How to display prefixes alongside certain values in an output table with c++ My
ID: 3682215 • Letter: H
Question
How to display prefixes alongside certain values in an output table with c++
My program allows a user to input certain values for E,R,L,C, f initial and f final for ac circuit analysis. When prompted the user should type 15 for "E", 5000 for "R", 20m for "L", 0.02u for "C", 100hz for "Initial Frequency" and 1M for "Final Frequency".
This should be a simple answer. I am trying to get the symboll for the prefixs "ohms, kilo ohms and M ohms" in the correct places that correspond with columns "XC, XL and Z". As you can see in the picture of the output window all my smbols are showing up on the far left side. What do I need to do in order to get these into the correct columns?
Use prefixes to display the values of XC, XL, and Z in the output table as follows:
if value < 1000 - express in Ohms (?)
if 1000 < value < 1000000 - express in kiloOhms (k?)
if value > 1000000 - express in MegaOhms (M?)
Here is my Program
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
using namespace std;
//function prototype
//XL
double XL(int, double);Â Â // function for inductive reactance
//XC
double XC(int, double); // function for capacitive reactance
//Z
double Z(int, double, double); // function for total circuit impedance
//I
double I(double, double); // function for current
void MaxI(double,int,double&,int&); // function for finding maximum current and corresponding frequency
  const double PI = acos(-1.0); //global constant decalred outside of main program so as to not be overwritten
int main() {
 Â
  double L,C,E,Max_C,Max_F;
  int fi=0,ff=0,f;
  int frequncey,R;
  char Answer;
  string unit, unit2, unit3, unit4, u, m, prefix1, prefix2, prefix3;
  double max_current=0;
  int maxf;
 Â
  do
  {
 Â
  cout<<"Enter E,R,L,C: "<   cout<<"Enter vaule for E - Sinusodial voltage in Volts (V): ";
  cin>>E;                                                         // input for sinusoidal voltage in volts
  cout<<"Enter vaule for R - Resistance in Ohms (ê): ";
  cin>>R;                                                           // input for resistance in ohms
  cout<<"Enter value for L - Inductance in Henries (H) (use mH or uH): ";
  cin>>L>> unit;                                                      // input for indutance in henries and unit input
  cout<<"Enter value for C - Capacitance in Farads (F) (use mF, uF or nF): ";
  cin>>C>> unit2;                                                    // input for capacitance in farads
  cout<<"Enter Initial Frequency in Hertz (Hz) (Indicate either Hz, kHz, or MHz)"<   cin>>fi>> unit3;                                                  // input for initial frequency
  cout<<"Enter Final Frequency in Hertz (Hz) (Indicate either Hz, kHz, or MHz)"<   cin>>ff>> unit4;                                                     // input for final frequency
 Â
  if (unit[0]=='m')
     L= L*pow(10, -3);
  else if (unit[0]=='u')
     L= L*pow(10, -6);
    Â
  if (unit2[0]=='m') Â
     C = C*pow(10,-3);    Â
  else if (unit2[0]=='u') Â
     C = C*pow(10,-6);
  else if (unit2[0]=='n')
     C = C*pow(10,-9);
    Â
  if (unit3[0]=='k')
     fi = fi*pow(10,3);
  else if (unit3[0]=='M')
     fi = fi*pow(10,6);
    Â
     if (unit4[0]=='k')
     ff = ff*pow(10,3);
  else if (unit4[0]=='M')
     ff = ff*pow(10,6); Â
 Â
  if ( E<=0 || R<=0 || L<=0 ||C <=0 )
  {
  cout<<"Error Bad Input-Value Cannot Be Negative: Try Again! "<    }
/* else if(unit != m || unit !=u)
   {
   cout<<"Error! Use the Proper Units: Try Again! "<   }
  else if(fi !>= 1 || ff !<= 1000000000 || ff >= 2*fi)
  {
  cout<<"Input Error! Use the Correct Values! "<   }*/
   else
  {
  cout<< "____________________________________________________________________"<   cout<< "|  Freq (Hz)  "<<"|  XC (ê)  "<<"|  XL (ê)  "<<"|  Z (ê)  "<<"|  I (mA)  |"<   cout<< "--------------------------------------------------------------------"<   }
//Â Â cout<
  for (f=fi; f<=2*ff; f=f*2)
  {
     double inductance;              // function call for XL
     inductance = XL(f,L);
     double capacitance;           // function call for XC
     capacitance = XC(f,C);
     double total_impedance;     // function call for Z
     total_impedance = Z(R,inductance,capacitance);
       Â
     double current;              // function call for I
     current = I(E,total_impedance);
 Â
     MaxI(current,f,max_current,maxf); // function call to find max output
    Â
 Â
    if (inductance <= 999)
     {
     cout<<"ê"; Â
     }
     else if (inductance >= 1000 || inductance <=99999)
     {
     cout<<"kê"; Â
     }
     else if (inductance >= 1000000)
     {
     cout<<"Mê"; Â
     } Â
    Â
     cout<       <     Â
  }
 Â
 Â
  cout<< "The Maximum value of the current is: "<   cout<<"The corresponding frequency is: "<  Â
  cout<<"*****WOULD YOU LIKE TO RUN THE PROGRAM AGAIN???*****: (Enter Y or N): ";  // end of do while loop to run the program agian
  cin>>Answer;
 Â
  }
  while (Answer == 'Y' || Answer == 'y');
   return 0;
}
double XL(int frequency, double inductor)Â Â Â Â Â Â Â Â Â Â Â // R = Resistance (Ohms), L = Inductance (H), C = Capacitance (F), I = Current (A), E = Sinusoidal voltage (V), f = Frequency in Hertz (Hz)
{
  return 2*PI*frequency*inductor; Â
}
double XC(int frequency, double capacitor)
{
  return -1/(2*PI*frequency*capacitor);
}
double Z(int resistance, double inductor, double capacitor)Â Â Â Â // Z= total circuit impedance
{
  return sqrt(pow(resistance,2) + pow(inductor+capacitor,2));
}
double I(double volts, double impedance)
{
  return volts/impedance;
}
void MaxI(double current,int f,double& max_current ,int& maxf)
{
  if (current>max_current)
  {
 Â
     max_current = current;
     maxf = f;
  }
}
Explanation / Answer
Necessary changes have been made to the code:
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
using namespace std;
//function prototype
//XL
double XL(int, double); // function for inductive reactance
//XC
double XC(int, double); // function for capacitive reactance
//Z
double Z(int, double, double); // function for total circuit impedance
//I
double I(double, double); // function for current
void MaxI(double,int,double&,int&); // function for finding maximum current and corresponding frequency
const double PI = acos(-1.0); //global constant decalred outside of main program so as to not be overwritten
int main() {
 Â
double L,C,E,Max_C,Max_F;
int fi=0,ff=0,f;
int frequncey,R;
char Answer;
string unit, unit2, unit3, unit4, u, m, prefix1, prefix2, prefix3;
double max_current=0;
int maxf;
 Â
do
{
 Â
cout<<"Enter E,R,L,C: "< cout<<"Enter vaule for E - Sinusodial voltage in Volts (V): ";
cin>>E; // input for sinusoidal voltage in volts
cout<<"Enter vaule for R - Resistance in Ohms (ê): ";
cin>>R; // input for resistance in ohms
cout<<"Enter value for L - Inductance in Henries (H) (use mH or uH): ";
cin>>L>> unit; // input for indutance in henries and unit input
cout<<"Enter value for C - Capacitance in Farads (F) (use mF, uF or nF): ";
cin>>C>> unit2; // input for capacitance in farads
cout<<"Enter Initial Frequency in Hertz (Hz) (Indicate either Hz, kHz, or MHz)"< cin>>fi>> unit3; // input for initial frequency
cout<<"Enter Final Frequency in Hertz (Hz) (Indicate either Hz, kHz, or MHz)"< cin>>ff>> unit4; // input for final frequency
 Â
if (unit[0]=='m')
L= L*pow(10, -3);
else if (unit[0]=='u')
L= L*pow(10, -6);
 Â
if (unit2[0]=='m')
C = C*pow(10,-3);
else if (unit2[0]=='u')
C = C*pow(10,-6);
else if (unit2[0]=='n')
C = C*pow(10,-9);
 Â
if (unit3[0]=='k')
fi = fi*pow(10,3);
else if (unit3[0]=='M')
fi = fi*pow(10,6);
 Â
if (unit4[0]=='k')
ff = ff*pow(10,3);
else if (unit4[0]=='M')
ff = ff*pow(10,6);
 Â
if ( E<=0 || R<=0 || L<=0 ||C <=0 )
{
cout<<"Error Bad Input-Value Cannot Be Negative: Try Again! "< }
/* else if(unit != m || unit !=u)
{
cout<<"Error! Use the Proper Units: Try Again! "< }
else if(fi !>= 1 || ff !<= 1000000000 || ff >= 2*fi)
{
cout<<"Input Error! Use the Correct Values! "< }*/
else
{
cout<< "____________________________________________________________________"< cout<< "| Freq (Hz) "<<"| XC (ê) "<<"| XL (ê) "<<"| Z (ê) "<<"| I (mA) |"< cout<< "--------------------------------------------------------------------"< }
for (f=fi; f<=2*ff; f=f*2)
{
double inductance; // function call for XL
inductance = XL(f,L);
cout<<inductance<<prefix(inductance)<<" "<<flush;
double capacitance; // function call for XC
capacitance = XC(f,C);
cout<<capacitance<<prefix(capacitance)<<" "<<flush;
double total_impedance; // function call for Z
total_impedance = Z(R,inductance,capacitance);
cout<<total_impedance<<prefix(total_impedance)<<" "<<flush;
double current; // function call for I
current = I(E,total_impedance);
cout<<current<<endl;
MaxI(current,f,max_current,maxf); // function call to find max output
cout< < Â Â
}
 Â
cout<< "The Maximum value of the current is: "< max_current<<endl;
cout<<"The corresponding frequency is: "< maxf<<endl;
 Â
cout<<"*****WOULD YOU LIKE TO RUN THE PROGRAM AGAIN???*****: (Enter Y or N): "; // end of do while loop to run the program agian
cin>>Answer;
 Â
}
while (Answer == 'Y' || Answer == 'y');
return 0;
}
double XL(int frequency, double inductor) // R = Resistance (Ohms), L = Inductance (H), C = Capacitance (F), I = Current (A), E = Sinusoidal voltage (V), f = Frequency in Hertz (Hz)
{
return 2*PI*frequency*inductor;
}
double XC(int frequency, double capacitor)
{
return -1/(2*PI*frequency*capacitor);
}
double Z(int resistance, double inductor, double capacitor) // Z= total circuit impedance
{
return sqrt(pow(resistance,2) + pow(inductor+capacitor,2));
}
double I(double volts, double impedance)
{
return volts/impedance;
}
void MaxI(double current,int f,double& max_current ,int& maxf)
{
if (current>max_current)
{
 Â
max_current = current;
maxf = f;
}
}
void prefix(double value)
{
if (value <= 999)
{
cout<<"ê";
}
else if (value >= 1000 || value <=99999)
{
cout<<"kê";
}
else if (value >= 1000000)
{
cout<<"Mê";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.