Question

C++ Problem: Power Plant Data. The data file power1.dat contains a power plant output in megawatts...

C++ Problem:

Power Plant Data. The data file power1.dat contains a power plant output in megawatts over a period of 10 weeks. Each row of data contains 7 floating-point numbers that represent 1 week's data. In developing the following programs, use symbolic constants NROWS and NCOLS to represent the number of rows and columns in the array used to store the data.

Write a function to compute and return the average value in a two-dimensional vector of type double. Assume that the corresponding function prototype is

double avgVec(const vector<vector<double> > &x);

This is a screenshot of the data file opened in notepad:

C++ Problem: Power Plant Data. The data file

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<bits/stdc++.h>
using namespace std;

int m,n;
double avgVec(const vector<vector<double> >& x)
{
double sum=0;
double count=0;

for (int i = 0; i < m; i++) {
  
for (int j = 0; j < n; j++) {
   // Add an element to sum
   sum+=x[i][j];
   count++;//increment count of number
}
  
  
}
double avg=sum/count; //Calculate average
return avg;

}

int main(int argc, char const *argv[])
{
   vector<vector<double> > matrix; //create 2-D vector

   ifstream file("power1.dat",ios_base::in);//open file
  

  

  

  
   file>>m;//no of columns
   file>>n;//no of rows
for (int i = 0; i < m; i++) {
vector<double> row; // Create an empty row
for (int j = 0; j < n; j++) {
   double d=0;
   file>>d;
row.push_back(d); // Add an element (column) to the row
}
matrix.push_back(row); // Add the row to the main vector
}

double resultavg=avgVec(matrix);
cout<<"Avearge is "<<resultavg;
   return 0;
}

===========================================

For simplicity i took file as

power1.dat

4 4
4 4 4 4
4 4 4 4
4 4 4 4
4 4 4 4

output:

Avearge is 4

Add a comment
Know the answer?
Add Answer to:
C++ Problem: Power Plant Data. The data file power1.dat contains a power plant output in megawatts...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Submission Instruction Complete the following C++ programs. The assignment contains only one file with all different...

    Submission Instruction Complete the following C++ programs. The assignment contains only one file with all different class and functions from problem 1. The main function calls different functions as instructed in the problem description. Submit the CPP file during submission Problem 1. Define a class for a type called Fraction. This class is used to represent a ratio of two integers. Include mutator functions that allow the user to set the numerator and the denominator (one for each data). Also...

  • Can you help us!! Thank you! C++ Write a program that can be used by a...

    Can you help us!! Thank you! C++ Write a program that can be used by a small theater to sell tickets for performances. The program should display a screen that shows which seats are available and which are taken. Here is a list of tasks this program must perform • The theater's auditorium has 15 rows, with 30 seats in each row. A two dimensional array can be used to represent the seats. The seats array can be initialized with...

  • ================Data Structures C++=============== – Implement the Eight Queens Problem This is a...

    ================Data Structures C++=============== – Implement the Eight Queens Problem This is an object oriented program. This is #1 on page 187 of your book with ONE difference, I’m requiring you add a client program to test your code (a main). If you have the old book the question says “Complete the Queen and Board class for the Eight Queens problem.” On page 179 of your book is a function placeQueens that solves the Eight Queens problem. On page 180 of...

  • write a code on .C file Problem Write a C program to implement a banking application...

    write a code on .C file Problem Write a C program to implement a banking application system. The program design must use a main and the below functions only. The program should use the below three text files that contain a set of lines. Sample data of these files are provided with the assessment. Note that you cannot use the library string.h to manipulate string variables. For the file operations and manipulations, you can use only the following functions: fopen(),...

  • Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that...

    Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that will process monthly sales data for a small company. The data will be used to calculate total sales for each month in a year. The monthly sales totals will be needed for later processing, so it will be stored in an array. Basic Logic for main() Note: all of the functions mentioned in this logic are described below. Declare an array of 12 float/doubles...

  • Assignment Predator / Prey Objectives Reading from and writing to text files Implementing mathematical formulas in...

    Assignment Predator / Prey Objectives Reading from and writing to text files Implementing mathematical formulas in C++ Implementing classes Using vectors Using command line arguments Modifying previously written code Tasks For this project, you will implement a simulation for predicting the future populations for a group of animals that we’ll call prey and their predators. Given the rate at which prey births exceed natural deaths, the rate of predation, the rate at which predator deaths exceeds births without a food...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT