我需要显示最高和最低销售额

时间:2013-09-25 20:12:46

标签: c++ visual-c++-2010

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

Sales for day 1 are ###

Sales for day 2 are ###

The lowest sales was XXX

The highest sales was XXX

问题是我无法将代码转到cout:

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;

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

好的,也许您应该将值输入相应类型的std::vector,然后在其上调用std::minmax(...)