《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 5 Arrays

Chapter 5 ABSOLUTE C++ Arrays WALTER SAVITCH SECOND EDITION PEARSON Copyright2006 Pearson Addison-Wesley All rights reserved
Chapter 5 Arrays

Learning Objectives Introduction to Arrays Declaring and referencing arrays ◆For-loops and arrays ◆Arrays in memory ◆Arrays in Functions Arrays as function arguments,return values Programming with Arrays Partially Filled Arrays,searching,sorting Multidimensional Arrays Copyright 2006 Pearson Addison-Wesley.All rights reserved. 5-2
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives ¨ Introduction to Arrays ¨ Declaring and referencing arrays ¨ For-loops and arrays ¨ Arrays in memory ¨ Arrays in Functions ¨ Arrays as function arguments, return values ¨ Programming with Arrays ¨ Partially Filled Arrays, searching, sorting ¨ Multidimensional Arrays

Introduction to Arrays ◆Array definition: A collection of data of same type First "aggregate"data type ◆Means"grouping" int,float,double,char are simple data types Used for lists of like items Test scores,temperatures,names,etc. Avoids declaring multiple simple variables Can manipulate "list"as one entity Copyright006 Pearson Addison-Wesley.All rights reserved. 5-3
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-3 Introduction to Arrays ¨ Array definition: ¨ A collection of data of same type ¨ First "aggregate" data type ¨ Means "grouping" ¨ int, float, double, char are simple data types ¨ Used for lists of like items ¨ Test scores, temperatures, names, etc. ¨ Avoids declaring multiple simple variables ¨ Can manipulate "list" as one entity

Declaring Arrays ◆ Declare the array allocates memory int score[5]; Declares array of 5 integers named "score" Similar to declaring five variables: int score[0],score[1],score[2],score[3],score[4] Individual parts called many things: Indexed or subscripted variables "Elements"of the array Value in brackets called index or subscript Numbered from 0 to size-1 Copyright 2006 Pearson Addison-Wesley.All rights reserved. 5-4
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-4 Declaring Arrays ¨ Declare the array allocates memory int score[5]; ¨ Declares array of 5 integers named "score" ¨ Similar to declaring five variables: int score[0], score[1], score[2], score[3], score[4] ¨ Individual parts called many things: ¨ Indexed or subscripted variables ¨ "Elements" of the array ¨ Value in brackets called index or subscript ¨ Numbered from 0 to size - 1

Accessing Arrays Access using index/subscript ◆cout<<score[3]; Note two uses of brackets: In declaration,specifies SIZE of array Anywhere else,specifies a subscript Size,subscript need not be literal int score[MAX SCORES]; ◆score[n+1]=99; If n is 2,identical to:score[3] Copyright006 Pearson Addison-Wesley.All rights reserved. 5-5
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-5 Accessing Arrays ¨ Access using index/subscript ¨ cout << score[3]; ¨ Note two uses of brackets: ¨ In declaration, specifies SIZE of array ¨ Anywhere else, specifies a subscript ¨ Size, subscript need not be literal ¨ int score[MAX_SCORES]; ¨ score[n+1] = 99; ¨ If n is 2, identical to: score[3]

Array Usage Powerful storage mechanism Can issue command like: "Do this to ith indexed variable" where i is computed by program "Display all elements of array score" "Fill elements of array score from user input" "Find highest value in array score" "Find lowest value in array score" Copyright 2006 Pearson Addison-Wesley.All rights reserved. 5-6
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-6 Array Usage ¨ Powerful storage mechanism ¨ Can issue command like: ¨ "Do this to i th indexed variable" where i is computed by program ¨ "Display all elements of array score" ¨ "Fill elements of array score from user input" ¨ "Find highest value in array score" ¨ "Find lowest value in array score

Array Program Example: Display 5.1 Program Using an Array(1 of 2) Display 5.1 Program Using an Array 1 //Reads in five scores and shows how much each 2 //score differs from the highest score. 3 #include 4 using namespace std; int main() 6 7 int i,score[5],max; cout score[0]; max =score[0]; 11 for (i =1;iscore[i]; 14 if (score[i]max) 15 max score[i]; 16 //max is the largest of the values score[0],.,score[i]. 17 Copyright006 Pearson Addison-Wesley.All rights reserved. 5-7
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-7 Array Program Example: Display 5.1 Program Using an Array (1 of 2)

Array Program Example: Display 5.1 Program Using an Array(2 of 2) 1 cout <"The highest score is "<max <endl 19 <"The scores and their\n" <<"differences from the highest are:\n"; 21 for(i=0;i<5;i+) 2 cout <score[i]<<off by 23 <<(max-score[i])<<endl; 24 return 0; 25 SAMPLE DIALOGUE Enter 5 scores: 592106 The highest score is 10 The scores and their differences from the highest are: 5 off by 5 9 off by 1 2 off by 8 10 off by 0 6 off by 4 Copyright 2006 Pearson Addison-Wesley.All rights reserved. 5-8
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-8 Array Program Example: Display 5.1 Program Using an Array (2 of 2)

for-loops with Arrays Natural counting loop Naturally works well "counting thru"elements of an array ◆Example: for (idx 0;idx<5;idx++) { cout <score[idx]<"off by <max-score[idx]<endl; } Loop control variable (idx)counts from 0-5 Copyright 2006 Pearson Addison-Wesley.All rights reserved. 5-9
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-9 for-loops with Arrays ¨ Natural counting loop ¨ Naturally works well "counting thru" elements of an array ¨ Example: for (idx = 0; idx<5; idx++) { cout << score[idx] << "off by " << max – score[idx] << endl; } ¨ Loop control variable (idx) counts from 0 – 5

Major Array Pitfall ◆Array indexes always start with zero! Zero is "first"number to computer scientists C++will "let"you go beyond range Unpredictable results ◆Compiler will not detect these errors! Up to programmer to "stay in range" Copyright 2006 Pearson Addison-Wesley.All rights reserved. 5-10
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-10 Major Array Pitfall ¨ Array indexes always start with zero! ¨ Zero is "first" number to computer scientists ¨ C++ will "let" you go beyond range ¨Unpredictable results ¨Compiler will not detect these errors! ¨ Up to programmer to "stay in range
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 8 Operator Overloading, Friends, and References.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 7 Constructors and Other Tools.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 6 Structures and Classes.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 2 Flow of Control.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 3 Function Basics.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 1 C++ Basics.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 4 Parameters and Overloading.ppt
- 西安邮电大学:《信息论与编码》课程教学课件(PPT讲稿)第三章.ppt
- 西安邮电大学:《信息论与编码》课程教学课件(PPT讲稿)第二章 信源与信息熵.ppt
- 西安邮电大学:《信息论与编码》课程教学课件(PPT讲稿)第一章 绪论(主讲:王军选).ppt
- 《信息论与编码》课程教学资源(作业习题)第三章 信道与信道容量(含解答).pdf
- 《信息论与编码》课程教学资源(作业习题)自测题无答案.pdf
- 《信息论与编码》课程教学实验指导书.pdf
- 《信息论与编码》课程教学大纲 Element of Information Theory and Coding B.pdf
- 《信息论与编码》课程教学大纲 Element of Information Theory and Coding A.pdf
- 《信息安全概论》课程教学资源(PPT课件)第9章 信息安全标准与法律法规.ppt
- 《信息安全概论》课程教学资源(PPT课件)第2章 信息保密技术.ppt
- 《信息安全概论》课程教学资源(PPT课件)第7章 网络安全技术.ppt
- 《信息安全概论》课程教学资源(PPT课件)第8章 信息安全管理.ppt
- 《信息安全概论》课程教学资源(PPT课件)第6章 访问控制技术.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 11 Separate Compilation and Namespaces.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 12 Streams and File IO.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 10 Pointers and Dynamic Arrays.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 9 Strings.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 13 Inheritance.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 14 Polymorphism and Virtual Functions.ppt
- 《微机技术及应用》课程教学大纲 Microcmputer Technology and aplications.doc
- 《微型计算机技术及应用》课程电子教案(PPT教学课件,共十五章,完整版).pptx
- 《计算机导论》课程教学大纲 Computer Concepts.pdf
- 《计算机导论》课程教学课件(英文讲稿)1-a-Computer History+ Di Devices.pdf
- 《计算机导论》课程教学课件(英文讲稿)1-b-Digital Data Representation.pdf
- 《计算机导论》课程教学课件(英文讲稿)2-a-Computer Hardware.pdf
- 《计算机导论》课程教学课件(英文讲稿)2-b-Computer Hardware.pdf
- 《计算机导论》课程教学课件(英文讲稿)3-a-b-Computer Software.pdf
- 《计算机导论》课程教学课件(英文讲稿)4- operating system.pdf
- 《计算机导论》课程教学课件(英文讲稿)4-a- File mangement.pdf
- 《计算机导论》课程教学课件(英文讲稿)5-a- LANS_WANS.pdf
- 《计算机导论》课程教学课件(英文讲稿)5-b- LANS_WANS.pdf
- 《计算机导论》课程教学课件(英文讲稿)6-a- The Internet.pdf
- 《计算机导论》课程教学课件(英文讲稿)6-b- The Internet.pdf