Reputation: 83
So I'm trying to figure out how to get the min/max value of an array, and get the corresponding index value. I also am not sure how to call that function in my main function. My trouble is in the final part of the problem statement. I put it in block quotes. I started to make the getHighest function, but I don't know where to go or if I am even doing it right. Any help appreciated.
//Write a program that uses a structure to store the following information, for
//a particular month, at the local airport:
// Total number of planes that landed
// Total number of planes that departed
// Greatest number of planes that landed in a given day that month
// Least number of planes that landed in a given day that month
//The program should have an array of twelve structures to hold travel information
//for the entire year. The program should prompt the user to enter data for each
//month. Once all data is entered, the program should calculate and output the average
//monthly number of landing planes, the average monthly number of departing planes,
//the total number of landing and departing planes for the year, and
the greatest and least number of planes that landed on any one day (and which month it occurred in).
#include <iostream>
#include <iomanip>
using namespace std;
const int NUM_MONTHS = 2;
struct AirportData
{
int planesLanded;
int planesDeparted;
int mostPlanesLanded;
int leastPlanesLanded;
};
// Function Prototypes
void getHighest(int AirportData array[], int size);
void getLowest();
double getLandingAverage(int landedSum, int NUM_MONTHS);
double getDepartedAverage(int departedSum, int NUM_MONTHS);
int main()
{
int landedSum = 0;
int departedSum = 0;
int totalPlanes = 0;
double average = 0.0;
AirportData travelInformation[NUM_MONTHS];
// Get user input
for(int i = 0; i < NUM_MONTHS; i++)
{
cout << "How many planes landed in month " << i + 1 << " ? ";
cin >> travelInformation[i].planesLanded;
cout << "How many planes departed in month " << i + 1 << " ? ";
cin >> travelInformation[i].planesDeparted;
cout << "What is the greatest number of planes that landed "
<< "on a given day in month " << i + 1 << " ? ";
cin >> travelInformation[i].mostPlanesLanded;
cout << "What is the least number of planes that landed "
<< "on a given dey in month " << i + 1 << " ? ";
cin >> travelInformation[i].leastPlanesLanded;
cout << endl;
}
// Calculate the Sum
for(int i = 0; i < NUM_MONTHS; i++)
{
landedSum = landedSum + travelInformation[i].planesLanded;
departedSum = departedSum + travelInformation[i].planesDeparted;
}
// Calculate the total amount of planes landed and departed YTD
totalPlanes = landedSum + departedSum;
// Output the results
cout << endl;
cout << "The average number of monthly landing planes is: "
<< getLandingAverage(landedSum, NUM_MONTHS) << endl;
cout << "The average number of monthly departed planes is: "
<< getDepartedAverage(departedSum, NUM_MONTHS) << endl;
cout << "Landing and Departing Planes this Year: " << totalPlanes << endl;
return 0;
}
// getHighest() function - Get's the most planes landed on a given day and outputs
void getHighest(AirportData array[], int size)
{
int highest = 0;
int maxIndex = 0;
for(int i = 0; i < NUM_MONTHS; i++)
{
if(array[i].mostDailyLanded > highest)
{
highest = array[i].mostDailyLanded;
maxIndex = i + 1;
}
}
cout << "The greatest number of planes that landed on a day was " << highest
<< " in Month " << maxIndex << endl;
}
// getLowest() function - Get's the least planes landed on a given day and outputs
void getLowest(AirportData array[], int size)
{
int lowest = array[1].leastDailyLanded;
int maxIndex = 0;
for(int i = 0; i < NUM_MONTHS; i++)
{
if(array[i].leastDailyLanded < lowest)
{
lowest = array[i].leastDailyLanded;
maxIndex = i + 1;
}
}
cout << "The least number of planes that landed on a day was " << lowest
<< " in Month " << maxIndex << endl;
}
// getLandingAverage() function - Get's the average monthly planes landed
double getLandingAverage(int landedSum, int NUM_MONTHS)
{
return landedSum/NUM_MONTHS;
}
// getDepartedAverage() function - Get's the average monthly planes departed
double getDepartedAverage(int departedSum, int NUM_MONTHS)
{
return departedSum/NUM_MONTHS;
}
Upvotes: 3
Views: 1974
Reputation: 148890
IMHO, it is bad practice to process output in a lower level function.
I would use following signature:
int getHighest(int AirportData array[], int size, int *month);
Code could become:
int getHighest(AirportData array[], int size, int *month)
{
int highest = 0;
for(int i = 0; i < NUM_MONTHS; i++)
{
if(array[i].mostDailyLanded > highest)
{
highest = array[i].mostDailyLanded;
*month = i + 1;
}
}
return highest
}
That way, you call it from main, and only do the outputs in main. If you later change you program to a GUI one, the computing routines will not have to change.
Upvotes: 0
Reputation: 157
You'll have to create an array to pass in first, copying each month's value into it, so in the context of your program, it's probably be easier to make the function signature
void getHighest(AirportData array[], int size)
Other than that, the way you're going is a simple way to work out what you need. You're starting with the lowest value, iterating over all the elements, and if you find a higher value, you record what it is, and which month it was found in. The only mistake is you assign maxIndex = 1;
when it should be maxIndex = i + 1;
Upvotes: 1
Reputation: 5204
I think you can solve it by adding two arrays, landed and departed.
int landed[NUM_MONTS]
and then in the for loop, you can add
landed[i] = travelInformation[i].planesLanded;
Then you can call the function getHighest(landed, NUM_MONTHS)
to produce the output you want.
And change the maxIndex
assignment in the getHighest function, to this
maxIndex = i + 1;
Or maybe I am mistaken, your question isn't very clear.
Upvotes: 0