0

我需要创建一个程序,输入一周中每一天的每日销售额。输入值后,我需要能够显示:

Sales for day 1 are ###

Sales for day 2 are ###

The lowest sales was XXX

The highest sales was XXX

问题是我无法让我的代码输出:

Sales for day 1 are XXX

Sales for day 2 are XXX

我只能说

Sales are:

XXX

XXX

XXX

而且我也不知道如何找到最低和最高的销售额。我们甚至还没有开始使用 MIN 和 MAX 函数,所以我不知道如何完成它。

到目前为止,我的代码是:

const int DAYS_SALES = 7;
double sales[DAYS_SALES];
int sub;
double min = 0;
double max = 0;

for(sub = 0; sub < DAYS_SALES; ++sub)
{
    cout << "Enter in the sales for day " << (sub + 1) << " ";
    cin >> sales[sub];
}
cout << endl << "The sales for day are: " << endl;
for (sub = 0; sub < DAYS_SALES; ++sub)
    cout << sales[sub] << " " << endl;

任何帮助,将不胜感激!

4

2 回答 2

0

当您的 for 循环遍历每个值时,跟踪最小值和最大值。

如果当前值 ( sales[sub]) 小于min到目前为止,则将该值存储为新的最小值。

const int DAYS_SALES = 7;
double sales[DAYS_SALES];
int sub;
double min = 0.0;
double max = 0.0;

for(sub = 0; sub < DAYS_SALES; ++sub)
{
    cout << "Enter in the sales for day " << (sub + 1) << " ";
    cin >> sales[sub];
}

min = sales[0];
max = sales[0];

cout << endl << "The sales for day are: " << endl;
for (sub = 0; sub < DAYS_SALES; ++sub)
{
    cout << endl << "The sales for day are: " << sales[sub] << " " << endl;

    if (sales[sub] < min)
    {   // If we find a smaller min. value, store that in min
        min = sales[sub];
    }

    if (sales[sub] > max)
    {   // If we find a bigger max. value, store that in max
        max = sales[sub];
    }
}

// Print out the Min and Max that we found.
cout<< "The lowest sales was " << min;
cout<< "The highest sales was " << max <<endl;
于 2013-09-25T20:53:34.573 回答
0

好的,也许您应该将值输入到std::vector适当类型的 a 中,然后调用std::minmax(...)它。

于 2013-09-25T21:08:06.607 回答