C++大学基础教程_7_5将数组传递给函数

//_7_5_main.cpp
//传递数组和单个数组元素到函数中
//传递整个数组时是引用传递,对引用的任何修改都会修改原数组,
//传递某个数组元素时是直接传值调用,,,,
#include <iostream>
#include <iomanip>
using namespace std;

void modifyArray(int [] ,int);//传递整个数组
void modifyElement(int);//传递数组元素

int main()
{
	const int arraySize = 5;
	
	//调用一个const类型的数组时不可改变其中的数组元素值,
        //否则就会出现编译错误!!!!
	int a[arraySize] = {1,2,3,4,5};
	//打印没有引用之前的数组
	cout << "Effects of passing entire array by reference:" 
		   << "\nThe values of the original array are :" << endl;
	for(int k=0;k<arraySize;k++)
		cout << setw(3) << a[k];
	//引用整个数组
	modifyArray(a,arraySize);
	//打印引用之后的数组
	cout << "\nThe values of the modified array are:" << endl;
	for(int n=0;n<arraySize;n++)
		cout << setw(3) << a[n];
	cout << "\n\nEffects of passing element by value:" 
		   << "\na[3] before modifyElement :" << a[3] << endl;
	modifyElement(a[3]);
	cout << "a[3] after modifyElement :" << a[3] << endl;

	system("pause>>cout");
	return 0;
}

void modifyArray(int b[],int sizeOfArray)
{
	for(int i=0;i<sizeOfArray;i++)
		b[i]*=2;
}
void modifyElement(int e)
{
	cout << "Value of element in modifyElement:" 
                << (e*=2) << endl;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值