2013-09-25 73 views
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功能,所以我迷失在如何完成它。

我的代碼,我至今是:

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; 

任何幫助,將不勝感激!

回答

0

跟蹤最小值和最大值,作爲循環遍歷每個值。

如果當前值(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; 
0

好吧,也許你應該輸入你的價值觀,以適當類型的std::vector然後調用它std::minmax(...)