Write a C++ program for a specialized laboratory voltmeter that calculates and d
ID: 3738329 • Letter: W
Question
Write a C++ program for a specialized laboratory voltmeter that calculates and displays the RMS voltage for waveforms of any shape. Assume that the circuitry that converts the analog waveform to digital, stores the voltage in an array every 15 degrees. Exactly one full cycle, no more or less, is stored in the array. The program should use one function to calculate the RMS voltage and a second to display the results. A maximum of three decimal places should be displayed.
Since the waveform can be any shape (square, triangular, sine), you must calculate the RMS voltage by first squaring all the values in the array, then finding the mean (average), and then taking the square root of the mean. It is incorrect to find the RMS voltage using the formula:
V RMS = V Peak / ?2
as this is valid only for sine waves and the voltmeter should measure correctly for all waveforms.
Test your program by populating the array, first with the voltages for a symmetric square wave with 50% duty cycle (positive and negative amplitudes are equal) and then a sine wave.
Explanation / Answer
// to find RMS Voltage for waveforms
#include<stdio.h>
#include<iostream.h>
#include<math.h>
#include<iomanip.h>
class SplLabVoltmeter {
double *readings;
double rms;
int sz;
public:
SplLabVoltmeter(){
readings=NULL;
rms=0.0;
sz=0;
}
SplLabVoltmeter(int n)
{
readings=new double[n];
sz=n;
}
void getReadings()
{
cout<<" Enter Reading of Sine wave ";
for(int i=0;i<sz;++i)
{
cout<<" Enter Reading "<< (i+1)<<": ";
cin>>readings[i];
}
}
void calcRMSVoltage()
{
double sqsum=0.0;
double mean;
double *t= new double[sz];
for(int i=0;i<sz;++i)
{
t[i]=readings[i] * readings[i];
sqsum=sqsum+t[i];
}
mean=sqsum/(double)sz;
rms=sqrt(mean);
}
void showResults()
{
cout<<" "<<"Given Readings of Wave is";
for(int i=0;i<sz;++i)
cout<<" "<<readings[i];
cout<<" RMS Voltage is ....."<<setprecision(3)<<rms;
}
~SplLabVoltmeter(){};
};
void main()
{
int choice=0;
int r=0;
while(choice !=4)
{
cout<<" Which Wave 1. Sine 2. Traingular 3. Square Wave 4. Exit Enter Choice:";
cin>>choice;
switch(choice)
{
case 1 :
cout<<" How many Readings for Sine wave do you give : ";
cin>>r;
SplLabVoltmeter obj(r);
obj.getReadings();
obj.calcRMSVoltage();
cout<<" For Sine Wave... ";
obj.showResults();
break;
case 2 :
cout<<" How many Readings for Triangular wave do you give : ";
cin>>r;
SplLabVoltmeter obj1(r);
obj1.getReadings();
obj1.calcRMSVoltage();
cout<<" For Triangular Wave... ";
obj1.showResults();
break;
case 3 :
cout<<" How many Readings for Square wave do you give : ";
cin>>r;
SplLabVoltmeter obj2(r);
obj2.getReadings();
obj2.calcRMSVoltage();
cout<<" For Square Wave... ";
obj2.showResults();
break;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.