当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Array and its Applications

资源类别:文库,文档格式:PDF,文档页数:67,文件大小:638.18KB,团购合买
点击下载完整版文档(PDF)

上海交通大学交大密西根 ·联合学院一 181 UM-SJTU Joint Institute ■ University of Michigan Shanghal Jiao Tong University Vg101:Introduction to Computer and Programming Array and Array Operation

Array and Array Operation Vg101 :Introduction to Introduction to Computer and Programming Computer and Programming

上海交通大学交大密西根 ·联合学院一 : 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Introduction Today we will start on a major new topic in C++: arrays. Arrays An array is an ordered collection of data values of the same type. Ordered: We can refer to the elements by their position Ist,2nd,... Same type: All ints,all doubles,all strings

Introduction Introduction • Today we will start on a major new topic in C++: arrays

上海交通大学交大密西根 联合学院·一 ◆ 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Array Definition To declare an array,we specify that type and name,just like any variable,and also the size,which means how many elements the array will have.A square-bracket notation is used to specify the size of an array. Here is how Arrays arrays look in Arrays must be declared,and as with any variable,declaring a stack frame an array sets aside the memory for it. diagram. main main() letters char letters[4]; □ int values[3]; values

Array Definition Array Definition • To declare an array, we specify that type and name, just like any variable, and also the size, which means how many elements the array will have. A square-bracket notation is used to specify the size of an array. • Here is how arrays look in a stack frame diagram

上海交通大学交大密西根 ·联合学院一 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Array in Memory Space Here is how they look if we show memory as a linear sequence of addresses.The letters array would occupy four bytes of memory;values would occupy 12 since an int requires four bytes. Arrays Arrays must be declared,and as with any variable,declaring an array sets aside the memory for it. 100 letters main() char letters[4]; 104 values int values[3]; }

Array in Memory Space Array in Memory Space • Here is how they look if we show memory as a linear sequence of addresses. The letters array would occupy four bytes of memory; values would occupy 12 since an int requires four bytes

上海交通大学交大密西根 ·联合学院一 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Element Numbering As with most things in C++,array elements are numbered from zero. Some other languages let the programmer decide what the first index will be,but it is always 0 in C++. Arrays Array elements are numbered from 0. main main() f letters char letters[4]; D int values[3]; 0123 。”。 values 0

Element Numbering Element Numbering • As with most things in C++, array elements are numbered from zero. • Some other languages let the programmer decide what the first index will be, but it is always 0 in C++

上海交通大学交大密西根 B ·联合学院一 1811 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Array Element Accessing Besides being used in declaration,square brackets are used to refer to particular members of arrays in assignment statements or in expressions. Arrays Array elements are referenced in expressions of the form array-name[index] main ( main char letters[4]; letters int values[3]; A 25 int i; 0123 letters[0]A'; values va1ues[1]=23; 23 0 1 i values[1]2;

Array Element Accessing Array Element Accessing • Besides being used in declaration, square brackets are used to refer to particular members of arrays in assignment statements or in expressions

上海交通大学交大密西根 ·联合学院一 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Array Element Accessing Since an array element is something that can go on the left side of an assignment,we can use operators like++ with an array element. value[1]++; Arrays is just shorthand for Array elements are referenced in expressions of the form value[1]+1; array-name[index] main ( main char letters[4]; int values[3]; letters int i; a 25 0123 letters[0]=A'; values va1ues[1]=23; 24 i=values[1]+2; 0 1 values [1]++;

Array Element Accessing Array Element Accessing • Since an array element is something that can go on the left side of an assignment, we can use operators like ++ with an array element. value[1]++; is just shorthand for value[1] += 1;

上海交通大学交大密西根 联合学院·一 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Array Indexing We can use an integer variable or expression as an array index.This is a very typical for loop. Arrays Array elements are referenced in expressions of the form array-name[index] main ( main char letters[4]; letters 1 int values[3]; 3 int i; 0123 for (i=0;i<3; values i++) 0 0 0 values[i]0; 0 2

Array Indexing Array Indexing • We can use an integer variable or expression as an array index. This is a very typical for loop

上海交通大学交大密西根 联合学院·一 ◆ 81 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Array Size Definition Using a #defined constant as the array size is often convenient in declarations.It is not possible in C to use a variable as the size of an array. #define MAX EXAMS 500 main() int i; int scores [MAX EXAMS]; What goes here must be a constant,because as it compiles your program,the compiler needs to know how much memory to set aside

Array Size Definition Array Size Definition • Using a #defined constant as the array size is often convenient in declarations. It is not possible in C to use a variable as the size of an array

上海交通大学交大密西根 ·联合学院一 81 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Indexing Problem Any integer variable i can be used as an array index.C comnilars do not check to see thet the indey valis V #define MAX EXAMS 500 main() [ W int i; int scores [MAX EXAMS]; al Square brackets and an index are ti scores[1]99; i=·· used to set or access the value of an scores[i]100; array element. if (scores[i]high) It's up to you to be sure the index high scores[i];is valid

Indexing Problem Indexing Problem • Any integer variable i can be used as an array index. C compilers do not check to see that the index value is valid for the given array. If i here was 523, the statement scores[i] = 100; would store 100 somewhere, but it wouldn’t be in the array! Doubtless an error would occur, but maybe not till later

点击下载完整版文档(PDF)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共67页,可试读20页,点击继续阅读 ↓↓
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有