第四章 数组
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] ,称为数组元素或下标变量。
第四章 数组
例: char c[5]; // 数组元素 c[0] , c[1] , c[2] , c[3] , c[4] 均为 char 型。
例: int iArray[10]; // 全局数组 , 初始化为0, 跨文件作用域 , 静态生存期。
第四章 数组
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]={};//错误
第四章 数组
(2) 初始化值个数少于数组元素个数时,不足的初始化为 0( 全局或静态数组 ) 或不确定( 局部数组 ) 。
例: #include<iostream.h> int array1[5]={1,2,3}; static int array2[5]={1}; void main() {
第四章 数组
int arr1[5]={2}; static int arr2[5]={1,2}; int n; cout<<"global:\n"; for(n=0;n<5;n++) cout<<" "<<array1[n];
第四章 数组
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];
第四章 数组
cout<<"\nlocal static:\n"; for(n=0;n<5;n++) cout<<" "<<arr2[n]; cout<<endl; }
第四章 数组
运行结果: 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
第四章 数组
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’};
第四章 数组
3 .省略数组大小:初始化数组时可省略数组大小,此时大小由初始化值的个数决定。
例: int a[]={2,4,6,8,10};
第四章 数组
例: #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; }
第四章 数组
例: #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; }
第四章 数组例: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};
第四章 数组二、数组的访问1.通过下标操作符 []访问指定的数组元素。 K= num[4]; //取指定数组元素的值 num[2]=6.7; //向指定数组元素赋值 cout<<name[3*k]; //输出指定的数组元素的值,下标可以是任意的表达式
第四章 数组例:给一维数组赋一组 Fibonacci数。void main() { int iArray[10]; iArray[0]=1; iArray[1]=1; iArray[2]=2; iArray[3]=3; //…… iArray[9]=55; //…… }
第四章 数组例:给一维数组赋一组 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]; //…… }
第四章 数组三、数组单元在内存中的排列 长度为 n的数组,下标范围为 0至 (n-1)。
int w[5]={23,24,25,26};
w[0] w[1] w[2] w[3] w[4]
23 24 25 26 0
第四章 数组4.1.2一维数组的应用举例
例 4.1:输入八个整数,然后按输入的相反顺序显示这些数据。
第四章 数组#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]<<‘’;
}
第四章 数组说明 :正向扫描数组和反向扫描数组的方法
正向扫描有 n个元素的数组的方法for(i=0;i<n;i++)
for(i=0;i<=n-1;i++)
反向扫描有 n个元素的数组的方法for(i=n-1;i>=0;i--)
第四章 数组例 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]<<‘’;
}
第四章 数组 例 4.2 输入 10个整数到一个数组中,调整这 10个数在数组中的排列位置,使得其中最小的一个数成为数组的首元素。
第四章 数组#include<iostream.h>#define size 10void 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]<<‘ ’;
}
第四章 数组
例 4.3 重新安排整数序列 12、 23、 9、 34、 45、 7、 78、 -33、 59和 3的顺序,使其按升序排列。显示排序前后的这两个整数序列。
第四章 数组#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;
第四章 数组 if(j>m){ k=data[i]; data[i]=data[m]; data[m]=k; } } cout<<endl<<“排后的序列:”; for(i=0;i<10;i++) cout<<data[i]<<‘ ’;}
第四章 数组
例 4.4 显示输出 100以内的所有质数。 质数:也叫素数,指除 1和它本身以外不能被别的数整除的整数。最小的质数是 2
第四章 数组#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 }}
第四章 数组
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]。
第四章 数组
内存表示 : 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]
第四章 数组
2 .二维数组初始化
例: int b[2][3]={{1,2,3},{4,5,6}}; int b[2][3]={1,2,3,4,5,6};
3 .若初始化值个数少于数组元素个数,不足的补 0( 全局数组和静态数组 ) 或不确定( 局部数组 ) 。
第四章 数组
例: #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]<<",";
第四章 数组
cout<<endl; } cout<<endl; for(i=0;i<2;i++) { for(j=0;j<3;j++) cout<<array2[i][j]<<",";
cout<<endl; } }
第四章 数组
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}};
第四章 数组4.2.2 二维数组应用举例 例输入一个 5×5的整数矩阵,然后将之转置并显示转置后的矩阵。
第四章 数组#include<iomanip>#define size 5void 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];
}
}
第四章 数组 例 4.6 定义一个 25行 25列的数组,并将其左上三角部分的元素改为 1(包括对角线,其余元素置为 0然后显示输出这个数组。
第四章 数组#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];
}
}
第四章 数组例 4.7计算如下所示两个矩阵的乘积 2 3 - 5 0 12 - 1 27 8 91 22 - 32 1
25 13 65 0 5 - 2 0 18 10 45 53 33 3 9 0 7 61 26 - 37 - 1
第四章 数组程序:#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]; }}
第四章 数组4.3 字符数组与字符串
4.3.1一维数组与字符串 C++字符串是一个以‘ \0’结为的字符序列 字符串的长度:字符串中字符的个数 字符串的存储空间:字符串中字符的个数加 1 字符串常量的表示方法:“c++程序设计“”First line\nsecond line”
第四章 数组一维字符数组的定义和初始化 char s1[]=“I am going”;
char s2[]={‘I’,’a’,’m’,’g’,’0’,’I’.’n’,’g’,’\0’};字符数组的访问 (不用使用下标,直接通过数组名访问存在于其中的字符串。)
cout<<s1<<endl<<s2;
第四章 数组4.3.2二维字符数组与字符串1.二维字符数组的定义和初始化char wd[][4]={“sun”,”mon”,”tue”,”wed”,”thu”,”fri”,”sat”};
说明:定义了一个 7行 4列的字符数组行数由初始化数据的个数决定,列数不得小于最长的初始字符串的长度加 1
第四章 数组2.二维字符数组的访问 (通过行下标访问存在于二维数组中的字符串常量 )
cout<<endl<<wd[4];
输出结果为: thu
第四章 数组4.3.3 字符串的主要操作1.求字符串的长度 调用格式: strlen(字符串) 功能:计算并返回字符串的长度。 例: cout<<strlen(“How long?”); 输出结果为: 9 cout<<strlen(wd[4]); 输出结果为:3
第四章 数组2.字符串复制 调用格式: strcpy(字符串变量,字符串) 功能:将字符串复制到字符串变量中 例: char s[]=“12345”; cout<<s<<‘’; cout<<strcpy(s,”ABCD”)<<‘’; cout<<s; 输出结果为: 12345 ABCD ABCD
第四章 数组3. 字符串的连接 调用格式: strcat(字符串变量,字符串) 功能:将字符串复制到字符串变量中原有字符串的后面,形成连接效果。例: char s[]=“12345”; cout<<s<<‘’; cout<<strcat(s,”ABCD”)<<‘’; cout<<s; 输出结果: 12345 12345ABCD 12345ABCD
第四章 数组4.字符串的比较调用格式: strcmp(s1,s2)
功能: strcmp(s1,s2)=
0 若 s1等于s2正整数 若 s1大于 s2
负整数 若 s1小于 s2
第四章 数组
if(!strcmp(“ABCD”,”abcd”) cout<<“相等”; else cout<<“ 不相等”; 输出结果为:不相等比较过程是:首先比较两个首字母的大小(排在字母表前面的比排在后面的小)
第四章 数组5.求子串位置调用格式: strstr(字符串,子串)功能:在字符串中寻找与子串相等的子串,如找到,返回该字符串的从子串起始处的后半部分,否则返回一个空字符串。
char s[]=“abcdefghi”;
cout<<s[3]<<‘ ’<<strstr(s,”def”);
输出结果为: d defghi
第四章 数组
6.字符串输出调用格式: cout<<“字符串”功能:显示输出字符串
第四章 数组7.字符串输入调用格式 1: cin>>字符串变量功能:输入一个无空白字符的字符串。例: char s1[50],s2[50];
cin>>s1>>s2;
若输入: ABCDEF 1234
第四章 数组格式 2: cin.geline(字符串变量,变量大小)功能:从键盘输入一个以回车键标志结束的字符串。例: char s[18];
cin.getline(s,18)
第四章 数组4.3.4 字符串处理应用举例
例 4.8 输入一个字符串,将其中所有的小写字母换为大写字母,大写字母换为小写字母,然后显示输出转换后的字符串。
第四章 数组#include <iostream.h>
void main()
{
char s[18];
cin.getline(s,18);
for (int i=0;s[i];i++) s[i]=(islower(s[i])?toupper(s[i]):tolower(s[i]))
cout<<s;
}
第四章 数组例 4.9:按行输入字符串,从中摘出词并建立词表。当词表中的词满 20个或发现名为 QUIT的单词时,显示输出词表,程序结束。词表中不得有重复的词,每个词的长度不超过 10。
第四章 数组#include <iostream.h>
#include <strong.h>
void main()
{ char s[11],dict[20][11];
int i;
while(i<20)
{ cin>>s;
if(!strcmp(s,”QUIT”))
break;
for(j=0;j<i;j++) if(!strcmp(s,dict[j]))break; if(j==i){ strcpy(dict[i],s); i=i++; }}for(j=0;j<i;j++)cout<<dict[j]<<endl;}
第四章 数组
本章小结:
Recommended