3

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;
}
1
  • Anybody have any ideas? Commented Dec 10, 2015 at 22:26

3 Answers 3

1

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;

Sign up to request clarification or add additional context in comments.

4 Comments

So I have made some edits to the above code, but I am getting the error "no operator ">" matches these operands Operand types are AirportData > int" Any idea how to fix that?
You need to look at the value you're interested in in the AirportData. if(array[i].planesLanded > highest)
So I have made the necessary adjustments to my getHighest() function. That works. In my getLowest() function, the code compiles fine, but after running through the debugger, I fail to see why the maxIndex does not increment, and the lowest number is found. This is only the case when the lowest value is not in the first index.
First, you're initialising the lowest value to the second month's (array[1].leastDailyLanded) instead of the first (index 0). Secondly, you're not initialising maxIndex to be that index + 1. In your current code, if the month you picked for your base value also happens to be the lowest, it will finish with maxIndex still equal to 0, instead of the actual lowest month. One fix would be to make the first two lines "int lowest = array[0].leastDailyLanded;" and "int maxIndex = 1;"
0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.