簡體   English   中英

查找數組C ++中最小值/最大值的索引

[英]Finding the Index of the Min/Max Value in an Array C++

所以我試圖找出如何獲得數組的最小/最大值,並獲得相應的索引值。 我也不確定如何在我的main函數中調用該函數。 我的麻煩在於問題陳述的最后部分。 我把它放在塊引號中。 我開始制作getHighest函數,但我不知道去哪里或者我是否做得對。 任何幫助贊賞。

//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 

在任何一天(以及它發生在哪個月)降落的最大和最少數量的飛機。

#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;
}

您必須先創建一個數組,然后將每個月的值復制到其中,因此在程序的上下文中,可能更容易進行函數簽名

void getHighest(AirportData array[], int size)

除此之外,你的方式是一種簡單的方法來找出你需要的東西。 你從最低值開始,迭代所有元素,如果你找到一個更高的值,你記錄它是什么,以及它找到了哪個月。唯一的錯誤是你指定maxIndex = 1; 當它應該是maxIndex = i + 1;

我想你可以通過添加兩個陣列來解決它,着陸和離開。

int landed[NUM_MONTS]

然后在for循環中,您可以添加

landed[i] = travelInformation[i].planesLanded;

然后你可以調用函數getHighest(landed, NUM_MONTHS)來產生你想要的輸出。

並將getHighest函數中的maxIndex賦值更改為this

maxIndex = i + 1;

或許我錯了,你的問題不是很清楚。

恕我直言,在較低級別的功能中處理輸出是不好的做法。

我會使用以下簽名:

int getHighest(int AirportData array[], int size, int *month);

代碼可能變成:

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
}

這樣,你從main調用它,只在main中輸出。 如果您稍后將程序更改為GUI程序,則計算例程將不必更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM