实验名称 | 指针作为函数参数和堆内存管理 |
实验目的 |
|
实验任务 |
8.1 第180页 修正版第182页 ex08_01 下面的程序调用了findmax()函数,该函数寻找数组中的最大元素,将该元素的下标通过参数返回,并返回其地址值,编程实现findmax()函数。 #include<iostream> using namespace std; int *findmax(int *array,int size,int *index); int main() { int a[]= {33,91,54,67,82,37,85,63,19,68}; int *maxaddr; int idx;
maxaddr=findmax(a,sizeof(a)/sizeof(*a),&idx);
cout<<"the index of maximum element is "<<idx<<endl; cout<<"the address of is "<<maxaddr<<endl; cout<<"the valve of is "<<a[idx]<<endl; return 0; }
8.4 第181页 修正版第183页 ex08_04 编写程序,将输入的一行字符加密和解密。加密时,每个字符依次反复加上“4962873”中数字,如果范围超过ASCII码的032(空格)~122('z'),则进行模运算,解密与加密的顺序相反,编制加密和解密函数,打印各个过程的结果。 例如,加密:the result of 3 and 2 is not 8 得到密文为:xqk"zlvyuz"wm#7)gpl'5$ry"vvw$A |
实验内容
#include<iostream> using namespace std; int main() { int *iPtr; int iCount =18; iPtr=&iCount; cout <<*iPtr<< endl; }
#include<iostream> using namespace std; int main() { int iArray[10]; int *iPtr=iArray; for(int i=0;i<10;i++)
iArray[i]=i*2; for(int idx=0;idx<10;idx++,iPtr++) cout << "&Array["<<idx<<"]:"<<iPtr<<"=>"<<*iPtr<< endl;
}
#include<iostream> using namespace std; int main() { cout <<"please input a number of array: "; int aSize; cin >>aSize; int *array=new int[aSize]; for(int i=0;i<aSize;i++) array[i]=i*2; for(int i=0;i<aSize;i++) cout << array[i]<< " "<< endl; delete[] array; }
#include<iostream> using namespace std; void Sum(int array[],int n) { int sum=0; for(int i=0;i<n;i++) { sum+=*array; array++; } cout << sum << endl; } int main() { int a[]={1,2,3,4,5,6,7,8,9,10}; Sum(a,10); }
#include<iostream> using namespace std; int *findmax(int *array,int size,int *index) { int max=array[0]; int num; for(int i=0;i<size;i++) { if(array[i]>max) { max=array[i]; *index=i; num=i; } } return &array[num]; } int main() { int a[]= {33,91,54,67,82,37,85,63,19,68}; int *maxaddr; int idx;//最大元素的下标 maxaddr=findmax(a,sizeof(a)/sizeof(*a),&idx);//返回最大元素的地址 cout<<"the index of maximum element is "<<idx<<endl; cout<<"the address of is "<<maxaddr<<endl; cout<<"the valve of is "<<a[idx]<<endl; return 0; }
#include<iostream> #include<string> using namespace std; int main() { string s; getline(cin,s); int num[]={4,9,6,2,8,7,3}; int j=0; for(int i=0;i<s.length();i++) { s[i]+=num[j]; j=(j+1)%7; } cout << s << endl; return 0; } 0 | |
小结 |
c++高级程序设计语言实验七指针作为函数参数和堆内存管理
最新推荐文章于 2024-05-09 21:25:10 发布