Thread: 2D Array Problem - Finding each rows minimum value

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    6

    2D Array Problem - Finding each rows minimum value

    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;
    }


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > if (y[i] < y[rowMin])
    Compare with your average function, and the use of subscripts therein.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help finding the minimum value of an array
    By Kevin Nguyen in forum C Programming
    Replies: 5
    Last Post: 09-09-2013, 11:55 PM
  2. Finding maximum and minimum number in 2D array
    By wonderwall in forum C Programming
    Replies: 4
    Last Post: 10-23-2011, 10:21 PM
  3. Finding the minimum value in an array?
    By kabuatama in forum C Programming
    Replies: 8
    Last Post: 02-18-2006, 01:45 PM
  4. finding non-zero minimum in array
    By xstudent in forum C Programming
    Replies: 8
    Last Post: 12-16-2002, 03:58 AM
  5. finding minimum in array
    By xstudent in forum C# Programming
    Replies: 3
    Last Post: 12-15-2002, 12:23 AM

Tags for this Thread