版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、 第五章 数 组5.1 数组的概念5.2 一维数组的定义和引用5.2.1 定义一维数组类型标识符 数组名常量表达式int a10;数组从零开始;常量表达式中不能有变量,即C+不允许对数组的大小作动态定义。数组变量占用内存的大小可用数组的大小乘上其元素的大小计算。5.2.2 引用一维数组的元素数组名下标a0=a5+a7-a2*3;例5.1 数组元素的引用#include using namespace std;void main()int i,a10;for(i=0;i=0;i-)coutai ;coutendl;9 8 7 6 5 4 3 2 1 05.2.3 一维数组的初始化(1) int
2、a10=0,1,2,3,4,5,6,7,8,9;(2) int a10=0,1,2,3,4;则后5个默认为0(3) int a=0,1,2,3,4;则a是有五个元素的数组5.2.4 一维数组程序举例例5.2 用数组来处理求Fibonacci数列问题.#include #include using namespace std;void main()int i,f20=1,1;for(i=2;i20;i+)fi=fi-2+fi-1;for(i=0;i20;i+)if(i%5=0) coutendl; coutsetw(8)fi;coutendl; 1 1 2 3 5 8 13 21 34 55 8
3、9 144 233 377 610 987 1597 2584 4181 67655.3 二维数组的定义和使用5.3.1 定义二维数组类型标识符 数组名常量表达式 常量表达式int a34;数组元素是按行存放的。5.3.2 二维数组的引用a12=6;5.3.3 二维数组的初始化(1) int a34=1,2,3,4,5,6,7,8,9,10,11,12;(2) int a34=1,2,3,4,5,6,7,8,9,10,11,12;(3) int a34=1,5,9;(4) int a4=2,3,4,0,10;则数组共有三行。 5.3.4 二维数组程序举例例5.4 将一个二维数组行和列元素互换,
4、存到另一个二维数组中。#include using namespace std;void main()int a23=1,2,3,4,5,6;int b32,i,j;coutarray a:endl;for(i=0;i2;i+)for(j=0;j3;j+)coutaij ;coutendl;coutarray b:endl;for(i=0;i3;i+)for(j=0;j2;j+)cout(bij=aji) ;coutendl;array a:1 2 34 5 6array b:1 42 53 6例5.5 有一个3X4的矩阵,要求编程序求出最大值,以及其所在的行号和列号。#include usi
5、ng namespace std;void main()int i,j,row=0,colum=0,max;int a34=5,12,23,56,19,28,37,46,-12,-34,6,8;max=a00;for(i=0;i3;i+)for(j=0;jmax)max=aij;row=i;colum=j;coutmax=max,row=row,colum=columendl;max=56,row=0,colum=35.4 用数组名作函数参数1. 用数组元素作函数实参例5.6 用函数处理例5.5#include using namespace std;void main()bool if_ma
6、x(int x,int max);int i,j,row=0,colum=0,max;int a34=5,12,23,56,19,28,37,46,-12,-34,6,8;max=a00;for(i=0;i3;i+)for(j=0;j4;j+)if(if_max(aij,max)max=aij;row=i;colum=j;coutmax=max,row=row,colum=colummax) return true;else return false;数组元素可以用在该类型变量可用的任何地方。2. 用数组名作函数参数例5.7 用选择法对数组中10个整数按由小到大排序。3,6,1,9,4#inc
7、lude using namespace std;void main()void select_sort(int array,int n);int a10,i;coutenter the original array:endl;for(i=0;iai;select_sort(a,10);coutthe sorted array:endl;for(i=0;i10;i+) coutai ;coutendl;void select_sort(int array,int n)int i,j,k,t;for(i=0;in-1;i+)k=i;for(j=i+1;jn;j+)if(arrayjarrayk)
8、k=j;t=arrayk;arrayk=arrayi;arrayi=t;enter the original array:6 9 -2 56 87 11 -54 3 0 77the sorted array:-54 -2 0 3 6 9 11 56 77 87关于数组名作函数参数有两点要说明:(1) 函数形参是数组名,实参也是数组名。(2) 数组名代表数组首元素的地址,并不代表数组中的全部元素。3. 用多维数组名作函数参数例5.8 有一个3X4的矩阵,求矩阵中所有元素中的最大值。要求用函数处理。#include using namespace std;void main()int max_va
9、lue(int array4);int a34=11,32,45,67,22,44,66,88,15,72,43,37;coutmax value is max_value(a,3)endl;int max_value(int array4,int n)int i,j,max;max=array00;for(i=0;in;i+)for(j=0;jmax) max=arrayij;return max;max value is 885.5 字符数组5.5.1 字符数组的定义和初始化char c10;c0=I;c1= ;char c10=I, ,a,m, ,h,a,p,p,y;5.5.2 字符数组
10、的赋值和引用例5.9 设计和输出一个钻石图形。#include using namespace std;void main()char diamond5= , ,*, ,*, ,*,*, , , ,*, ,*, ,*, , ,*;int i,j;for(i=0;i5;i+)for(j=0;j5;j+)coutdiamondij;coutendl; * * * * * * *5.5.3 字符串和字符串结束标志 0char str=I am happy;与char str11=I, ,a,m, ,h,a,p,p,y;相同char str11=I, ,a,m, ,h,a,p,p,y,0;5.5.4
11、字符数组的输入与输出(1) 逐个字符输入输出(2) 将整个字符串一次输入或输出#include using namespace std;void main()char str=I am happy;char str111=I, ,a,m, ,h,a,p,p,y;char str211=I, ,a,m, ,h,a, p,p,y,0;char str310=I, ,a,m, ,h,a,p,p,y;cinstr;coutstr1endl;coutstr2endl;coutstr3endl;I am happyI am happyI am happyI am happy5.5.5 字符串处理函数1.
12、字符串连接函数strcatstrcat(char,const char);例#include using namespace std;void main()char str130=Peoples Republic of ;char str2=china;strcat(str1,str2);coutstr1endl;Peoples Republic of china2. 字符串复制函数strcpystrcpy(char,const char); 例#include using namespace std;void main()char str130=Peoples Republic of;cha
13、r str2=china;strcpy(str1,str2);coutstr10) coutyes;4. 字符串的长度strlen(const char);#include using namespace std;void main()char str10=china;coutstrlen(str)endl;55.5.6 字符串应用举例例5.10 有3个字符串,要求找出其中最大者,要求用函数调用。#include using namespace std;void main()void max_string(char str30,int i);int i;char country_name330
14、;for(i=0;icountry_namei;max_string(country_name,3);void max_string(char str30,int n)char string30;strcpy(string,str0);if(strcmp(str1,string)0) strcpy(string,str1);if(strcmp(str2,string)0) strcpy(string,str2);coutthe largest string is:stringendl;CHINAGERMANYFRANCHthe largest string is:GERMANY5.6 C+处理
15、字符串的方法-字符串类与字符串变量string不是C+语言的基本数据类型,它是C+标准库中声明的一个字符串类。使用时应加 #include 5.6.1 字符串变量的定义和引用1 定义字符串变量string string1;string string2=China;2. 对字符串变量的赋值string1=Canada;char str10;str=Hello!; /错误string1=string2;string word= Then;word2=a;3. 字符串变量的输入和输出cinstring1;coutstring2;例#include #include using namespace s
16、td;void main()string string1;string string2=China;string1=Canada;char str10;/str=Hello!; /错误string1=string2;string word= Then;word2=a;cinstring1;coutstring2endl;coutword = =#include #include using namespace std;void main()string string1=C+ ;string string2=Language;string1+=string2;coutstring1endl;5.
17、6.3 字符串数组#include #include using namespace std;void main()string name5=Zhang,Wang,Li,Zhao,Tan;coutname1.length()endl;45.6.4 字符串运算举例例5.11 输入3个字符串,要求将字母按由小到大的顺序输出。#include #include using namespace std;void main()string string1,string2,string3,temp;coutstring1string2string3;if(string2string3)temp=string2;string2=string3;string3=temp;if(string1=string2) coutstring1 string2 string3endl;else if(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 广安市前锋区广兴镇片区纪检监督员招聘考试备考试题及答案解析
- 2026年国营红明农场医院医护人员招聘笔试模拟试题及答案解析
- 2026年河北省社区工作者招聘考试备考试题及答案详解
- 2026重庆市两江中学校西南政法大学附中应届高校毕业生招聘7人笔试参考题库及答案解析
- 2026年杭州市林业系统事业单位人员招聘考试备考试题及答案详解
- 2026年金融数字货币支付系统安全创新报告
- 2026年湖州市城管协管人员招聘考试备考试题及答案详解
- 高中艺术教学中数字绘画与多传感器融合的情感表达设计课题报告教学研究课题报告
- 2026年抚州市信访系统事业单位人员招聘考试备考试题及答案详解
- 2026浙江嘉兴市体育产业发展投资有限公司招聘(劳务派遣制)工作人员岗位2人考试备考试题及答案解析
- 全民国家安全教育日知识普及课件
- (正式版)DB36∕T 1442.6-2022 《水利工程标准化管理规程 第6部分:农村水电站》
- 中国人民革命军事博物馆
- 跆拳道训练体系
- 航天发射与卫星运维手册
- 2026年1月浙江省首考地理真题卷(附答案解析)
- 急诊科气道异物急救护理流程
- 超长期特别国债项目申报工作指南
- 2026云南昆明市官渡区国有资产投资经营有限公司招聘5人考试备考试题及答案解析
- 2026年及未来5年市场数据中国防静电防潮袋行业发展监测及投资战略咨询报告
- 食品生产供应商管理制度
评论
0/150
提交评论