I need to get the 2D array to output the minimum value of each row, however, I am only able to output the averages of each row. Can someone help me figure out what I'm doing wrong? I only get the output of 0 for each week. Here's what the output is supposed to look like:
Week 1, day 5 was a minimum usage day with a usage level of 22
Week 2, day 2 was a minimum usage day with a usage level of 60
Week 3, day 5 was a minimum usage day with a usage level of 21
Week 4, day 6 was a minimum usage day with a usage level of 117
and so on so forth.
Here's what the text file is from Notepad:
207 301 222 302 22 167 125
367 60 120 111 301 499 434
211 62 441 192 21 293 316
401 340 161 297 441 117 206
448 111 370 220 264 444 207
21 313 204 222 446 401 337
213 208 444 321 320 335 313
162 137 265 44 370 315 322
150 218 234 384 283 199 204
204 245 287 298 302 288 297
Code:
/********************************************************************************
Author: Jordan Velez
Program Name: Lab #10 Chapter 8 - Two-Dimensional Arrays - Power Plant Data
Date: 4/18/2015
Program Description: This program reads a data file into a 2D array. The function will then determine the average and minimum values of each row, and print them out to the console.
******************************************************************************/
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
using namespace std;
const int NCOLS = 7;
const int NROWS = 10;
double row_ave(double x[][NCOLS], int row);
double row_min(double y[][NCOLS], int row);
int main()
{
/* Define variables. */
double power_out[NROWS][NCOLS], rowAvg = 0, sum = 0, rowMin = 0;
int row, col;
ifstream power;
/* Open input file. */
power.open("power1.dat");
/* Read data. */
for (row = 0; row < NROWS; row++)
{
for (col = 0; col < NCOLS; col++)
{
power >> power_out[row][col];
}
}
power.close();
cout << fixed << setprecision(3);
/* Find row min for a given row. */
for (row = 0; row < NROWS; row++)
{
rowMin = row_min(power_out, row);
cout << "Week " << setw(2) << row + 1 << ": Min Power Output in Megawatts: "
<< rowMin << endl;
}
cout << "\nWeeks 1-10: Minimum Power Output in Megawatts: "
<< rowMin << endl;
/* Find row average for a given row. */
for (row = 0; row<NROWS; row++)
{
rowAvg = row_ave(power_out, row);
cout << "Week " << setw(2) << row + 1 << ": Average Power Output in Megawatts: "
<< rowAvg << endl;
sum += rowAvg;
}
cout << "\nWeeks 1-10: Average Power Output in Megawatts: "
<< sum / NROWS << endl;
system("Pause");
/* Exit program. */
return 0;
}
/****************************************************************************************************/
double row_ave(double x[][NCOLS], int row)
{
/* Declare variables. */
int col;
double sum = 0;
/* compute row sum. */
for (col = 0; col < NCOLS; col++)
sum += x[row][col];
/* compute row average. */
return sum / NCOLS;
}
double row_min(double y[][NCOLS], int row)
{
/* Declare variables. */
int col;
int rowMin = 0;
/* compute row min. */
for (int i = 0; i < row; i++) {
if (y[i] < y[rowMin]) {
rowMin = i;
}
}
return rowMin;
}