Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Two-Dimensional Array that stores integers. How can I show the index of the highest temp and lowest temp?

Posted on 2008-10-29
5
Medium Priority
?
686 Views
Last Modified: 2010-04-21
Im trying to complete an assignment and am stuck on two parts.

With the code listed below, I need to add two additional functions named indexHighTemp and indexLowTemp.

indexLowTemp: This function needs to return the index of the highest high temperature in the array.
indexHighTemp: This function needs to return the index of the lowest low temperature in the array.

I'd like to add these functions to the code listed below, with minimal intrusions.
Thanks for your input!
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
 
const int rows =2;
const int columns =12;
string month[] = {"January", "Feburary", "March", 
				  "April", "May", "June", "July", 
				  "August", "September", "October", 
				  "November", "December"};
 
 
void getData();
double averageHigh(int matrix[rows][columns]);
double averageLow(int matrix[rows][columns]);
 
     int row;
     int col;
     int matrix[rows][columns];
	 int counter = 0;
	 int maxIndex = 0;
 
int main()
{
    
 
    
    
    double highaverage;
    double lowaverage;
 
    for(row = 0; row<rows; row++)
         for(col =0; col < columns; col++)
              matrix[row][col] = 0;
 
    getData();
    highaverage = averageHigh(matrix);
 
    cout<<"The average of the high temperatures is "<<highaverage<<endl;
 
    lowaverage = averageLow(matrix);
 
    cout<<"The average of the low temperatures is "<<lowaverage<<endl;
 
 
system("pause");
return 0;
 
}
 
void getData()
{
	cout<<"Please Enter the Highest Temperature for each month: "<<endl;
    row = 0;
 
    for(col = 0; col<columns; col++)
	{
		cout << month[col] << ": ";
		cin>>matrix[row][col];
	}
 
	cout<<"Please Enter the Lowest Temperature for each month: "<<endl;
 
    row = 1;
    for(col = 0; col<columns; col++)
	{ 
		cout << month[col] << ": ";
        cin>>matrix[row][col];
	}
}
double averageHigh(int matrix[rows][columns])
{
    double average=0;
    int sum = 0;
    
    row = 0;
    for(col =0; col<columns; col++)
         sum = sum + matrix[row][col];
 
    average = sum / 12;
 
    return average;
}
double averageLow(int matrix[rows][columns])
{
    double average=0;
    int sum =0;
    row =1;
 
    for(col =0; col<columns; col++)
         sum = sum + matrix[row][col];
 
    average = sum / 12;
 
    return average;
}

Open in new window

0
Comment
Question by:tomsterkw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 22837857
I assume your problem is that the array is two dimensional, but a function can only return one thing.

You have two options.  Define a struct with two integers and return a structure, or return a single integer that is equal to the location of the desired item as if it were a one dimensional array.

In other words, for an array of matrix[rows][cols], that is equal to an array of matrix2[rows*cols], and the item at matrix[x][y] would be at matrix2[(x-1)*size of an element + col], (basically, it's possible to specify an element in a multi-dimensional array with a single value).
0
 

Author Comment

by:tomsterkw
ID: 22837884
Thanks HooKoo for the quick response!

I think the first option would work fine, seeing how High temps are assigned to matrix[0][rows] and Low temps are stored in matrix[1][rows]. Could I simply utilize two for loops and send the results back to main? If so, think you could lend me a hand code wise?

I was thinking something like below but I cant seem to get it to work right. I think my logic is a little fubar also.

for (counter = 0; counter < 12; counter++)
{
     if (matrix[0][colums] < max)
          max = matrix[0][colums]
}
0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 22837895
Hopefully the 'struct' idea is self explanatory, but here's an example of treating the data as a single dimenstional array.
The concept is to allow access to the 2D array via a 1D array, then define a function that can return the offset of the desired item in the 1D array.
int indexLowTemp(int matrix[rows][columns])
{
  int LowTemp = matrix[0][0];
  int rc = 0; //Return Code - single digit index to matrix
  int row;
  int col;
  for( row=0; row < rows; row++ )
  {
    for( col=0; col < cols; col++ )
    {
      if( matrix[row][col] < LowTemp )
      {
        LowTemp = matrix[row][col];
        rc = row * rows + col;
      }
    }
  }
  return rc;
}
 
/* To get the value of the lowest temp */
int* pmatrix = &matrix;  //Pointer to matrix[0][0];
int LowTemp = *pmatrix[ indexLowTemp( matrix ) ];
 
/* Bonus, decode the row/col from the return of indexLowTemp */
int index = indexLowTemp( matrix );
int row = index / rows;
int col = index % rows;

Open in new window

0
 
LVL 16

Accepted Solution

by:
HooKooDooKu earned 200 total points
ID: 22837913
struct example

And careful about getting your row/col backwards.  The latest post you were using [row] as the 2nd dimension, but all of your code so far is using [row] for the 1st dimension.
struct rowcol
{
  int row;
  int col;
}; 
 
struct rowcol indexLowTemp(int matrix[rows][columns])
{
  int LowTemp = matrix[0][0];
  struct rc;
  rc.row = 0;
  rc.col = 0;
 
  int row;
  int col;
  for( row=0; row < rows; row++ )
  {
    for( col=0; col < cols; col++ )
    {
      if( matrix[row][col] < LowTemp )
      {
        LowTemp = matrix[row][col];
        rc.row = row;
        rc.col = col;
      }
    }
  }
  return rc;
}
 
/* To get the value of the lowest temp */
struct rowcol rc = indexLowTemp( matrix );
int LowTemp = matrix[ rc.row ][ rc.col ];
 

Open in new window

0
 

Author Closing Comment

by:tomsterkw
ID: 31511533
Thank you very much for your assistance!
I was really close by the time that I noticed this post. Your assistance helped confirm that I was on the right track.  I appricaite the timely reply.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question