Chapter 8: Arrays Gator Engineering One-dimensional array Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Move the first element to the.

Slides:



Advertisements
Similar presentations
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.9Arrays of Pointers Arrays can contain pointers For.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Lesson 6 - Pointers Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using the const.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer Variable Declarations and Initialization 7.3Pointer.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 10 Program Organization (continued)
 2006 Pearson Education, Inc. All rights reserved Arrays.
A First Book of ANSI C Fourth Edition
Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
Computer programming Lecture 5. Lecture 5: Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character arrays.
Chapter 8 Arrays and Strings
Chapter 8: Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions (continued)
Working with Arrays in MATLAB
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Chapter 8: Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
COP 3275: Chapter 08 Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA.
1. 1. Introduction to Array 2. Arrays of Data 3. Array Declaration 4. Array Initialization 5. Operations on Array 6. Multidimensional Arrays 7. Index.
1 Lecture 12 Pointers and Strings Section 5.4, ,
Computer programming Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
Gator Engineering One-dimensional array Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Sorting –Bubble sort –Merge sort –Quick sort –Selection.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
1 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Arrays Chap 8. 2 Without Array If you want to create Lottery winning numbers… –You need 7 variables (6 for winning numbers and 1 for the special number)
8. ARRAYS. Aggregate variables Scalar variables –Single value Aggregate variables –Collection of values –Arrays: elements have the same type.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Arrays. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.
Chapter 1 Computing Tools Variables, Scalars, and Arrays Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
Arrays in C. What is Array? The variables we have used so far can store a single value. Array is a new type of variable capable of storing many values.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
EGR 2261 Unit 10 Two-dimensional Arrays
Chapter 7 - Pointers Outline 7.1 Introduction
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 7 - Pointers Outline 7.1 Introduction
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
7 Arrays.
Introduction to Programming
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Strings Chapter 13 Copyright © 2008 W. W. Norton & Company.
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
7 Arrays.
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Presentation transcript:

Chapter 8: Arrays Gator Engineering One-dimensional array Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Move the first element to the last position and shift one position to the left for the rest of the elements Hello! Hello!

Chapter 8: Arrays Gator Engineering One-dimensional array Copyright © 2008 W. W. Norton & Company. All rights reserved. 2 Sorting –Bubble sort –Merge sort –Quick sort –Selection sort –Heapsort –Etc.

Chapter 8: Arrays Gator Engineering Multidimensional Arrays An array may have any number of dimensions. The following declaration creates a two-dimensional array (a matrix, in mathematical terminology): int m[5][9]; m has 5 rows and 9 columns. Both rows and columns are indexed from 0: Copyright © 2008 W. W. Norton & Company. All rights reserved. 3

Chapter 8: Arrays Gator Engineering Multidimensional Arrays To access the element of m in row i, column j, we must write m[i][j]. The expression m[i] designates row i of m, and m[i][j] then selects element j in this row. Resist the temptation to write m[i,j] instead of m[i][j]. C treats the comma as an operator in this context, so m[i,j] is the same as m[j]. Copyright © 2008 W. W. Norton & Company. All rights reserved. 4

Chapter 8: Arrays Gator Engineering Multidimensional Arrays Although we visualize two-dimensional arrays as tables, that’s not the way they’re actually stored in computer memory. C stores arrays in row-major order, with row 0 first, then row 1, and so forth. How the m array is stored: Copyright © 2008 W. W. Norton & Company. All rights reserved. 5

Chapter 8: Arrays Gator Engineering Multidimensional Arrays Nested for loops are ideal for processing multidimensional arrays. Consider the problem of initializing an array for use as an identity matrix. A pair of nested for loops is perfect: #define N 10 double ident[N][N]; int row, col; for (row = 0; row < N; row++) for (col = 0; col < N; col++) if (row == col) ident[row][col] = 1.0; else ident[row][col] = 0.0; Copyright © 2008 W. W. Norton & Company. All rights reserved. 6

Chapter 8: Arrays Gator Engineering Initializing a Multidimensional Array We can create an initializer for a two-dimensional array by nesting one-dimensional initializers: int m[5][9] = {{1, 1, 1, 1, 1, 0, 1, 1, 1}, {0, 1, 0, 1, 0, 1, 0, 1, 0}, {0, 1, 0, 1, 1, 0, 0, 1, 0}, {1, 1, 0, 1, 0, 0, 0, 1, 0}, {1, 1, 0, 1, 0, 0, 1, 1, 1}}; Initializers for higher-dimensional arrays are constructed in a similar fashion. C provides a variety of ways to abbreviate initializers for multidimensional arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 7

Chapter 8: Arrays Gator Engineering Initializing a Multidimensional Array If an initializer isn’t large enough to fill a multidimensional array, the remaining elements are given the value 0. The following initializer fills only the first three rows of m ; the last two rows will contain zeros: int m[5][9] = {{1, 1, 1, 1, 1, 0, 1, 1, 1}, {0, 1, 0, 1, 0, 1, 0, 1, 0}, {0, 1, 0, 1, 1, 0, 0, 1, 0}}; Copyright © 2008 W. W. Norton & Company. All rights reserved. 8

Chapter 8: Arrays Gator Engineering Initializing a Multidimensional Array If an inner list isn’t long enough to fill a row, the remaining elements in the row are initialized to 0: int m[5][9] = {{1, 1, 1, 1, 1, 0, 1, 1, 1}, {0, 1, 0, 1, 0, 1, 0, 1}, {0, 1, 0, 1, 1, 0, 0, 1}, {1, 1, 0, 1, 0, 0, 0, 1}, {1, 1, 0, 1, 0, 0, 1, 1, 1}}; Copyright © 2008 W. W. Norton & Company. All rights reserved. 9

Chapter 8: Arrays Gator Engineering Initializing a Multidimensional Array We can even omit the inner braces: int m[5][9] = {1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1}; Once the compiler has seen enough values to fill one row, it begins filling the next. Omitting the inner braces can be risky, since an extra element (or even worse, a missing element) will affect the rest of the initializer. Copyright © 2008 W. W. Norton & Company. All rights reserved. 10

Chapter 8: Arrays Gator Engineering Constant Arrays An array can be made “constant” by starting its declaration with the word const : const char hex_chars[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; An array that’s been declared const should not be modified by the program. Copyright © 2008 W. W. Norton & Company. All rights reserved. 11

Chapter 8: Arrays Gator Engineering Constant Arrays Advantages of declaring an array to be const : –Documents that the program won’t change the array. –Helps the compiler catch errors. const isn’t limited to arrays, but it’s particularly useful in array declarations. Copyright © 2008 W. W. Norton & Company. All rights reserved. 12

Chapter 8: Arrays Gator Engineering Program: Dealing a Hand of Cards The deal.c program illustrates both two- dimensional arrays and constant arrays. The program deals a random hand from a standard deck of playing cards. Each card in a standard deck has a suit (clubs, diamonds, hearts, or spades) and a rank (two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, or ace). Copyright © 2008 W. W. Norton & Company. All rights reserved. 13

Chapter 8: Arrays Gator Engineering Program: Dealing a Hand of Cards The user will specify how many cards should be in the hand: Enter number of cards in hand: 5 Your hand: 7c 2s 5d as 2h Problems to be solved: –How do we pick cards randomly from the deck? –How do we avoid picking the same card twice? Copyright © 2008 W. W. Norton & Company. All rights reserved. 14

Chapter 8: Arrays Gator Engineering Program: Dealing a Hand of Cards To pick cards randomly, we’ll use several C library functions: –time (from ) – returns the current time, encoded in a single number. –srand (from ) – initializes C’s random number generator. –rand (from ) – produces an apparently random number each time it’s called. By using the % operator, we can scale the return value from rand so that it falls between 0 and 3 (for suits) or between 0 and 12 (for ranks). Copyright © 2008 W. W. Norton & Company. All rights reserved. 15

Chapter 8: Arrays Gator Engineering Program: Dealing a Hand of Cards The in_hand array is used to keep track of which cards have already been chosen. The array has 4 rows and 13 columns; each element corresponds to one of the 52 cards in the deck. All elements of the array will be false to start with. Each time we pick a card at random, we’ll check whether the element of in_hand corresponding to that card is true or false. –If it’s true, we’ll have to pick another card. –If it’s false, we’ll store true in that element to remind us later that this card has already been picked. Copyright © 2008 W. W. Norton & Company. All rights reserved. 16

Chapter 8: Arrays Gator Engineering Program: Dealing a Hand of Cards Once we’ve verified that a card is “new,” we’ll need to translate its numerical rank and suit into characters and then display the card. To translate the rank and suit to character form, we’ll set up two arrays of characters—one for the rank and one for the suit—and then use the numbers to subscript the arrays. These arrays won’t change during program execution, so they are declared to be const. Copyright © 2008 W. W. Norton & Company. All rights reserved. 17

Chapter 8: Arrays Gator Engineering deal.c /* Deals a random hand of cards */ #include /* C99 only */ #include #define NUM_SUITS 4 #define NUM_RANKS 13 int main(void) { bool in_hand[NUM_SUITS][NUM_RANKS] = {false}; int num_cards, rank, suit; const char rank_code[] = {'2','3','4','5','6','7','8', '9','t','j','q','k','a'}; const char suit_code[] = {'c','d','h','s'}; Copyright © 2008 W. W. Norton & Company. All rights reserved. 18

Chapter 8: Arrays Gator Engineering srand((unsigned) time(NULL)); printf("Enter number of cards in hand: "); scanf("%d", &num_cards); printf("Your hand:"); while (num_cards > 0) { suit = rand() % NUM_SUITS; /* picks a random suit */ rank = rand() % NUM_RANKS; /* picks a random rank */ if (!in_hand[suit][rank]) { in_hand[suit][rank] = true; num_cards--; printf(" %c%c", rank_code[rank], suit_code[suit]); } printf("\n"); return 0; } Copyright © 2008 W. W. Norton & Company. All rights reserved. 19