




已阅读5页,还剩84页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Points and Array Xue Qing2009.4Terms the relationship between a,b,c ? If you declare int a1,a2,a3 as the students score , the number of the grades is large, representing and manipulating the data by means of unique identifiers will be cumbersome.How to group several data with the same type together ?ArrayThe same typeLimited numbersStore in sequenceOne dimensional Arrays 2 How to declare a array?Type array-name length All array members are the same typeAlso: size- How many array membersint a 10 3 How to access array members ?Name and subscriptFrom 0 to lenth-14 How to store array members in memory?a0a2a4a3a6a7a1a5a8a9memoryStore in sequence from the first element to the last one.a0a2a4a3a6a7a1a5a8a9memory5 How to initialize an array?int a10=1,2,3,4,5,6,7,8,9,0;float x5 = 1.9, 2.0 ;int a = 1, 2, 3, 4;main ( ) int i, int a10=1,2,3,4,5,6,7,8,9,10;for(i=0;i3 nothing change203 nothing change11 nothing change531 nothing change 121 nothing change651 nothing changeak=a0a0=minmain( ) int i , a10 , min ,k;for(i=0;iai ) /* find the smallest one */ min=ai ; k=i ; ak=a0; /* exchange the value */a0=min;for(i=0;i b j + 1 ) t = b j ; b j = b j + 1 ;b j + 1 = t ; Actual argument is array name Formal argument is array name One dimensional Arrays One dimensional Arrays Application : insert a new element in array Give numbers k, n , input k to the nth element in array , an=k 5 4 6 8 9 1 2 3 7int k,n , a10=5,4,6,8,9,1,2,3,7; scanf(“%d,%d”, 50, 4 5 4 6 8 50 9 1 2 3 7a4=50main( ) int k,n, i , a10=5,4,6,8,9,1,2,3,7; scanf(“%d,%d“,for(i=8; i=n; i-)ai+1=ai; /* move elements */an=k; /* insert new value */for(i=0;i=9;i+)printf(“%3d“,ai);One dimensional Arrays Application : delete an element from an array Give numbers n , delete the nth element in array 5 4 6 8 9 1 2 3 7 105 4 6 8 1 2 3 7 10 int k,n , a10=5,4,6,8,9,1,2,3,7,10; scanf(“%d”, 4 main( ) int a10=5,4,6,8,9,1,2,3,7,10;int n,i;scanf(“%d“,for(i=n;i=9;i+)ai=ai+1; /* move elements */for(i=0;i=9;i+)printf(“%3d“,ai);After moving the following element from n, ,the previrous value of an will be changedApplication : sort1 (气泡法)#define N 10main( ) /* 气泡法排序 ,大 小 */ int i,j,m,aN;for(i=0;iN;i+)scanf(“%d”,for(j=1;j=N-1;j+) /* N-1轮处理 */for(i=0;iN-j;i+) /* N-J次比较 */if(aiai+1) /* 顺序不对时交换 */ m=ai; ai=ai+1; ai+1=m;for(i=0;iN;i+)printf(“%5d”,ai); One dimensional Arrays Application : sort2(选择法)#define N 10main( ) /* 选择法排序 大 小 */ int i,j,m,p,aN;for(i=0;iN;i+) scanf(“%d”,for(j=0;jN-1;j+) /* N-1轮处理 */ p=j; /*p记录最大值的下标 */for(i=j+1;iN;i+) /*找第 J轮最大值下标 P*/if( apai ) p=i; m=ap;ap=aj;aj=m; /*ap,aj交换 */for(i=0;iN;i+)printf(“%5d”,ai); One dimensional Arrays Multi-dimensional Arrays How to describe some data like: C provides rectangular multi-dimensional arrays.Question 1 2 3 4 51 1 6 7 81 1 1 9 101 1 1 1 111 1 1 1 1How to define multi-dimensional array? How to store a multi-dimensional array in memory?How to initialize multi-dimensional array How to access members in multi-dimensional arrayMulti-dimensional Arrays Define a multi-dimensional array 1 2 34 5 67 8 9type array-name length1 length2 rows columnsint a33Array elements are stored row by row1 2 3 4 5 6 7 8 9The first row A 2D array is really a 1D array, each of whose elements itself is an array. How to initialize a multi-dimensional array int a23= 1,2,3 , 4,5,6 ; int a23=1,2,3,4,5,6; int a23=1,2,3; int a23=1,2,3; int a23=0,1,2,3;How to access members in multi-dimensional array?For int a 34, elements can be accessed by a i j , i from 0 to 2, j from 0 to 3Suppose 2D array a has m columns , then access elements via : m * row + col + base_address , or the position of aij is : i*m+j+1 when a00 is the first one .Multi-dimensional Arrays Which one is correct: A) int a3 ; B) float a(3,4);C) double a14; D) float a(3)(4);If we have int a34, which is correct when access the elements:A) a 2 4 B) a 1,3 C) a 1+1 0 D) a (2)(1)a00 aiii*m+j+1Multi-dimensional Arrays Autogeneration a matrix without any input.1 2 3 4 51 1 6 7 81 1 1 9 101 1 1 1 111 1 1 1 1main ( ) int i,j,a55;k=2;for(i=0;i5;i+) /* loop in row */for(j=0;j5;j+) /* loop in column */if(j=i) aij=1; else aij=k+; for(i=0;i5;i+) for(j=0;j5;j+)printf(“%4d”,aij);printf(“n”); /* new line */ All values are 1 The value is increment one by one矩阵(乘法)main( ) /* 矩阵乘积 */ static int i,j,k, c22 ;static int a23=1,2,3,4,5,6, b32=2,1,0,4,3,2;for(i=0;i2;i+)for(j=0;j2;j+) cij=0; for(k=0;k=2;k+)cij= cij + aik*bkj; printf(“|%4d %4d|n”,c00,c01); printf(“|%4d %4d|n”,c10,c11);Multi-dimensional Arrays 矩阵(转置)main( ) /* 矩阵 转置 */ int i,j,k;static int a33=1,2,3,4,5,6, 7, 8, 9;for(i=0;i3;i+)for(j=0;ji;j+) /* 矩阵 转置 */ k=aij; aij= aji; aji=k; for(i=0;i3;i+) /*按行输出 */ for(j=0;j3;j+) printf(“%5d”,aij); printf(“n”);Multi-dimensional Arrays Pointers and Addresses int i = 3; float f = 10.0;double d = 1.0; char c =a;we define i. f. d. c as follows:1. Where are the variables stored?2. If we want to access a variable, is there another way except direct operation?C provides an operator and data type to solve above the problem -by pointer A pointer is a variable that contains the address of others A pointer is a group of cells (often two or four) that can hold an address100int k=100a=5 ; b=10 ; c=a+b; printf(“%d,%d,%d ”, a,b,c);main( ) int a , b , c, *pc ;a=5 ; b=10 ; pc=c=a+b; printf(“%d,%d,%d ”, a,b,*pc);A pointer can point to any variables or functions A pointer store others address when it point to it.VariableFunctionArraypointerAdvantages of using pointer:a) faster operation execution of program.b) fewer memory spaces.c) convenient operation, such as assignment, subtraction “-”,+,- and relational operation.Pointers and Addresses PointervariablePointerarrayarrayfunctionAint k ; int *pk; int a10 ; int *pa=a; int fun();int (*pf)()int *f() int *p3PointerPoints to an arrayint ( *p)5Pointers an
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 年产5550吨医用造口袋项目可行性研究报告
- 核能制氢项目可行性研究报告
- 网络技术公司合同付款管理办法
- 公司餐饮员工聘用合同4篇
- 装修合同一页简单8篇
- 工厂生产线职工聘用合同书5篇
- 数字化转型中的文化资本积累-洞察及研究
- 阀门自组网通信技术-洞察及研究
- 部门负责人安全培训课程课件
- 部门安全知识培训总结课件
- 2025年辽宁省交通建设投资集团招聘(104人)备考练习试题及答案解析
- 七年级上册数学《相交线与平行线》100题练习(含答案)
- 西藏文化考试题目及答案
- 入党培训考试试题2025及答案
- (9月10日)师者如光虽微致远-2025年教师节主题班会课件-2025-2026学年高中主题班会课件
- 公章免责协议合同书模板
- 2025广东海珠区应急管理局招聘安全生产监督检查员18人笔试备考试题及答案解析
- 计算机维护合同补充协议
- 2025秋外研新版三起点小学英语四年级上册教学计划
- 2025-2026学年人教版(2024)初中数学八年级上册教学计划及进度表
- 2025秋部编版二年级上册语文教学计划+教学进度表
评论
0/150
提交评论