如有失误之处,还恳请指教!!!
// arrfun1.cpp -- functions with an array argument
#include <iostream>
//定义一个int类型的变量Arsize,并且将其用const限定符限定表明其始终不可修改,因此在属性上类似为常量
const int ArSize = 8;
//函数原型,声明了一个返回值为int类型,具有两个参数,并且其中一个参数是数组头元素指针的函数原型
int sum_arr(int arr[], int n); // prototype
int main()
{
using namespace std;
//定义了一个函数8个元素,并且各元素类型为int类型的数组(数组需要在声明时就为其赋值,因此声明与赋值同时进行)
int cookies[ArSize] = {1, 2, 4, 8, 16, 32, 64, 128};
// some systems require preceding int with static to
// enable array initialization
//调用函数sum_arr(),并将其返回值赋值给int类型的变量sum
int sum = sum_arr(cookies, ArSize);
cout << "Total cookies eaten: " << sum << "\n";
// cin.get();
return 0;
}
// return the sum of an integer array
//函数定义,此函数实现的功能是将数组中的各元素相加,并将和作为其返回值
int sum_arr(int arr[], int n)
{
int total = 0;
for (int i = 0; i < n; i++)
//数组中的元素通过下标访问
total = total + arr[i];
return total;
}