2D array

trouble finding average highest and lowest

Page 1 of 1

6 Replies - 3809 Views - Last Post: 13 September 2009 - 06:17 PM Rate Topic: -----

#1 bhuelsman  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 45
  • Joined: 20-August 09

2D array

Posted 13 September 2009 - 02:46 PM

Basically what my program is supposed to do is find, the high, low and average temperature during the twenty-four hour period. Which is all in the theArray[8][24]. And then it needs to find the difference in temperature between the server room and each of the seven servers for each hour of the day. When the temperature difference is greater than or equal to 50 degrees, mark the entry with an asterisk. Basically the the 7th element in the array is the room's temp and the others are the computer temps. What I'm having trouble with is the loops to find the highest and lowest. And then my average isn't working or it might be but I don't think I'm calling the functions correctly cause it will all compile but it just pops an error when I try to run it. Any advice would be appreciated.


#include <iostream>
using namespace std;

const int NMBROFCLMNS = 8;
const int NMBROFROWS = 24;

void processArray(int TR[][NMBROFCLMNS]);
double avgerage(int [][NMBROFCLMNS], int, int);
int high(int TR[][NMBROFCLMNS], int, int);
int low(int [][NMBROFCLMNS], int, int);
int tempDifference(int [][NMBROFCLMNS], int , int );

int main ( ){
	 int tempReadings [][NMBROFCLMNS] = {
		  {96, 94, 95, 100, 115, 97, 92, 65},
		  {100, 92, 91, 110, 115, 99, 98, 65},
		  {120, 91, 99, 113, 115, 95, 99, 66},
		  {97, 99, 100, 114, 115, 100, 100, 66},
		  {99, 100, 111, 115, 115, 104, 104, 68},
		  {95, 130, 112, 101, 115, 100, 115, 65},
		  {94, 90, 95, 100, 105, 97, 120, 64},
		  {96, 94, 95, 100, 115, 97, 92, 65},
		  {100, 92, 91, 110, 115, 99, 98, 65},
		  {120, 91, 99, 113, 115, 95, 99, 66},
		  {97, 99, 100, 114, 115, 100, 100, 66},
		  {99, 100, 111, 115, 115, 104, 104, 68},
		  {95, 130, 112, 101, 115, 100, 115, 65},
		  {94, 90, 95, 100, 105, 97, 120, 64},
		  {96, 94, 95, 100, 115, 97, 92, 65},
		  {100, 92, 91, 110, 115, 99, 98, 65},
		  {120, 91, 99, 113, 115, 95, 99, 66},
		  {97, 99, 100, 114, 115, 100, 100, 66},
		  {99, 100, 111, 115, 115, 104, 104, 68},
		  {95, 130, 112, 101, 115, 100, 115, 65},
		  {94, 90, 95, 100, 105, 97, 120, 64},
		  {99, 100, 111, 115, 115, 104, 104, 68},
		  {92, 93, 94, 95, 96, 97, 98, 64},
		  {96, 94, 95, 100, 115, 97, 92, 64} };

	 processArray(tempReadings);

	 return 0;
} //end main

void processArray(int temperatures[][NMBROFCLMNS]){
															//Create the required tables

																//print the table heading
	 cout << "Low / High / Average Temperature Table \n";
	 cout << "Unit" << "\t\tLow" << "\t\tHigh" << "\t\tAVG\n";
 
	  //Iterate through each clmn of the array and calculate column stats 
	 for (int clmn = 0; clmn < 8; clmn++) {
		  cout << clmn << "\t\t" 
		  << low(temperatures,NMBROFROWS,clmn) << "\t\t"
				   << high(temperatures,NMBROFROWS,clmn) << "\t\t"
		  << avgerage(temperatures,NMBROFROWS,clmn) <<"\n";
	 };

	 //calculate and display the temp difference table
	 tempDifference(temperatures,NMBROFROWS,NMBROFCLMNS);
}
void printColumn(int theArray[][NMBROFCLMNS], int NMBROFROWs, int NMBRCLMNS){
	//prints the values in the given column (TheClmn)
	for (int row = 0; row < NMBROFROWS; row++)
		 cout << theArray[row][NMBRCLMNS] << " ";
		cout <<endl;
}
int low(int theArray[][NMBROFCLMNS], int NMBROFROWS, int NMBROFCLMNS)
{
			int lowest;
		for (int col=0; col<NMBROFCLMNS;col++)			//finds the lowest value in the given column of a two-dimensional array
 
		{										
			 lowest=theArray[0][NMBROFCLMNS];
			for (int row=0; row<NMBROFROWS; row++)
				if (lowest<theArray[row][col])
					lowest=theArray[row][col];
		}
		return lowest;																
} //end low

int high(int theArray[][NMBROFCLMNS], int NMBROFROWS, int NMBROFCLMNS)
{
		int largest;
		for (int col=0; col<NMBROFCLMNS;col++)
		{										//finds the highest value in the given column of a two-dimensional array
			 largest=theArray[0][NMBROFCLMNS];
			for (int row=1; row<NMBROFROWS; row++)
				if (largest<theArray[row][col])
					largest=theArray[row][col];
		}
		return largest;

} //end high

double avgerage(int theArray[][NMBROFCLMNS], int NMBROFROWS, int NMBROFCLMNS)
{			int sum;
			for (int col=0;col<NMBROFCLMNS;col++)																	//finds the average of the values in the given column of a two-dimensional array
			{
				sum=0;
				for(int row=0;row<NMBROFROWS;row++)
					sum=(sum+theArray[row][col])/col;
			}
			return sum;
} //end average

int tempDifference(int theArray[][NMBROFCLMNS], int NMBROFROWS, int NMBROFCLMNS){
																	//creates a table that contains the difference between
			int difference;
			for (int row=0;row<NMBROFROWS;row++)
																//each computer's temperature and the room temperature
			{ 
				for (int col=0; col<NMBROFCLMNS;col++)

				difference = theArray[row][col] - theArray[row][7];	
			}
																			//for each time reading. Mark all differences 
				return difference;															//greater than or equal to 50 with an asterisk.
																			//Formula: differnce = theArray[row][clmn] - theArray[row][7];
																	 
} //end temperature Difference



Is This A Good Question/Topic? 0
  • +

Replies To: 2D array

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7161
  • View blog
  • Posts: 14,926
  • Joined: 16-October 07

Re: 2D array

Posted 13 September 2009 - 03:41 PM

From this code, I can't really tell what you're trying to find the average of:
double avgerage(int theArray[][NMBROFCLMNS], int NMBROFROWS, int NMBROFCLMNS) {
	int sum;
	for (int col=0;col<NMBROFCLMNS;col++) {
		// you set it to zero
		sum=0;
		// this means you'll completely set this to zero for never col loop?
		for(int row=0;row<NMBROFROWS;row++) {
			// the first time you hit this, col will be 0
			// you still can't divide by zero
			sum = (sum+theArray[row][col])/col;
		}
	}
	return sum;
}



If you want the average of all data points, it might look like this:
double avgerage(int theArray[][NMBROFCLMNS], int NMBROFROWS, int NMBROFCLMNS) {
	int sum = 0;
	for (int col=0;col<NMBROFCLMNS;col++) {
		for(int row=0;row<NMBROFROWS;row++) {
			sum += theArray[row][col];
		}
	}
	return sum / (NMBROFCLMNS*NMBROFROWS);
}



Do you need to use a 2D array? They're pretty sloppy. This could be easier.

const int NUMBER_OF_SERVERS = 7;

struct TempInfo {
	int room;
	int server[NUMBER_OF_SERVERS];
};

const int HOURS_IN_DAY = 24;

struct DayTemp {
	TempInfo hour[HOURS_IN_DAY];
};


Was This Post Helpful? 0
  • +
  • -

#3 tinychip  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 03-December 08

Re: 2D array

Posted 13 September 2009 - 03:54 PM

int largest;
for (int col=0; col<NMBROFCLMNS;col++)
{ //finds the highest value in the given column of a two-dimensional array
largest=theArray[0][NMBROFCLMNS];
for (int row=1; row<NMBROFROWS; row++)
if (largest<theArray[row][col]) <----highest should be > sign
largest=theArray[row][col];
}
return largest;
Was This Post Helpful? 0
  • +
  • -

#4 bhuelsman  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 45
  • Joined: 20-August 09

Re: 2D array

Posted 13 September 2009 - 04:09 PM

Yea I have to use a 2D array. Basically I need to find the average of each column.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7161
  • View blog
  • Posts: 14,926
  • Joined: 16-October 07

Re: 2D array

Posted 13 September 2009 - 04:30 PM

View Postbhuelsman, on 13 Sep, 2009 - 05:09 PM, said:

Yea I have to use a 2D array. Basically I need to find the average of each column.


Ok...
double getColumnAvg(int theArray[][NMBROFCLMNS], int NMBROFROWS, int col) {
	int sum = 0;
	for(int row=0;row<NMBROFROWS;row++) {
		sum += theArray[row][col];
	}
	return sum / NMBROFROWS;
}



Right?
Was This Post Helpful? 0
  • +
  • -

#6 bhuelsman  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 45
  • Joined: 20-August 09

Re: 2D array

Posted 13 September 2009 - 05:29 PM

I attached a file that shows a screen shot of what my program is supposed to look like after it runs and what I have it looking like right now. I'm completely lost as to why I got the results I did get. The screen shot on the left is what my program is doing and the one on the right is what its supposed to look like. Its kind of hard to explain what it needs to do so I figured seeing it would help.

Attached image(s)

  • Attached Image

This post has been edited by bhuelsman: 13 September 2009 - 05:36 PM

Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7161
  • View blog
  • Posts: 14,926
  • Joined: 16-October 07

Re: 2D array

Posted 13 September 2009 - 06:17 PM

I think you're making it harder on yourself. Break the problem down into just rows and columns and work on those separately.

Here's some framework code. The average works, the rest you can try.

#include <iostream>
using namespace std;

const int NMBROFCLMNS = 8;
const int NMBROFROWS = 24;


void processLowHighAvg(int temps[][NMBROFCLMNS], int col){
	int low, high, sum = 0;
	
	low = high = sum = temps[0][col];
	
	for (int row = 1; row < NMBROFROWS; row++) {
		// figure out your low
		// figure out your high
		sum +=temps[row][col];
	}
	int avg = sum / NMBROFROWS;
	cout << col << "\t\t" << low << "\t\t" << high << "\t\t" << avg << endl;
	
}


void processTempDiff(int temps[][NMBROFCLMNS], int row){
	cout << row;
	for (int col=0; col<NMBROFCLMNS; col++) {
		 cout << '\t' << temps[row][col];
	}
	cout <<endl;
}

void processArray(int temperatures[][NMBROFCLMNS]){
	cout << "Low / High / Average Temperature Table" << endl;
	cout << "Unit\t\tLow\t\tHigh\t\tAVG" << endl;
 	for (int col=0; col<NMBROFCLMNS; col++) {
		processLowHighAvg(temperatures, col);
	}

	cout << endl << "Temperature Diff Table" << endl;
	cout << "hour\tetc" << endl;
	for (int row=0; row<NMBROFROWS; row++) {
		processTempDiff(temperatures, row);
	}
}


int main ( ){
	 int tempReadings [][NMBROFCLMNS] = {
		  {96, 94, 95, 100, 115, 97, 92, 65},
		  {100, 92, 91, 110, 115, 99, 98, 65},
		  {120, 91, 99, 113, 115, 95, 99, 66},
		  {97, 99, 100, 114, 115, 100, 100, 66},
		  {99, 100, 111, 115, 115, 104, 104, 68},
		  {95, 130, 112, 101, 115, 100, 115, 65},
		  {94, 90, 95, 100, 105, 97, 120, 64},
		  {96, 94, 95, 100, 115, 97, 92, 65},
		  {100, 92, 91, 110, 115, 99, 98, 65},
		  {120, 91, 99, 113, 115, 95, 99, 66},
		  {97, 99, 100, 114, 115, 100, 100, 66},
		  {99, 100, 111, 115, 115, 104, 104, 68},
		  {95, 130, 112, 101, 115, 100, 115, 65},
		  {94, 90, 95, 100, 105, 97, 120, 64},
		  {96, 94, 95, 100, 115, 97, 92, 65},
		  {100, 92, 91, 110, 115, 99, 98, 65},
		  {120, 91, 99, 113, 115, 95, 99, 66},
		  {97, 99, 100, 114, 115, 100, 100, 66},
		  {99, 100, 111, 115, 115, 104, 104, 68},
		  {95, 130, 112, 101, 115, 100, 115, 65},
		  {94, 90, 95, 100, 105, 97, 120, 64},
		  {99, 100, 111, 115, 115, 104, 104, 68},
		  {92, 93, 94, 95, 96, 97, 98, 64},
		  {96, 94, 95, 100, 115, 97, 92, 64} };

	 processArray(tempReadings);

	 return 0;
}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1