第一个程序: 演示数组函数是如何运作的。
#include "stdafx.h"
#include <iostream>
using namespace std;
const int ArSize=8;

 /**//* arr实际上并不是数组,而是一个指针。在编写函数的
* 其余部分时,可以将arr看作是数组。C++将数组名解释
* 为其第一个元素的地址。
* int sum_arr(int * arr,int n) 这个函数头也是正确的。
* int * arr和int arr[]含义是相同,都意味着arr是一个int指针
* 但不能在函数体中使用int tip[]来声明指针
*/
int sum_arr(int arr[],int n); //prototype
int main(int argc, char* argv[])
  {
 int cookies[ArSize]= {1,2,4,8,16,32,64,128};

int sum=sum_arr(cookies,ArSize);
cout<<"Total cookies eaten: "<<sum<<"\n";
return 0;
}

int sum_arr(int arr[], int n)
  {
int total=0;
for(int i=0;i<n;i++)
total=total+arr[i];
return total;
}
第二个程序: cookies和arr的值相同。并且还演示了指针概念如何使sum_arr函数比以前更通用。该程序是用限定符std::而不是编译指令using来提供对cout和endl的访问权。
#include "stdafx.h"
#include <iostream>
const int ArSize=8;
int sum_arr(int arr[],int n);
//use std::instead of using directive
int main(int argc, char* argv[])
  {
 int cookies[ArSize]= {1,2,4,8,16,32,64,128};

std::cout<<cookies<<" = array address, ";
// some system require a type cast: unsigned (cookies)
std::cout<<sizeof cookies<<" = sizeof cookies\n";
int sum=sum_arr(cookies,ArSize);
std::cout<<"Total cookies eaten: "<<sum<<std::endl;
sum=sum_arr(cookies,3); //a lie
std::cout<<"First three eaters ate "<<sum<<" cookies.\n";
sum=sum_arr(cookies+4,4);//another lie
std::cout<<"Last four eaters ate "<<sum<<" cookies.\n";
return 0;
}

int sum_arr(int arr[], int n)
  {
int total=0;
std::cout<<arr<<" = arr, ";
// some systems require a type cast: unsigned (arr)

std::cout<<sizeof arr<<" = sizeof arr\n";
for(int i=0;i<n;i++)
total=total+arr[i];
return total;
}
// 注意,地址值和数组的长度将随系统而异,另外,有些C++实现将以十进制而不是十六进制格式显示地址。
 /**//* 为将数组类型和元素数量告诉数组处理函数,请通过两个不同的参数来传递它们:
* void fillArray(int arr[],int size);//prototype
* 而不要试图使用方括号表示法来传递数组长度
* void fillArray(int arr[size]);//No--bad prototype
*/
第三个程序:
#include "stdafx.h"
#include <iostream>
using namespace std;
const int Max=5;

// function prototypes
int fill_array(double ar[], int limit);
void show_array(const double ar[], int n);// don't change data
void revalue(double r, double ar[], int n);
int main(int argc, char* argv[])
  {
double properties[Max];

int size=fill_array(properties,Max);
show_array(properties,size);
cout<<"Enter revaluation factor: ";
double factor;
cin>>factor;
revalue(factor,properties,size);
show_array(properties,size);
cout<<"Done.\n";
return 0;
}

int fill_array(double ar[],int limit)
  {
double temp;
int i;
 /**//* 可以使用循环连续地将值读入到数组中,但如何提早结束循环呢?一种方法使,使用一个特殊值来指出输入结束。
* 由于所有的属性都不为负,因此可以使用复述来指出输入结束。另外,该函数应对错误输入做出反应,如停止输入等。
*/
for(i=0;i<limit;i++)
 {
cout<<"Enter value #"<<(i+1)<<":";
cin>>temp;
if(!cin) //bad input
 {
cin.clear();
while(cin.get()!='\n')
continue;
cout<<"Bad input: input process terminated.\n";
break;
}
else if(temp<0) //signale to terminate
break;
ar[i] =temp;
}
return i;
}

// the following function can use, but not alter,
// the array whose address is ar
 /**//* 要确保显示函数不修改原始数组。除非函数的目的就是修改传递给它的数据,
* 否则应避免发生这种情况。使用普通参数时,这种保护将自动实现,这是由
* 于C++按值传递给它的书籍,而且函数使用数据的拷贝。不过,接受数组名的
* 函数将使用原始数据,这正是fill_array()函数能够完成其工作的原因。
*/
void show_array(const double ar[], int n)
  {
 /**//* 注意,const并不是意味着原始数组必须是常量,而只是意味着不能在
* show_array()函数中使用ar来修改这些数据。因此,show_array()将数组
* 视为只读数据。C++将声明const double ar []解释为const double *ar,
* 实际上是说,ar指向的是一个常量值。
*/
for(int i=0;i<n;i++)
 {
cout<<"Property #"<<(i+1)<<": $";
cout<<ar[i]<<endl;
}
}

// multiplies each element of ar[] by r
void revalue(double r,double ar[], int n)
  {
for(int i=0;i<n;i++)
ar[i]*=r;
}
第四个程序:
#include "stdafx.h"
#include <iostream>
using namespace std;
const int ArSize=8;
int sum_arr(const int *begin,const int *end);
int main(int argc, char* argv[])
  {
 int cookies[ArSize]= {1,2,4,8,16,32,64,128};
int sum=sum_arr(cookies,cookies+ArSize);

cout<<"Total cookies eaten: "<<sum<<std::endl;
sum=sum_arr(cookies,cookies+3); //first 3 elements
cout<<"First three eaters ate "<<sum<<" cookies.\n";
sum=sum_arr(cookies+4,cookies+8);//last 4 elements
cout<<"Last four eaters ate "<<sum<<" cookies.\n";
return 0;
}

int sum_arr(const int *begin,const int * end)
  {
//注意,根据指针减法规则,表达式end-begin是一个整数值,等于数组的元素数目。
const int * pt;
int total=0;
for(pt=begin;pt!=end;pt++)
total=total+*pt;
return total;
}
|