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

上海交通大学交大密西根 ·联合学院一 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
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_20 Looking Ahead.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_19 Recursion 1.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_16 MATLAB environment short.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_15 Introduction to matlab.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》教学资源_Chapter 1 Introduction.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》教学资源_第二章习题与答案(第三版).doc
- 上海交通大学:《数据库系统原理 The principle of Database System》教学资源_第三章习题与答案(第三版).doc
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)chapter8 Views, Indexes.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)Chapter7 Constraints and Triggers.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)Chapter6 The database Language SQL –as a tutorial.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)Chapter5 Algebraic and Logic Query languages.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)chapter4 High-level Database Models.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)chapter3 Design Theory for Relational Databases.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)chapter11 The semi-structured data model Structured data.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)Chapter1 Introduction.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》教学资源_intro.pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第二章 8086系统结构.pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第一章 绪论(毛义梅).pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第四章 汇编语言程序设计_习题及解答.pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第四章 汇编语言程序设计.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_examples on class design.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Expressions and Statements.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_files_DataBase Design.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Function.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Introduction to Computer and Programming.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Introduction to Vg101.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_objects and classes.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_programming style guide for C plusplus.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Random Number_Graphics.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Start with C plusplus.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_vector_string.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation 1.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_recitation 13.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation IX.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation V.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation VII.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation VIII.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation X.ppt
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第十四章 MCS-51单片机(1/2).pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第十四章 MCS-51单片机(2/2).pdf