1 / 59

第四章   数组 

第四章   数组 . 常量表达式中不能是变量. 4.4.1 一维数组的定义和初始化 一、一维数组的定义和初始化 1 .一维数组定义  定义格式:类型 数组名 [ 常量表达式 ]; // 常量表达式中不能是变量 . 第四章   数组. 例: int a[10];  定义一个一维数组,数组名为 a ,此数组共有 10 个元素,且均为整型,这 10 个元素分别为 a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9] ,称为数组元素或下标变量。 . 第四章   数组.

Download Presentation

第四章   数组 

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第四章   数组  常量表达式中不能是变量 4.4.1一维数组的定义和初始化 一、一维数组的定义和初始化 1.一维数组定义  定义格式:类型 数组名[常量表达式]; // 常量表达式中不能是变量 

  2. 第四章   数组 例:int a[10];  定义一个一维数组,数组名为a,此数组共有10个元素,且均为整型,这10个元素分别为a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9],称为数组元素或下标变量。 

  3. 第四章   数组 例:char c[5];//数组元素c[0],c[1],c[2],c[3],c[4]均为char型。  例:int iArray[10];//全局数组,初始化为0,跨文件作用域,静态生存期。 

  4. 第四章   数组 2.一维数组的初始化 (1)初始化时,初始化值个数不能多于数组元素个数,初始化值不能省略。  例: int array1[5]={1,2,3,4,5,6};//错误  int array2[5]={1,,2,3,4};//错误  int array3[5]={1,2,3,};//错误  int array4[5]={};//错误 

  5. 第四章   数组 (2)初始化值个数少于数组元素个数时,不足的初始化为0(全局或静态数组)或不确定(局部数组)。  例:#include<iostream.h> int array1[5]={1,2,3}; static int array2[5]={1}; void main() {

  6. 第四章   数组 int arr1[5]={2}; static int arr2[5]={1,2}; int n; cout<<"global:\n"; for(n=0;n<5;n++) cout<<" "<<array1[n];

  7. 第四章   数组 cout<<"\nglobal static:\n"; for(n=0;n<5;n++) cout<<" "<<array2[n]; cout<<"\nlocal:\n"; for(n=0;n<5;n++) cout<<" "<<arr1[n];

  8. 第四章   数组 cout<<"\nlocal static:\n"; for(n=0;n<5;n++) cout<<" "<<arr2[n]; cout<<endl; }

  9. 第四章   数组  运行结果:global: 1 2 3 0 0 global static: 1 0 0 0 0 local: 2 23567 23567 23567 local static: 1 2 0 0 0

  10. 第四章   数组 3.初始化字符数组  注:长度为n的字符数组至少需要n+1个字节的存储空间。  例:char array[10]={"hello"}; char array[10]={‘h’,‘e’,‘l’,‘l’,‘o’,‘\0’}; char array[10]="hello"; char array[5]=“hello”;//错误  char array[5]={ ‘\t’,‘\t’,‘\t’,‘\t’,‘\0’};

  11. 第四章   数组 3.省略数组大小:初始化数组时可省略数组大小,此时大小由初始化值的个数决定。  例:int a[]={2,4,6,8,10};

  12. 第四章   数组 例:#include<iostream.h> void main() { static int a[]={1,2,4,8,16}; for(int i=0;i<(sizeof(a)/sizeof(a[0]));i++) cout<<a[i]<<" "; cout<<endl; }

  13. 第四章   数组 例:#include<iostream.h> #include<string.h> void main() { char ch[]="how are you"; cout<<"size of array:"<<sizeof(ch)<<endl; cout<<"size of string:“ <<strlen("how are you")<<endl; }

  14. 第四章 数组 例: char s1[80]; int d[]={1,2,3,4,5,6}; double num[5]={1.0,3.0,5.0}; long ldata[10]={0l} #define first 15 int D3[]={firsr,first+2,first+3};

  15. 第四章 数组 二、数组的访问 1.通过下标操作符[]访问指定的数组元素。 K=num[4]; //取指定数组元素的值 num[2]=6.7; //向指定数组元素赋值 cout<<name[3*k]; //输出指定的数组元素的值,下标可以是任意的表达式

  16. 第四章   数组 例:给一维数组赋一组Fibonacci数。 void main() {int iArray[10]; iArray[0]=1; iArray[1]=1; iArray[2]=2; iArray[3]=3; //…… iArray[9]=55; //…… }

  17. 第四章   数组 例:给一维数组赋一组Fibonacci数,使用一重循环进行。  void main() { int iArray[10]; iArray[0]=1; iArray[1]=1; for(int i=2;i<10;i++) iArray[i]=iArray[i-1]+iArray[i-2]; //…… }

  18. 第四章 数组 三、数组单元在内存中的排列 长度为n的数组,下标范围为0至(n-1)。 int w[5]={23,24,25,26}; w[0] w[1] w[2] w[3] w[4]

  19. 第四章 数组 4.1.2一维数组的应用举例 例4.1:输入八个整数,然后按输入的相反顺序显示这些数据。

  20. 第四章 数组 #include<iostream.h> void main() { int data[8]; cout<<endl<< “请输入八个整数:”; int i; for(i=0;i<8;i++) cin>>data[i]; cout<<endl; for(i=7;i>=0;i--) cout<<data[i]<<‘’; }

  21. 第四章 数组 说明:正向扫描数组和反向扫描数组的方法 正向扫描有n个元素的数组的方法 for(i=0;i<n;i++) for(i=0;i<=n-1;i++) 反向扫描有n个元素的数组的方法 for(i=n-1;i>=0;i--)

  22. 第四章 数组 例4.1的改进 #include<iostream.h> #define size 8 void main() { int data[size]; cout<<endl<<“入”<< size<<“八个数:”; int i; for(i=0;i<size;i++) cin>>data[i]; cout<<endl; for(i=size-1;i>=0;i--) cout<<data[i]<<‘’; }

  23. 第四章 数组 例4.2 输入10个整数到一个数组中,调整这10个数在数组中的排列位置,使得其中最小的一个数成为数组的首元素。

  24. 第四章 数组 #include<iostream.h> #define size 10 void main() { int data[size]; cout<<endl<<“请输入 ”<<size<<“个整数:”; int m; for(m=0;m<size;i++) cin>>data[m]; int j=0; for(int i=1;i<size;i++) if(data[i]<data[j]) j=i; if(j>0) { int k=data[0]; data[0]=data[j]; data[j]=k; } cout<<endl; for(m=0;m<size;m++) cout<<data[m]<<‘ ’; }

  25. 第四章 数组 例4.3 重新安排整数序列12、23、 9、 34、 45、 7、 78、-33、 59和 3的顺序,使其按升序排列。显示排序前后的这两个整数序列。

  26. 第四章 数组 #include <iostream.h> void main() { int data[]={12,23, 9, 34, 45, 7 ,78 ,-33 ,59 ,3}; int m; cout<<endl<<“排序前:”; for (m=0;m<10;m++) cout<<data[m]<<‘’; for (m=0;j<9;m++){ int j=m; for (int i=m+1;i<10;i++) if (data [i]<data [j]) j=i;

  27. 第四章 数组 if(j>m){ k=data[i]; data[i]=data[m]; data[m]=k; } } cout<<endl<<“排后的序列:”; for(i=0;i<10;i++) cout<<data[i]<<‘ ’; }

  28. 第四章 数组 例4.4 显示输出100以内的所有质数。 质数:也叫素数,指除1和它本身以外不能被别的数整除的整数。最小的质数是2

  29. 第四章 数组 #include<iostream.h> void main() { bool p[100]; cout <<“100以内的质数有:2”; int i; for (i=3;i<100;i+=2)p[i]=true; for (i=3;i<100;i+=2) if (p[i]){ cout <<‘ ’<<I; for(int j=i+i+I;j<100;j+=i+i) p[j]=false } }

  30. 第四章   数组 4.2 多维数组 1.二维数组定义  格式:类型 数组名[常量表达式1][常量表达式2];  例:int a[3][4];  表示定义了一个二维数组,数组名为a,此数组共有12个元素,且均为整型,这12个元素分别为a[0] [0], a[0][1], a[0][2], a[0][3], a[1][0], a[1][1], a[1][2], a[1][3], a[2][0], a[2][1],a[2][2], a[2][3]。 

  31. 第四章   数组 内存表示: a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3]

  32. 第四章   数组  2.二维数组初始化  例:int b[2][3]={{1,2,3},{4,5,6}}; int b[2][3]={1,2,3,4,5,6}; 3.若初始化值个数少于数组元素个数,不足的补0(全局数组和静态数组)或不确定(局部数组)。 

  33. 第四章   数组 例:#include<iostream.h> void main() { int array1[2][3]={1,2,3,4,5}; int array2[2][3]={{1,2},{4}}; int i,j; for(i=0;i<2;i++) { for(j=0;j<3;j++) cout<<array1[i][j]<<",";

  34. 第四章   数组 cout<<endl; } cout<<endl; for(i=0;i<2;i++) { for(j=0;j<3;j++)cout<<array2[i][j]<<","; cout<<endl; } }

  35. 第四章   数组 4.省略第一维大小:初始化多维数组时,第一维大小可省略。  例:int a[][4]={1,2,3,4,5,6,7,8,9,10,11,12}; static int a[][4]={{1,2,3},{},{4,5}};

  36. 第四章 数组 4.2.2 二维数组应用举例 例输入一个5×5的整数矩阵,然后将之转置并显示转置后的矩阵。

  37. 第四章 数组 #include<iomanip> #define size 5 void main() { int data[size][size],i,j; for(i=0;i<size;i++) for(j=0;j<size;j++) cin>>data[i][j]; for(i=0;i<size-1;i++) for(j=i+1;i<size;j++){ int d=data[i][j]; data[i][j]=data[i][j]; data[i][j]=d; } for(i=0;i<size;i++) { cout<<endl; for(j=0;j<5;j++) cout<<setw(8)<<data[i][j]; } }

  38. 第四章 数组 例4.6 定义一个25行25列的数组,并将其左上三角部分的元素改为1(包括对角线,其余元素置为0然后显示输出这个数组。

  39. 第四章 数组 #include<iomanip> void main() { int data[25][25]={{0}}; int I,j; for(i=0;i<25;i++) for(j=0;j<25-i;j++) data[i][j]=1; for(i=0;i<25;i++) { cout<<endl; for(j=0;25;j++) cout<<setw(2)<<data[i][j]; } }

  40. 第四章 数组 例4.7计算如下所示两个矩阵的乘积 23  -50 12 -1278 9122  -321 25136505   -20181045 5333390 76126 -37 -1

  41. 第四章 数组 程序: #include<iomanip.h> void main() { int A[3][4]={{2,3,-5,0},{12,-1,27,8},{91,22,-32,1}}; int B[4][5]={{25,13,65,0,5},{-2,0,18,10,45}, {53,33,3,9,0},{7,61,26,-37,1}}; int c[3][5]={{0}}; int i,j; for (i=0;i<3;i++) for (j<0;j<5;j++) for(int k=0;k<4;k++) c[i][j]+=A[i][j]*B[k][j]; for(int i=0;i<3;i++) { cout<<endl; for(int j=0;j<5;j++) cout<<setw(5)<<c[i][j]; } }

  42. 第四章 数组 4.3 字符数组与字符串 4.3.1一维数组与字符串 • C++字符串是一个以‘\0’结为的字符序列 • 字符串的长度:字符串中字符的个数 • 字符串的存储空间:字符串中字符的个数加1 • 字符串常量的表示方法: “c++程序设计“ ”First line\nsecond line”

  43. 第四章 数组 • 一维字符数组的定义和初始化 char s1[]=“I am going”; char s2[]={‘I’,’a’,’m’,’g’,’0’,’I’.’n’,’g’,’\0’}; • 字符数组的访问(不用使用下标,直接通过数组名访问存在于其中的字符串。) cout<<s1<<endl<<s2;

  44. 第四章 数组 4.3.2二维字符数组与字符串 1.二维字符数组的定义和初始化 char wd[][4]={“sun”,”mon”,”tue”,”wed”,”thu”,”fri”,”sat”}; 说明:定义了一个7行4列的字符数组 行数由初始化数据的个数决定, 列数不得小于最长的初始字符串的长度加1

  45. 第四章 数组 2.二维字符数组的访问(通过行下标访问存在于二维数组中的字符串常量) cout<<endl<<wd[4]; 输出结果为:thu

  46. 第四章 数组 4.3.3 字符串的主要操作 1.求字符串的长度 调用格式:strlen(字符串) 功能:计算并返回字符串的长度。 例: cout<<strlen(“How long?”); 输出结果为:9 cout<<strlen(wd[4]); 输出结果为:3

  47. 第四章 数组 2.字符串复制 调用格式:strcpy(字符串变量,字符串) 功能:将字符串复制到字符串变量中 例: char s[]=“12345”; cout<<s<<‘’; cout<<strcpy(s,”ABCD”)<<‘’; cout<<s; 输出结果为:12345 ABCD ABCD

  48. 第四章 数组 3. 字符串的连接 调用格式:strcat(字符串变量,字符串) 功能:将字符串复制到字符串变量中原有字符串的后面,形成连接效果。 例: char s[]=“12345”; cout<<s<<‘’; cout<<strcat(s,”ABCD”)<<‘’; cout<<s; 输出结果:12345 12345ABCD 12345ABCD

  49. 第四章 数组 4.字符串的比较 调用格式:strcmp(s1,s2) 功能: strcmp(s1,s2)= 0 若s1等于s2 正整数 若s1大于s2 负整数 若s1小于s2

  50. 第四章 数组 if(!strcmp(“ABCD”,”abcd”) cout<<“相等”; else cout<<“ 不相等”; 输出结果为:不相等 比较过程是:首先比较两个首字母的大小(排在字母表前面的比排在后面的小)

More Related