C语言程序7章数组.ppt

大学C语言程序设计-任正云-课件PPT

收藏

资源目录
跳过导航链接。
大学C语言程序设计-任正云-课件PPT.zip
C语言程序设计-任正云-PPT演示文稿
教案资料.ppt---(点击预览)
C语言程序设计-任正云-PPT课件文件
文稿ppt_ppt.txt---(点击预览)
文稿ppt_ppt.jpg---(点击预览)
文稿ppt.ppt---(点击预览)
C语言程序设计-任正云-大学教学资料
(课件资料)《C语言程序设计》-任正云-电子教案
压缩包内文档预览:(预览前20页/共43页)
预览图 预览图 预览图 预览图 预览图 预览图 预览图 预览图 预览图 预览图 预览图 预览图 预览图 预览图 预览图 预览图 预览图 预览图 预览图 预览图
编号:21836451    类型:共享资源    大小:13.43MB    格式:ZIP    上传时间:2019-09-06 上传人:QQ24****1780 IP属地:浙江
25
积分
关 键 词:
大学 语言程序设计 任正云 课件 ppt
资源描述:
大学C语言程序设计-任正云-课件PPT,大学,语言程序设计,任正云,课件,ppt
内容简介:
第七章 数组,The C Programming Language Chapter 7 Arrays,构造数据类型; 有序数据的集合; 可以看成是具有相同名字不同下标的同一数据类型的简单变量的集合.,一维数组 二维数组 字符数组,7.1 一维数组 定义: 类型说明 数组名正整型常量表达式;,定义数组名,数组中元素的个数;, :下标的界定符,不能用 ( ),注意事项: 数组必须先定义后使用; 只能一个一个使用数组元素; 数组元素的使用 : 例 ai ( i: 0N-1),例 int i=15; int datai; /表达式不能使变量,例 int data5; data5=10; /超出范围,例 int a10; printf(“%d“,a); () = for(j=0; j10; j+) printf(“%dt“,aj); (),初始化,注意事项: 超出数组, 数据个数多于数组元素的个数,是不允许的.,对全部数组元素赋初值时,可以不指定数组长度.,例 int a5=6,2,3; =: a0=6; a1=2;a2=3; a3=0; a4=0; 例 int a3=6,2,3,5,1; (),初始化时,只对部分赋值,则其他元素的初值为0.,例 int a=1,2,3,4,5,6; / 数组的长度是 6,例题讲解,例1 分析下面函数的输出结果。,main() int i,a10; for(i=0;i=0;i-); printf(“%5d“,ai); /*输出第i个元素值*/ ,运行结果为:9 8 7 6 5 4 3 2 1 0,例2:用数组求 Fibonacci数列的前20项,#include main() int i; int f20=1,1; for(i=2;i20;i+) fi=fi-2+fi-1; for(i=0;i20;i+) if(i%5=0) printf(“n“); printf(“%12d“,fi); ,例题讲解,例3一维数组的输入与输出。,# include“stdio.h“ main() int i; int a4; /*说明了一个元素个数为4的整型数组*/ printf(“n输入数组a(共4个整数):“); for(i=0;i4;i+); scanf(“%d“, ,运行结果为: 输入数组a(共4个整数):4 5 6 12 输出数组a:a0=4 a1=5 a2=6 a3=12,例题讲解,例4求数组元素中 的最大值最小值。,# include “stdio.h“ main() int term10; int i,max,min; printf(“请输入10个整数:“); for(i=1;imax) max=termi; printf(“最大数为%d“,max); printf(“最小数为%d“,min); ,例题讲解,例5计算并输出全班30个学生C语言程序设计课程的平均成绩, 以及每个人的成绩与平均成绩之差。,int main(void) float x1,x2, ,x30,sum, average; printf(“input 30 scores:n“); scanf(“%f%f%f“, ,# include “stdio.h“ # define NUMBERS 30 int main(void) int i; float xNUMBERS,sum,average; sum=0; printf(“input %d scores:n“, NUMBERS); for(i=0;i NUMBERS;i+) scanf(“%f“, ,例6: 用起泡法(下沉法)将8个数排序,38 49 65 76 13 27 30 97 2,38 49 65 13 27 30 76 97 3,38 49 13 27 30 65 76 97 4,38 13 27 30 49 65 76 97 5,13 27 30 38 49 65 76 97 6,13 27 30 38 49 65 76 97 7,49 38 65 97 76 13 27 30 1,n=8,38,49,76,97,13,97,27,97,30,97,13,76,76,76,27,30,13,65,27,65,30,65,13,13,49,49,30,49,27,38,27,38,30,38,步骤:,30,27,#include main() int a9,i,j,t; printf(“Input 8 numbers:n“); for(i=1;iai+1) t=ai; ai=ai+1; ai+1=t; printf(“The sorted numbers:n“); for(i=1;i9;i+) printf(“%d “,ai); ,1: 49 38 65 97 76 13 27 ,13,49,2: 13 38 65 97 76 49 27 ,27,38,13 27 38 49 65 76 97,例4: 用选择法对7个数排序,步骤:,j,k两个变量的说明: K:标注最小值的位置; J:寻找最小值。,#include main() int a8,i,j,k,x; printf(“Input 7 numbers:n“); for(i=1;i8;i+) scanf(“%d“, ,7.2 二维数组 定义: 类型说明 数组名常量表达式1常量表达式2,数组元数的排列顺序: 按行存放,即在内存中先顺序存放第一行的元素,再存放第二行的元素。,例 int a34; float b25; int a3,4; (),行数,列数,数组元素个数=行数*列数,说明 :,每一个元素 ai 是包含 4 元素 的一维数组,a 可以看作是一个含有3个一维 数组元素的一维数组;,引用 例 aij ( i: 0N-1, j: 0M-1) 初始化,例题讲解,例7 向一个三维数组输入值并输出此数组全部元素。,main() int i,j,k,a232; for(i=0;i2;i+) for(j=0;j3;j+) for(k=0;k2;k+) scanf(“%d“, ,若输入: 1 2 3 4 5 6 7 8 9 10 11 12,a000=1 a001=2 a010=3 a011=4 a020=5 a021=6 a100=7 a101=8 a110=9 a111=10 a120=11 a121=12,例题讲解,例7 求一个4*4阶矩阵元素的最小值。,# include “stdio.h“ main() static int a44=12,76,4,1,-19,28,55,-6, 2,10,13,-2,3,-9,112,111; int i,j,row,colum,min; min=a00; for(i=0;i4;i+) for(j=0;j4;j+) if(aijmin) min=aij; row=i,colum=j; printf(“最小值=%d,位于矩阵第%d行,第%d列n“,min,row+1,colum+1); ,运行结果如下: 最小值= 19,位于矩阵第2行,第1列,例题,例 8 将二维数组行列元素互换,存到另一个数组中,例 求二维数组中最大元素值及其行列号,#include main() int a34=1,2,3,4, 9,8,7,6, -10,10,-5,2; int i,j,row=0,colum=0,max; max=a00; for(i=0;imax) max=aij; row=i; colum=j; printf(“max=%d,row=%d, colum=%dn“,max,row,colum); ,例 有a个学生,每个学生学b门课,已知所有学生各门课的成 绩,分别求每门课的平均成绩和每个学生的平均成绩。,void cour_ave (int a,int b,float scroe ) int course,stud; float total1 aver1; for(course=0;courseb;course+) total1=0; for(stud=0;studa;stud+) total1=total1+scorestud*b+course; aver1=total1/a; printf(“average of course %d is :%6.2fn“,course,aver1); return; void stud_ave(int a,int b,float score ) int course,stud; float total2 aver2; printf(“n“); for(stud=0;studa;stud+) ,total2=0; for(course=0;courseb;course+) total2=total2+scorestud*b+course; aver2=total2/b; printf(“average of studend %d is :%6.2fn“,stud,aver2); return; # define a 4 # define b 3 main() void cour_ave (int x, int y,float score); void stud_ave(int x,int y,float score); float score ab=89,78,56,88,99,100,72,80,61,60,70,75; cour_ave(a,b,score); stud_ave(a,b,score); ,7.3 字符数组 定义:,初始化 逐个字符赋给数组中的元素; 利用字符串常量.,例 char c10, ch34;,例1: 输出字符串,#include main() char c10=I, ,a,m, ,a, ,b,o,y; int i; for(i=0;i10;i+) printf(“%c“,ci); printf(“n“); ,字符串: 没有字符串变量,字符串用字符数组来处理 ; 一个字符串用 0作结束标志。 (或空字符),输入输出字符串 字符的输入/输出: %c 字符串的输入/输出: %s,例 使用 %c main() char str5; int i; for(i=0;i5;i+) scanf(“%c”, ,例 使用 %s main() char str5; scanf(“%s”, str); printf(“%s”, str); ,使用数组名,不需使用地址运算符 & 输入字符串的长度 数组的长度 用 空格 或 换行结束输入 自动添加结束符 0,使用数组名 用 0标志字符串的结束,例 main( ) char a5=H,e,l,l,o; printf(“%s”,a); ,例 main( ) char a =“Hello”; printf(“%s”,a); ,输出:Hello#-=*,输出 :Hello,例 main() char a=h,e,l,0,l,o,0; printf(“%s“,a); ,输出 :hel,没有 0作结束符,main() int i; char a5; scanf(“%s“,a); for(i=0;i5;i+) printf(“%c“,ai); ,运行: (1)输入 : hel , 输出 : hel (2)输入: hell, 输出 : hell (3)输入: hello, 输出 : hello#-=* 或给出错误信息,输入字符的个数 数组的长度,例: 字符串输入,#include main() char a15,b5,c5; scanf(“%s%s%s“,a,b,c); printf(“a=%snb=%snc=%sn“,a,b,c); scanf(“%s“,a); printf(“a=%sn“,a); ,运行: 输入: How are you? 输出:a=How b=are c=you? 输入: How are you? 输出:a=How,使用 scanf 输入字符串 , 不能输入空格,用 空格 或 换行表示结束输入,例 如果想将 “This is a string.” 输入数组 s, 不能采用的格式是: (A)scanf(“%20s“,s); (B)for(k=0;k17;k+) sk=getchar(); (C)while(c=getchar()!=n) sk+=c; (D)gets(s);,字符串处理函数,puts(字符数组名) stdio.h puts 输出字符串,可以包含转义字符; 字符数组必须具有字符串结束标记 0,gets(字符数组名) stdio.h gets 输入一个字符串到字符数组,换行表示 输入字符串结束 ; 自动在输入字符串后加入结束标志 0 ; 字符串长 数组长度 函数的返回值是数组的首地址,一般不关心。 只输入一个字符串。,例 #include main( ) char string80; printf(“Input a string:“); gets(string); puts(string); input: How are you? output: How are you ?,strcat(字符数组名1,字符数组名2) string.h 将字符数组2 连接到字符数组1的后面,连接时 自动取消字符数组1后面的结束标志; 结果存放到字符数组1中,函数返回值是字符数组1 的首地址; 字符数组1要有足够的空间存放结果 ;,strcpy(字符数组1,字符串2) string.h 将 字符串2 复制到字符数组1,包括 0; 字符数组1 必须有足够的空间存放结果; 字符串2可以是数组名。 可以将字符串2前面的若干个字符赋值到字符数组1中去。 不能用赋值语句将一个字符串赋值给字符数组,例 char str120, str220; str1=“Hello! “; () str2=str1; (),例: strcpy 和 strcat,#include #include main() char destination25; char blank = “ “, c= “C+“, turbo = “Turbo“; strcpy(destination, turbo); strcat(destination, blank); strcat(destination, c); printf(“%sn“, destination); ,Turbo C+,strcmp(字符串1,字符串2) string.h 用字符的 ASCII 值逐个对应比较 字符串1 与 字符串2 字符的大小 (left-to-right); if 字符串1 字符串2 ,返回0 ; 不能用 = 去直接比较两个字符串的大小.,strlen(字符数组) string.h 返回字符串的长度 ; 函数的值是字符串的实际长度 ,不包括 0.,例 strlen(s) 的结果: (1)char s10=A, 0, B, C, 0, D; (2)char s =“tb0willn“; (3)char s =“x69072n“;,结果:1 3 1,#include #include main() char str1 = “Hello! “, str2 = “ How are you? “,str20; int len1,len2,len3; len1=strlen(str1); len2=strlen(str2); if(strcmp(str1, str2)0) strcpy(str,str1); strcat(str,str2); else if (strcmp(str1, str2)0) strcpy(str,str2); strcat(str,str1); else strcpy(str,str1); len3=strlen(str); puts(str); printf(“ Len1=%d,Len2=%d,Len3=%dn “,len1,len2,len3); ,How are you?Hello! Len1=6,Len2=12,Len3=18,例 : strcmp 和 strlen,例 : 输入一行字符,统计单词的个数,单词之间用空格隔开。,#include main() char string81; int i,num=0,word=0; char c; gets(string); for(i=0;(c=stringi)!=0;i+) if(c= ) word=0; else if(word=0) word=1; num+; printf(“There are %d words in the linen“,num); ,例 : 有3个字符串,要求找出其中最大的. (使用 二维数组),#include #include main() char string20,str320; int i; for(i=0;i0) strcpy(string,str0); else strcpy(string,str1); if(strcmp(str2,string)0) strcpy(string,str2); printf(“nThe largest string is:n%sn“,string); ,例 比较 int a23=5,6,7,8; 两者 int a23=5,6,7,8;,例 int a10; float f2=1.2 ,2.2;,例 int a5; a=2,4,6,8,10;,例 int a10; float i=3; ai=10;,例 char name0; float weight10.3; int array-100;,例 char str=“Hello“; char str=H , e , l , l , o ;,说明:,数组的应用举例,7.4.1 排序,直接插入排序法,元素下标:(0 1 2 3 4) 初始数据: 5 3 4 1 2 第一步插入:3 5 4 1 2 第二步插入:3 4 5 1 2 第三步插入:1 3 4 5 2 第四步插入:1 2 3 4 5,# include “stdio.h“ main() int i,j; int a5,t; printf(“请输入5个整数:“); for(i=0;i5;i+) scanf(“%d“,while(j=0 ,数组的应用举例,7.4.1 SHELL排序,# include “stdio.h“ # define N 9 main( ) int i,j,d,t; int aN=7,3,2,5,9,1,4,6,8 d=N/2; /*分组*/ while(d1) for (i=d;i=0) if (ajaj+d) t=aj+d; aj+d=aj; aj=t; j=j-d; else j =-1; d/=2; /*减少分组数*/ printf(“排序结果为:“) for(i=0;iN;i+) p
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
提示  人人文库网所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
关于本文
本文标题:大学C语言程序设计-任正云-课件PPT
链接地址:https://www.renrendoc.com/p-21836451.html

官方联系方式

2:不支持迅雷下载,请使用浏览器下载   
3:不支持QQ浏览器下载,请用其他浏览器   
4:下载后的文档和图纸-无水印   
5:文档经过压缩,下载后原文更清晰   
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

网站客服QQ:2881952447     

copyright@ 2020-2025  renrendoc.com 人人文库版权所有   联系电话:400-852-1180

备案号:蜀ICP备2022000484号-2       经营许可证: 川B2-20220663       公网安备川公网安备: 51019002004831号

本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知人人文库网,我们立即给予删除!