I not know about 2d array Please help me to solve this problem.........................
Write a program that declares a 2-D array of 7 rows and 2 columns. The rows represent one day of the week. The first column is the maximum temperature of that day and the second columns is the minimum temperature of that day.

    #include iostream;

    using namespace std;

    int main ()
    {

        //[2] = two columns, [7] = 7 rows.
        int array[2][7];

        return 0;
    }

Edited 1 Year Ago by luis.baez.9216

Sorry about that..

I always thought the first dimension ([x][]) represemted the index, and second ([][x]) represented the sub index similar to a listview or database.

I agree with Suzie999. First index is the number of "rows", and the second is the number of "colums" in the row, although either way will work if programmed correctly, though this way is simpler in that you can do this:

for (int i = 0; i < rows; i++)
{
    for (int j = 0; j < columns; j++)
    {
        sometype colval = array[i][j];
        cout << "row " << dec << i << ", column " << j " == " << array[i][j] << endl;
    }
}

I'm sure someone will point out that I am incorrect in some of this, but it seems to work for me! :-)

You could use something like this:

struct WeekData
{
    int weekNum;
    int values[7][2];

    // ctor...
    WeekData( int weekNum, int new_values[7][2] ) : weekNum( weekNum )
    {
        for( int i = 0; i < 7; ++ i )
        {
            values[i][0] = new_values[i][0];
            values[i][1] = new_values[i][1];
        }
    }

    double get_avg() const
    {
        double sum = 0;
        for( int i =0; i < 7; ++ i ) sum += values[i][0] + values[i][1];
        return sum/14;
    }
    int get_max() const
    {
        int max = values[0][1];
        for( int i = 1; i < 7; ++ i )
             if( values[i][1] > max ) max = values[i][1];
        return max;
    }
    int get_min() const
    {
        int min = values[0][0];
        for( int i = 1; i < 7; ++ i )
             if( values[i][0] < min ) min = values[i][0];
        return min;
    }

    // show all the info for this week ... //
    friend std::ostream& operator << ( std::ostream& os, const WeekData& wd )
    {
        os << "For week " << wd.weekNum << ":\n";
        for( int i = 0; i < 7; ++ i )
             os << wd.values[i][0] << ":" << wd.values[i][1] << ' ';
        os << "\nmin: " << wd.get_min() << ", max:" << wd.get_max()
           << ", avg: " << wd.get_avg() << '\n';

        return os;
    }
} ;

You could use these to create some random values:

double random() { return double( rand() )/RAND_MAX ; }

int random_in_range( const int RAND_BOT, const int RAND_TOP )
{
    return (RAND_TOP-RAND_BOT+1) * random() + RAND_BOT ;
}

Then you could have a test program like this:

int main()
{
    using std::cout; using std::flush; using std::endl;
    using std::vector;

    vector< WeekData > weeks;


    int week[7][2]; // to hold min/max temp data for a week //

    srand( time(0) ); // different seed for each run for the rand() gen... //

    // get same random data for all the weeks in some year //
    for( int weekNo = 1; weekNo <= 52; ++ weekNo )
    {
        for( int i = 0; i < 7; ++ i )
        {
            week[i][0] = random_in_range( -20+weekNo, weekNo+20 ); // temp can range from 0..99 //
            week[i][1] = random_in_range( weekNo, weekNo+50 );
        }

        weeks.push_back( WeekData(weekNo, week) );
    }


    // showing processed info ... for a 'random' year ...  //
    for( size_t i = 0; i < weeks.size(); ++ i )
        cout << weeks[i] << endl;

    cout << "All done year ... " << flush;
}

You would need to also have, at the top:

#include <iostream>
#include <vector>
#include <cstdlib> // re. rand()
#include <ctime>

Edited 1 Year Ago by David W

This article has been dead for over six months. Start a new discussion instead.