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.

## All 6 Replies

int array;

Or

int array;

    #include iostream;

using namespace std;

int main ()
{

// = two columns,  = 7 rows.
int array;

return 0;
}


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;

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

double get_avg() const
{
double sum = 0;
for( int i =0; i < 7; ++ i ) sum += values[i] + values[i];
return sum/14;
}
int get_max() const
{
int max = values;
for( int i = 1; i < 7; ++ i )
if( values[i] > max ) max = values[i];
return max;
}
int get_min() const
{
int min = values;
for( int i = 1; i < 7; ++ i )
if( values[i] < min ) min = values[i];
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] << ":" << wd.values[i] << ' ';
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; // 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] = random_in_range( -20+weekNo, weekNo+20 ); // temp can range from 0..99 //
week[i] = 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>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.