资源预览内容
第1页 / 共40页
第2页 / 共40页
第3页 / 共40页
第4页 / 共40页
第5页 / 共40页
第6页 / 共40页
第7页 / 共40页
第8页 / 共40页
第9页 / 共40页
第10页 / 共40页
亲,该文档总共40页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
03 C+ 程序中的数组,数组的定义,数组的定义类型 数组名 常量表达式 float sheep10;int a20011000;强调:数组下标从0开始int a10= 1,2,3,4,5,6,7,8,9,10;,关于数组的定义 (1/3),#includeusing namespace std;int main()int n = 10;int an = 0 ;for (int i = 0; i n; i+)cout ai;return 0;,此处应为常量表达式,关于数组的定义 (2/3),#include using namespace std;int main()const int i = 4;int ai = 1, 2, 3, 4 ;cout a0= a0 endl a1= a1 endl a2= a2 endl a3= a3 endl;return 0;,关于数组的定义 (3/3),#include using namespace std;#define N 4int main()int aN = 1, 2, 3, 4 ;cout a0= a0 endl a1= a1 endl a2= a2 endl a3= a3 endl;return 0;,数组的初始化,int a10 = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ;,数组的初始化 (1/5),#include using namespace std;int main()int a4;cout a0 a1 a2 a3 endl;return 0;,数组的初始化 (2/5),#include using namespace std;int main()int a= 1, 2, 3, 4 ;cout a0 a1 a2 a3 endl;return 0;,数组的初始化 (3/5),#include using namespace std;int main()int a4= 1, 2;cout a0 a1 a2 a3 endl;return 0;,数组的初始化 (4/5),#include using namespace std;int main()int a4= 0;cout a0 a1 a2 a3 endl;return 0;,数组的初始化 (5/5),#include using namespace std;int main()int a= 1, 2, 3, 4, 5, 6;cout a0 a1 a2 a3 endl;return 0;,从 一维数组 到 二维数组,int a12 = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ;,二维数组,int a34;,二维数组的初始化 (1/6),#include #include using namespace std;int main()int a34 = 1, 2, 3, 4 , 5, 6, 7, 8 , 9, 10, 11, 12 ;for (int i = 0; i 3; i+)for (int j = 0; j 4; j+)cout setw(3) aij;cout endl;return 0;,二维数组的初始化 (2/6),#include #include using namespace std;int main()int a34 = 1, 2, 3, 4 , 5, 6, 7, 8 , 9, 10, 11, 12 ;for (int i = 0; i 3; i+)for (int j = 0; j 4; j+)cout setw(3) aij;cout endl;return 0;,二维数组的初始化 (2/6),#include #include using namespace std;int main()int a 4 = 1, 2, 3, 4 , 5, 6, 7, 8 , 9, 10, 11, 12 ;for (int i = 0; i 3; i+)for (int j = 0; j 4; j+)cout setw(3) aij;cout endl;return 0;,二维数组的初始化 (4/6),#include #include using namespace std;int main()int a 4 = 1, 0, 6, 0, 0, 11;for (int i = 0; i 3; i+)for (int j = 0; j 4; j+)cout setw(3) aij;cout endl;return 0;,二维数组的初始化 (5/6),#include #include using namespace std;int main()int a34 = 0;for (int i = 0; i 3; i+)for (int j = 0; j 4; j+)cout setw(3) aij;cout endl;return 0;,二维数组的初始化 (6/6),int main()int a34 = 0 ;for (int i = 0; i 3; i+)for (int j = 0; j 4; j+)aij = 4 * i + j + 1;for (int i = 0; i 3; i+)for (int j = 0; j 4; j+)cout setw(3) aij;cout endl;return 0;,二维数组,int a34;,a34; a434; a34; a334; a34; a234; a34; a134; a34; a034;,三维数组,三维数组,int a534;,三维数组,int a534;,int a34;,int main()int a534 = 0 ;for (int i = 0; i 5; i+)for (int j = 0; j 3; j+)for (int k = 0; k 4; k+)aijk = 12 * i + 4 * j + k + 1;for (int i = 0; i 5; i+)for (int j = 0; j 3; j+)for (int k = 0; k 4; k+)cout setw(3) aijk;cout endl;cout num;for (j = 0; j num;for (int j = 0; j 10; j+)if (num = j) countj+;for (int i = 0; i 10; i+)if (counti != 0) cout i 输入了 counti 次 num;switch(num) case 0 : count0+;break; case 1 : count1+;break;case 2 : count2+;break;- ,#include using namespace std;int main()int num, count10 = 0;for (int i = 1; i num;switch (num)case 0: count0+; break;case 1: count1+; break;case 2: count2+; break;case 3: count3+; break;case 4: count4+; break;case 5: count5+; break;case 6: count6+; break;case 7: count7+; break;case 8: count8+; break;case 9: count9+; break;for (int i = 0; i 10; i+)if (counti != 0)cout i 输入了 counti 次 num;countnum+;,#include using namespace std;int main()int num, count10 = 0;for (int i = 0; i num;countnum+;for (int i = 0; i 10; i+) if (counti != 0) cout i 输入了counti 次endl;return 0;,例2 数字统计,问题某学校有1000位老师,分布在20个不同的学院中,每个学院最多有12个系,请你编写一个程序,输入每位老师的所在院、系的编号(院编号1-20,系编号1-12),打印出各个系老师的数量。分析你的解决方案?,
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号