1 Chapter Eleven Arrays. 2 A Motivating Example main( ) { int n0, n1, n2, n3, n4; scanf(“%d”, &n0); scanf(“%d”, &n1); scanf(“%d”, &n2); scanf(“%d”, &n3);

Slides:



Advertisements
Similar presentations
Topic Reviews For Unit ET156 – Introduction to C Programming Topic Reviews For Unit
Advertisements

You have been given a mission and a code. Use the code to complete the mission and you will save the world from obliteration…
Advanced Piloting Cruise Plot.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 4 Parameters and Overloading. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Learning Objectives Parameters Call-by-value Call-by-reference.
Chapter 16 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
Chapter 1 The Study of Body Function Image PowerPoint
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Business Transaction Management Software for Application Coordination 1 Business Processes and Coordination.
1 C++Tutorial Rob Jagnow This tutorial will be best for students who have at least had some exposure to Java or another comparable programming language.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
My Alphabet Book abcdefghijklm nopqrstuvwxyz.
0 - 0.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Addition Facts
Year 6 mental test 5 second questions
Introduction to Programming
Around the World AdditionSubtraction MultiplicationDivision AdditionSubtraction MultiplicationDivision.
ZMQS ZMQS
Chapter 7: Arrays In this chapter, you will learn about
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.
Data Structures ADT List
LIST PROCESSING.
1 Joe Meehean. Ordered collection of items Not necessarily sorted 0-index (first item is item 0) Abstraction 2 Item 0 Item 1 Item 2 … Item N.
ABC Technology Project
1 / / / /. 2 (Object) (Object) –, 10 (Class) (Class) –, –, – (Variables) [ Data member Field Attribute](, ) – (Function) [ Member function Method Operation.
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
VOORBLAD.
Review Pseudo Code Basic elements of Pseudo code
Squares and Square Root WALK. Solve each problem REVIEW:
1 Advanced C Programming from Expert C Programming: Deep C Secrets by Peter van der Linden CIS*2450 Advanced Programming Concepts.
© 2012 National Heart Foundation of Australia. Slide 2.
Backup Slides. An Example of Hash Function Implementation struct MyStruct { string str; string item; };
Mark Dixon, School of Computing SOFT 120Page 1 5. Passing Parameters by Reference.
Chapter 5 Test Review Sections 5-1 through 5-4.
GG Consulting, LLC I-SUITE. Source: TEA SHARS Frequently asked questions 2.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 14 - Advanced C Topics Outline 14.1Introduction 14.2Redirecting Input/Output on UNIX and DOS Systems.
C Language.
Addition 1’s to 20.
25 seconds left…...
Slippery Slope
Test B, 100 Subtraction Facts
Week 1.
Complexity Analysis (Part II)
We will resume in: 25 Minutes.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Pointers and Arrays Chapter 12
A SMALL TRUTH TO MAKE LIFE 100%
1 Unit 1 Kinematics Chapter 1 Day
PSSA Preparation.
How Cells Obtain Energy from Food
Chapter 30 Induction and Inductance In this chapter we will study the following topics: -Faraday’s law of induction -Lenz’s rule -Electric field induced.
User Defined Functions Lesson 1 CS1313 Fall User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough #1.
Data Structures Using C++ 2E
Pointers. 2 A pointer is a variable that points to or references a memory location in which data is stored. Each memory cell in the computer has an address.
Senem KUMOVA METİN CS FALL 1 POINTERS && ARRAYS CHAPTER 6.
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Arrays and Strings.
By Senem Kumova Metin 1 DATA TYPES. by Senem Kumova Metin 2 DATA TYPE? …… x; // DECLARATION OF VARIABLE X printf(“Do you want to go on? \n”) printf(“Please.
CSCI 161 Lecture 14 Martin van Bommel. New Structure Recall “average.cpp” program –Read in a list of numbers –Count them and sum them up –Calculate the.
Presentation transcript:

1 Chapter Eleven Arrays

2 A Motivating Example main( ) { int n0, n1, n2, n3, n4; scanf(“%d”, &n0); scanf(“%d”, &n1); scanf(“%d”, &n2); scanf(“%d”, &n3); scanf(“%d”, &n4); printf(“n%d = %d\n”, n4); printf(“n%d = %d\n”, n3); printf(“n%d = %d\n”, n2); printf(“n%d = %d\n”, n1); printf(“n%d = %d\n”, n0); } Input 5 integers and print them in reverse order

3 Arrays An array is a consecutive group of memory locations with two characteristics An array is homogeneous: all memory locations in the array store data of the same type An array is ordered: memory locations in the array are named in ordered integer index beginning at zero

4 Examples

5 Array Declaration & Access Arrays are declared as element-type array-name [ array-size ]; int intArray[6]; float floatArray[6]; Array elements are accessed as intArray[0] = 0; floatArray[1] = floatArray[2] + 2.3;

6 Examples intArray floatArray intArray intArray[0] = 0; floatArray floatArray[1] = floatArray[2] + 2.3;

7 An Example #define SIZE 5 main( ) { int i, n[SIZE]; for ( i = 0; i < SIZE; i++ ) { scanf(“%d”, &n[i]); } for ( i = SIZE - 1; i >= 0; i-- ) { printf(“n[%d] = %d\n”, i, n[i]); }

8 An Example #define SIZE 5 main( ) { int i, sum, n[SIZE]; sum = 0; for ( i = 0; i < SIZE; i++ ) scanf(“%d”, &n[i]); for ( i = 0; i < SIZE; i++ ) sum += n[i]; printf(“sum = %d\n”, sum); }

9 Address of Variables In memory, every byte is identified by an address Data values requiring multiple bytes are identified by the address of the first byte intfloat

10 Address of Array Elements int iArray[5];number of bytes = 4 * 5 = 20 iArray[0]iArray[4]iArray[1]iArray[2]iArray[3] address of iArray[i] = * i base addressoffset

11 Common Pitfalls Whenever you use arrays in your programs, make sure that the index values used to select elements from the array remain within the array bounds On most computers, referencing elements that are outside the array bounds is not detected as an error but will certainly lead to unpredictable results

12 Passing Arrays as Parameters #define SIZE 5 main( ) { int n[SIZE]; inputArray(n); /* use 0 as sentinel value */ reverseArray(n); printArray(n); }

13 Two Issues The required size of the array n is unknown The array n passed to the two functions inputArray and reverseArray should be changed by these two functions

14 Generalizing the Size of Arrays The usual strategy is to declare an array that is larger than you need and use only part of it The number of elements declared is called the allocated size of the array The number of elements actually in use is called the effective size of the array

15 Generalizing the Size of Arrays int n[MAXSIZE]; void printArray(int n[MAXSIZE], int size); void printArray(int n[], int size); void reverseArray(int n[], int size); int inputArray(int n[], int maxsize); int inputArray(int n[], int maxsize, int sentinel);

16 Generalizing the Size of Arrays #define MAX 100 main( ) { int n[MAX], size; size = inputArray(n, MAX, 0); reverseArray(n, size); printArray(n, size); }

17 Passing Array Arguments When an array is passed to a function, instead of copying the entire array to the function, only the base address of the array is passed to the function The array parameter is thus a synonym of the array argument. Changing the elements of the array parameter is the same as changing the elements of the array arguments

18 printArray static void printArray(int array[], int size) { int i; for (i = 0; i < size; i++) { printf(“%d\n”, array[i]); }

19 inputArray static int inputArray(int array[], int max, int sentinel){ int n, value; n = 0; while (TRUE) { printf(“?”); scanf(“%d”, &value); if (value == sentinel) break; if (n == max) {printf(“Error: array full”); exit(1); } array[n++] = value; } return n; }

20 reverseArray static void reverseArray(int array[], int size) { int i; for (i = 0; i < size / 2; i++) { /* swap(array[i], array[size – i –1]); */ swap(array, i, size – i –1); }

21 swap static void swap(int array[], int p1, int p2) { int tmp; tmp = array[p1]; array[p1] = array[p2]; array[p2] = tmp; }

22 An Example > Peter Piker picked a peck Of pickled peppers. A 1 C 3 D 2 E 8 F 1 I 3 …

23 An Example int nA, nB, nC, …, nZ; int letterCounts[26]; int letterIndex(char ch) { if (isalpha(ch)) { return toupper(ch) – ‘A’; } else { return –1; }

24 An Example void recordLetter(char ch, int letterCounts[]) { int index; index = letterIndex(ch); if (index != -1) letterCounts[index]++; }

25 An Example void clearIntArray(int array[], int n) { int i; for (i = 0; i < n; i++) { array[i] = 0; }

26 An Example void displayLetterCounts(int letterCounts[]) { char ch; int num; for (ch = ‘A’; ch <= ‘Z’; ch++) { num = letterCounts[letterIndex(ch)]; if (num != 0) printf(“%c %4d\n”, ch, num); }

27 Static Initialization of Arrays int digits[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int digits[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; string bigCities[] = { “New York”, “Los Angeles”, “Chicago”, “Houston”, “Philadelphia”, “San Diego”, “Detroit”, “Dallas”, } int nBigCities = sizeof bigCities / sizeof bigCities[0];

28 Scalar-Type Array Index string booleanName[2] = {“FALSE”, “TRUE”}; typedef enum {FALSE, TRUE} bool; printf(“flag = %s\n”, booleanName[flag]);

29 Multidimensional Arrays Arrays of arrays are called multidimensional arrays char board[3][3]; board[0][0] board[0][1] board[0][2] board[1][0] board[1][1] board[1][2] board[2][0] board[2][1] board[2][2]

30 Multidimensional Arrays board[0][0] board[0][1] board[0][2] board[1][0] board[1][1] board[1][2] board[2][0] board[2][1] board[2][2] board[0] board[1] board[2]

31 Passing Multidimensional Arrays void displayBorad(char board[3][3]) { int row, column; for (row = 0; row < 3; row++) { if (row != 0) printf(“ \n”); for (column != 0; column < 3; column++) { if (column != 0) printf(“|”); printf(“ %c “, borad[row][column]); } printf(“\n”); }

32 Initializing Multidimensional Arrays double identityMatrix[3][3] = { {1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0} };