c++ - 查找数组 C++ 中最小值/最大值的索引

标签 c++ arrays

所以我想弄清楚如何获取数组的最小/最大值,并获取相应的索引值。我也不确定如何在我的主要功能中调用该功能。我的问题出在问题陈述的最后部分。我把它放在 block 引号中。我开始制作 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 

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

最佳答案

您必须首先创建一个数组来传递,将每个月的值复制到其中,因此在您的程序上下文中,制作函数签名可能更容易

void getHighest(AirportData array[], int size)

除此之外,您要采用的方法是计算所需内容的简单方法。您从最低值开始,遍历所有元素,如果找到更高的值,则记录它是什么以及找到它的月份。唯一的错误是您分配 maxIndex = 1; 当它应该是 maxIndex = i + 1;

关于c++ - 查找数组 C++ 中最小值/最大值的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34192137/

相关文章:

c++ - 调用具有不同数量参数的函数

c++ - 使用 wmemset() 的代码可移植性如何?

C++ 循环不断评估 if 语句,使其速度太慢

javascript - 查找输入值并将其与 JSON 数据进行比较

c++ - 两个类之间的交叉访问

c++ - 如何在 win32 GUI 应用程序中向用户显示快速更新的数据?

c - 指针符号

c - 我在数组中的一个值是 "0"

c++ - 大整数乘法

c++ - 如何写短这个 "or"语句是不是?