找出一個整型數組中的元素的最大值。
1 /*找出一個整型數組中的元素的最大值。*/ 2 3 #include <iostream> 4 using namespace std; 5 6 class ArrayMax //創建一個類 7 { 8 public : 9 void set_value(); 10 void max_value(); 11 void show_value(); 12 13 private: 14 int array[10]; 15 int max; 16 }; 17 18 void ArrayMax::set_value() //成員函數,向數組元素輸入值 19 { cout<<"開請輸入10個數組元素值"<<endl; 20 int i; 21 for(i=0;i<10;i++) 22 cin>>array[i]; 23 } 24 void ArrayMax::max_value() //成員函數,查找最大值 25 { 26 cout<<"開始查找中,請稍后......"<<endl; 27 int i; 28 max=array[0]; 29 for(i=0;i<10;i++) 30 { 31 if(array[i]>max) 32 max=array[i]; 33 } 34 cout<<"查找完畢!"<<endl; 35 } 36 37 void ArrayMax::show_value() //成員函數,顯示最大值 38 { 39 cout<<"您輸入的數組最大值max="<<max; 40 } 41 42 int main() 43 { 44 ArrayMax arraymax1;//定義對象arraymax1 45 arraymax1.set_value();//調用輸入函數 46 arraymax1.max_value();//調用找最大值函數 47 arraymax1.show_value();//調用輸出最大值函數 48 return 0; 49 }
輸出結果
End!
歡迎大家一起交流 ,分享程序員勵志故事。 幸福的程序員 QQ群:![]()