阵列中最大/最小?

时间:2018-12-03 00:37:43

标签: c++

目前停留在一个家庭作业问题上,并希望获得一些提示,以了解我的代码为什么不起作用/我缺少什么。作业问题要求您制作一个数组,其中包含一串商店的莎莎调味料,从用户输入中收集有关已售出多少(罐)的数据,然后显示每种已售出调味料的总销售额。我已经做好了一切,但问题仍在继续,要求您找回最畅销和最差的卖家。我已经尝试过,可以使我的代码具有两种风格,但是它们是完全不正确的。任何帮助表示赞赏!这是我的代码:

    #include <iostream>
    #include <string>
    #include <iomanip>

    using namespace std;

    int main()

    {
        // variables
        const int SIZE = 5;
        string salsas[SIZE] = { "Mild", "Medium", "Sweet", "Hot", "Zesty"}; 
        //names of salsas
        int jars[SIZE]; //holds number of jars user enters
        int sales[SIZE]; //holds sales numbers
        int count; //counts for a loop
        int largest; //largest sale
        int smallest; //smallest sale


        cout << "Enter the monthly number of jars sold for each type of 
        salsa.\n\n";

        // Display the salsas in the array
        for (int count = 0; count < SIZE; count++) 
        {
            cout << salsas[count] << ": ";
            cin >> jars[count];
        }

        // Display salsa sales
        cout << "\nEach jar of salsa, no matter the type, costs $5.50.\n";
        cout << "Here are the monthly sales numbers for each type of salsa. 
        \n\n";
        cout << fixed << showpoint << setprecision(2);
        for (int count = 0; count < SIZE; count++)
        {
            double sales = jars[count] * 5.50;
            cout << salsas[count] << ": $";
            cout << sales << endl;  

        }

        //Gets highest sale
        {

        int count;
        int largest = sales[0];
        int index = 0;

        for (count = 0; count < SIZE; count++)
        {
            if (sales[count] > largest)
            {
                largest = sales[count];
                index = count;
            }
        }

        cout << "\nBest Seller: " << salsas[index] << endl;
        }

            //Gets lowest sale
        {

        int count;
        int smallest = sales[0];
        int index = 0;

        for (count = 0; count < SIZE; count++)
        {
            if (sales[count] < smallest)
            {
                smallest = sales[count];
                index = count;
            }
        }

        cout << "\nWorst Seller: " << salsas[index] << endl;
    }
    return 0;
    }

1 个答案:

答案 0 :(得分:0)

调试自己的代码的最大障碍之一就是命名变量。如果您在count函数的开始处声明了largestsmallestmain()变量,但是稍后在一个单独的块中重新声明了它们。这是完全多余的。

实际的错误是您没有真正计算和填充sales[]数组。稍后将它们与largestsmallest进行比较将无法按预期进行。

在下面的代码中查找此行

sales[count] = jars[count] * 5.50;

Lé代码

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
    // variables
    const int SIZE = 5;
    string salsas[SIZE] = { "Mild", "Medium", "Sweet", "Hot", "Zesty"}; 
    //names of salsas
    int jars[SIZE]; //holds number of jars user enters
    int sales[SIZE]; //holds sales numbers

    // int count; //counts for a loop   //  no need
    // int largest; //largest sale      //  no need
    // int smallest; //smallest sale    //  no need

    cout << "Enter the monthly number of jars sold for each type of salsa.\n\n";

    // Display the salsas in the array
    for (int count = 0; count < SIZE; count++) 
    {
        cout << salsas[count] << ": ";
        cin >> jars[count];
    }

    // Display salsa sales
    cout << "\nEach jar of salsa, no matter the type, costs $5.50.\n";
    cout << "Here are the monthly sales numbers for each type of salsa. \n\n";
    cout << fixed << showpoint << setprecision(2);
    for (int count = 0; count < SIZE; count++)  //  declare `count` where it is used
    {
        sales[count] = jars[count] * 5.50;      //  calculate sales[count]   IMPORTANT
        cout << salsas[count] << ": $";
        cout << sales[count] << endl;           //  show appropriate output

    }

    //Gets highest sale
    int largest = sales[0];     //  declare largest and index closest to where they are used
    int index = 0;

    for (int count = 0; count < SIZE; count++)
    {
        if (sales[count] > largest)
        {
            largest = sales[count];
            index = count;
        }
    }

    cout << "\nBest Seller: " << salsas[index] << endl;

    //Gets lowest sale
    int smallest = sales[0];    //  declare smallest closest to where it is used
    index = 0;                  //  reuse your old variable

    for (int count = 0; count < SIZE; count++)
    {
        if (sales[count] < smallest)
        {
            smallest = sales[count];
            index = count;
        }
    }

    cout << "\nWorst Seller: " << salsas[index] << endl;
    return 0;
}

示例控制台交互

Enter the monthly number of jars sold for each type of salsa.

Mild: 20
Medium: 10
Sweet: 5
Hot: 2
Zesty: 1

Each jar of salsa, no matter the type, costs $5.50.
Here are the monthly sales numbers for each type of salsa. 

Mild: $110
Medium: $55
Sweet: $27
Hot: $11
Zesty: $5

Best Seller: Mild

Worst Seller: Zesty