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
659 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
  • 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 50 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

816 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now