在二维数组中找到一个数组的最小和最大

时间:2018-12-18 03:41:38

标签: c++ arrays

所以,这是学校的一项任务,我一直在努力弄清楚如何做工作...

我要使用两个不同的数组:一个带有字符串,另一个带有整数。

在获得用户输入的每种莎莎酱的销售量后,我需要:

  1. 创建一个格式正确的表格,显示每张莎莎酱的销售量

  2. 显示所有已售莎莎的总金额

  3. 显示出售出的莎莎酱数量最多,而销售额最低。

它编译得很好,但它总是对出售的最小和最大萨尔萨斯犬吐出奇怪的答案。

我不太确定如何进行这项工作...解决此问题的正确方法是什么?

谢谢您的任何预先输入!

Name                            -    Name
Advised x                       -    Yes
Logged x                        -    Yes

-----------------------------------------------------------------

Describe Issue
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Summary                         -    Summary
Started occurring               -    2018-12-17
Things affected                 -    1

Error message                   -    Errors

-----------------------------------------------------------------

System Information
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
System Audit                    -    Yes
Server                          -    Live
Version                         -    1.23.12.312
Win OS Version                  -    Windows 8
System Architecture             -    64bit
MSO version                     -    MSO 365
Antivirus Installed             -    AV here

------------------------------------------------------------------

What actions been taken:
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
steps taken

Assistance?                     -    Yes | Initals 


------------------------------------------------------------------

Resolution                      -    Closing

输出看起来像这样:

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;


int main()
{
string salsa[5] = {"mild", "medium", "sweet", "hot", "zesty"}; //x-values
int jars[5] = {0}; //y-values

        cout << "\nHow many jars of mild salsa were sold?: ";
            cin >> jars[0];
        while (jars[0] < 0)
        {
            cout << "\nHow many jars of mild salsa were sold?: ";
            cin >> jars[0];
        }

        cout << "\nHow many jars of medium salsa were sold?: ";
            cin >> jars[1];
        while (jars[1] < 0)
        {
            cout << "\nHow many jars of medium salsa were sold?: ";
            cin >> jars[1];
        }

        cout << "\nHow many jars of sweet salsa were sold?: ";
            cin >> jars[2];
        while (jars[2] < 0)
        {
            cout << "\nHow many jars of sweet salsa were sold?: ";
            cin >> jars[2];
        }

        cout << "\nHow many jars of hot salsa were sold?: ";
            cin >> jars[3];
        while (jars[3] < 0)
        {
            cout << "\nHow many jars of hot salsa were sold?: ";
            cin >> jars[3];
        }

        cout << "\nHow many jars of zesty salsa were sold?: ";
            cin >> jars[4];
        while (jars[4] < 0)
        {
            cout << "\nHow many jars of zesty salsa were sold?: ";
            cin >> jars[4];
        }

        cout << left;
        cout << "\n" <<
        setw(7) << "[Spice] " << setw(6) << "       [Jar]" << '\n' <<
        setw(7) << salsa[0]   << setw(10) << " " << jars[0] << '\n' <<
        setw(7) << salsa[1]  << setw(10) << " " << jars[1] << '\n' <<
        setw(7) << salsa[2]   << setw(10) << " " << jars[2] << '\n' <<
        setw(7) << salsa[3]  << setw(10) << " " << jars[3] << '\n' <<
        setw(7) << salsa[4]   << setw(10) << " " << jars[4] << '\n' << "\n";

cout << "Total sales: " << jars[0] + jars[1] + jars[2] + jars[3] + jars[4] << "\n\n";


//------------------------------------------------


    int highest;
    highest = jars[0];
    for (int count = 0; count < 5; count++)
    {
        if (jars[count] > highest)
            highest = count;
    }

    int lowest;
    lowest = jars[0];
    for (int count = 0; count < 5; count++)
    {
        if (jars[count] < lowest)
            lowest = count;
    }

//------------------------------------------------

    cout << salsa[highest] << " was sold the most." << "\n";
    cout << salsa[lowest] << " was sold the least." << "\n\n";

return 0;
}

应该说“轻度”的销量最少,而不是“甜”。

2 个答案:

答案 0 :(得分:0)

您的逻辑有问题。你有这个:

    int lowest;
    lowest = jars[0];
    for (int count = 0; count < 5; count++)
    {
        if (jars[count] < lowest)
            lowest = count;
    }

我建议您使用此功能(可以根据需要单独保存索引和值。这类似于指针的想法):

    int lowest_value = jars[0];
    int lowest_index = 0;
    for (int count = 0; count < 5; count++)
    {
        if (jars[count] < lowest_value) {
            lowest_value = jars[count];
            lowest_index = count;
        }
    }

然后,如果要检索最小值,则可以使用“ lowest_value”变量,如果要检索索引,则可以使用“ lowest_index”变量。

此外,我建议您使用名称“ index”或“ idx”或“ i”代替计数,因为您没有计数。

希望它会有所帮助:)

答案 1 :(得分:0)

您的代码中只有一个小错误。如下更改您的代码。我想我不需要对代码做更多的解释,因为它非常简单!

int highest;
highest = 0;
for (int count = 1; count < 5; count++)
{
    if (jars[count] > jar[highest])
        highest = count;
}

int lowest;
lowest = 0;
for (int count = 1; count < 5; count++)
{
    if (jars[count] < jars[lowest])
        lowest = count;
}

注意:您只需要从索引1遍历数组。检查索引0就是一种繁荣。