正在加载图片...
C+十语言程序设计 第四章數组 实例分析 【例43】找出一个包含10个元素的数组中最大 cout<<end 的元素。 big=a[o]; 分析:找出一个数组中最大元素这类问题可以利for(=0k10;+) 用扫描法解决。即以数组的第一个元素为基准, 向后比较,如果遇到有比基准元素更大的元素,fa>bg)bga 则将基准元素替换为该元素,直到数组中所有的 cout<<" the big number is:"<<big<endl 元素均被扫描。这时得到的最新的基准元素就是 数组中最大的元素。 include<iostream.h> 程序的运行结果: Include<iomanip. h> please input the numbers: void maino the numbers are: 15237836455699621130 int al1oli, big; cout<< please input the numbers: In", the big number is: 99 for(=0;<10;i++) cins>a0 cout<< the numbers are: for(i=0;<10;|++) cout≤etw4)≤≤aC++语言程序设计 第四章 数组 实例分析 【例4.3】 找出一个包含10个元素的数组中最大 的元素。 分析:找出一个数组中最大元素这类问题可以利 用扫描法解决。即以数组的第一个元素为基准, 向后比较,如果遇到有比基准元素更大的元素, 则将基准元素替换为该元素,直到数组中所有的 元素均被扫描。这时得到的最新的基准元素就是 数组中最大的元素。 #include<iostream.h> #include<iomanip.h> void main() { int a[10],i,big; cout<<"please input the numbers:\n"; for(i=0;i<10;i++) cin>>a[i]; cout<<"the numbers are: "; for(i=0;i<10;i++) cout<<setw(4)<<a[i]; cout<<endl; big=a[0]; for(i=0;i<10;i++) if(a[i]>big) big=a[i]; cout<<"the big number is : "<<big<<endl; } 程序的运行结果: please input the numbers: the numbers are: 15 23 78 36 45 56 99 62 11 30 the big number is :99
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有