Unit 6 - Arrays - Elementary Statistics return to unit course outline

Two-dimensional arrays. An example:

Read the explanations provided for the example.

Place your mouse in the areas below to see comments about the corresponding sections of code  
Code
 
 /*
  Example on two dimensional arrays
____________________________________
  Jacob Y. Kazakia  jyk0
  June, 2001
  Recitation Instructor: J.Y.Kazakia
  Recitation Section 01
____________________________________
 
Purpose: This program illustrates the use of a two
dimensional array to store data relating to the
temperatures of rooms in a hotel.
 
Algoritm: The room temperatures (in oF) at a given time are
stored in a data file named thermo.txt. There are 20
floors of rooms and there are 50 rooms at each floor.
The file contains a list of temperatures in all rooms
of the first floor, then the temperatures of all rooms
of the second floor etc.
 
The program reads this file and stores the temperatures
in an array temp[20][50]. Finds the average temperature
in the entire hotel and then prints to the screen
the rooms ( floor number and room number) where the
temperature exceeds the 150% of the average.
 
*/
#include <fstream.h>
 
void main()
{
 
  int floor;  // index of floor varying between 0 and 19
  int room;   // room index at each floor varying between 0   
              // and 49
  double temp[20][50]; // the 2D array for storing
                      // temperatures in oF
 
  double average; // the average temperature in all hotel
                  // rooms
  double sum = 0.0;
 
  ifstream data("thermo.txt", ios:: in);    
 
    for(floor = 0; floor <= 19; floor++)
  {
         for (room = 0; room <= 49; room++)
         {
                data >> temp[floor][room];
                sum = sum + temp[floor][room];
         }
  }
 
 average = sum / ( 20*50);
 
 
 for(floor = 0; floor <= 19; floor++)
  {
   for (room = 0; room <= 49; room++)
   {
     if(temp[floor][room] >= 1.5*average)
     {
      cout <<"\n\n Room " <<room<<"  at floor "<<floor;
      cout <<" is too hot";
     }
   }
  }
 
 cout << "\n\n enter a character and hit enter to exit  ";
char hold;
cin >> hold;
 
 }
 
   /*
    THE OUTPUT :
 
 
 
 Room 21  at floor 7 is too hot
 
 Room 15  at floor 11 is too hot
 
 Room 12  at floor 13 is too hot
 
 enter a character and hit enter to exit
 
 
*/

(text file of the above code - 6ex5)

(printable file)

© 2001 J.Y. Kazakia. All rights reserved