C++ Problem(maximum and minimum number)

2006-11-17 6:15 pm
Read ten numbers and print out the maximum and minimum number.
*Please use for loop to read the number.
更新1:

I haven't learn the array in C++. Can the maximum and minimum only use the for loop without array to find out? * #include int main() int i; double n, max, min; for (i=0; i

更新2:

for (i=0; i n; } I only know this part and I don't know how to continue with this to find out the maximum and minimum nubmer......

更新3:

for (i =0; i

更新4:

for (i =0; i<10; i = i+1);

更新5:

{ cout<<"Input a numer \n"; cin>>n; }

回答 (2)

2006-11-17 8:14 pm
✔ 最佳答案
try following code:

#include <iostream.h>
#include <stdio.h>

int main( )
{ int iMax = 0, iMin = 0, iIn = 0, iLoop = 0;

for ( iLoop = 0; iLoop < 10; iLoop++ )
{ printf( "Enter (%d) Number: ", iLoop + 1 );
cin >> iIn;
if ( iLoop <= 0 )
{ iMax = iIn;
iMin = iIn;
}
if ( iIn > iMax )
iMax = iIn;
if ( iIn < iMin )
iMin = iIn;

}

cout << "Max Number : " << iMax << endl;
cout << "Min Number : " << iMin << endl;

return 0;
}

hope can help u

PS: u can change the data type from integer to double or other numeric data type to fulfill ur need.

2006-11-17 6:43 pm
我只寫大約思路, 詳細的內容你應可以自己寫的

大意是先把第一個值放入變量中,
與後面的數值比較, 當遇到比現在大/少的值時,便改用大/少的那個值,
+-+-+-+-+-+


宣告 一個array 或變量, 存入那10 個數
宣告 變量 max1, min1, 用來暫存字值
宣告 變量 i , 用來做loop

max1 = array [0] //先把第一個值 放入 max1
min1 = array [0] //先把第一個值 放入 min1

建立循環 i&lt;=10 // 如果 i 少於 10 便循環
{

i++ ; // i加1

if ( max1 &lt; array [i] ) // max1 和 array[i] 比較
then ( max1 = array[i] )
//如果max1比下一個裡小,便把大的那個放入max1去


if ( min1 &gt; array [i] ) // min1 和 array[i] 比較
then ( min1 = array[i] )
//如果min1比下一個裡大,便把大的那個放入min1去

};

2006-11-17 10:47:55 補充:
讀入那10個值的方法for 循環 k = 1至10{array[k] = getchat ;}大約是這樣, 詳細的忘了


收錄日期: 2021-04-23 13:50:18
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20061117000051KK00755

檢視 Wayback Machine 備份