4_27.cpp
上传用户:zipjojo
上传日期:2009-07-20
资源大小:70k
文件大小:1k
- # include<iostream.h>
- double fun(int a[],int n)
- {
- int i;
- double s=0.0;
- for(i=0;i<n;i++)
- s+=a[i];
- return s/n;
- }
- void main()
- {
- int arrSize; //元素的个数
- int * arr,i;
- cout<<"Please enter the number of array elements:";
- cin>>arrSize; //临时分配元素的个数
- arr=new int[arrSize]; //临时分配这些元素所需的内存空间(堆内存中)
- if(arr!=NULL) //判断,堆空间不够分配时,系统会返回一个空指针值NULL
- {
- cout<<"nPlease enter the array elements:n";
- for (i=0;i<arrSize;i++) //逐个输入数组元素
- cin>>arr[i];
- cout<<endl;
- cout<<"the numbers you inputted are:"<<endl;
- for (i=0;i<arrSize;i++) //显示数组元素
- cout<<arr[i]<<" "; // arr[i] 可替换为*(arr+i)
- cout<<endl;
- double ave;
- ave=fun(arr,arrSize);
- cout<<"Average="<<ave<<endl;
- delete[]arr;//释放堆内存
- }
- else
- cout<<"Can't allocate more memory.n";
- }