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
646 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

728 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

10 Experts available now in Live!

Get 1:1 Help Now