Unformatted text preview:

College of Engineering and Computer ScienceComputer Science DepartmentComputer Science 106 Computing in Engineering and ScienceSpring 2006 Course number: 11672 Instructor: Larry CarettoSolution to Third Quiz OverviewWrite, edit, compile, link, and debug a program to input all the elements of a current array and a voltage array. Use these values, with the formulae below, to compute the average power, the average of the current squared and the average of the voltage squared. Use these results to compute and print a quantity called the power factor, defined in an equation below.Program detailsDownload the quiz three data file, quizThreeData.dat, from the home page of the course web site. This file has a series of data points on voltage and current, taken at the same time. The file structure has a value of voltage followed by a corresponding value of current. To input this data file, you can write your own code or modify the input routine in the program provided for exercise eight, task one. Declare the array size to allow at least 3000 elements. Your program should read in all values of voltage and current and determine the number of points by checking for an end of file condition. Once you have read all the individual data points for voltage, Vk, and current, Ik, your program should compute the three quantities shown in the equations below: the root-mean-squared (RMS) voltage, Vrms, the RMS current, iRMS, and the average instantaneous power, Pavg.10102102111NkkkavgNkkRMSNkkRMSIVNPINIVNVOnce you have computed these quantities, you can compute a fourth quantity known as the power factor, PF, which is found as follows: RMSRMSavgIVPPF Your program should print out the values that you computed for VRMS, IRMS, Pavg, and PF. Solution#include <iostream>#include <fstream>#include <cmath>using namespace std;int main(){ ifstream inFile("data.dat"); const int MAX_DATA = 3000;Jacaranda (Engineering) 3333 Mail Code Phone: 818.677.6448E-mail: [email protected] 8348 Fax: 818.677.7062double v[MAX_DATA], i[MAX_DATA]; double vin, iin; inFile >> vin >> iin; int n = 0; while ( !inFile.fail() && n < MAX_DATA ) { v[n] = vin; i[n] = iin; n++; inFile >> vin >> iin; } // while loop above will give n = total entries with // subscripts from v[0] to v[n-1] and i[0] to i[n-1] // now, check to see if there are still more data to read inFile >> vin >> iin; if ( !inFile.fail() ) { while ( !inFile.fail() ) { n++; inFile >> vin >> iin; } cout << "More data remains on file; increase array size to " << n << " and rerun program." << endl; return EXIT_FAILURE; } // compute RMS values and average power double rmsV = 0; double rmsI = 0; double power = 0; for ( int k = 0; k < n; k++ ) { rmsV += v[k] * v[k]; rmsI += i[k] * i[k]; power += v[k] * i[k]; } rmsV = sqrt( rmsV / n ); rmsI = sqrt( rmsI / n ); power /= n; cout << "\n The RMS voltage is " << rmsV << " volts" << "\n The RMS current is " << rmsI << " amps" << "\nAverage instantaneous power is " << power << " watts" << "\n The power factor is " << power / ( rmsV * rmsI ) << endl; return EXIT_SUCCESS;}/* Results The RMS voltage is 2.13095 volts The RMS current is 3.09107 ampsJacaranda (Engineering) 3333 Mail Code Phone: 818.677.6448E-mail: [email protected] 8348 Fax: 818.677.7062Average instantaneous power is 6.04863 watts The power factor is 0.918278*/Jacaranda (Engineering) 3333 Mail Code Phone: 818.677.6448E-mail: [email protected] 8348 Fax:


View Full Document

CSUN COMP 106 - Solution to Third Quiz

Download Solution to Third Quiz
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Solution to Third Quiz and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Solution to Third Quiz 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?