Arrays 1 2 3 4 5 Arrays are data structures that allow us to store data of the same type in contiguous memory locations. That was a pretty dense statement!

Slides:



Advertisements
Similar presentations
Chapter 7: Arrays In this chapter, you will learn about
Advertisements

Basic Algorithms on Arrays. Learning Objectives Arrays are useful for storing data in a linear structure We learn how to process data stored in an array.
Multidimensional Arrays Arrays with more than one dimension are called multidimensional arrays. Human cannot easily visualize more than three dimension.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Processing Arrays Lesson 8 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
ARRAY Prepared by MMD, Edited by MSY1.  Introduction to arrays  Declaring arrays  Initializing arrays  Examples using arrays  Relationship with pointers.
Computer Science 210 Computer Organization Arrays.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
Computer programming Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character.
Processing Arrays Lesson 9 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
CHAPTER 7: Arrays CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (Feb 2012)
Prepared by MMD, Edited by MSY1 CHAPTER 4 ARRAY. Prepared by MMD, Edited by MSY2 Arrays  Introduction to arrays  Declaring arrays  Initializing arrays.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
CS 201 Tarik Booker California State University, Los Angeles.
Strings C supports strings using one-dimensional character arrays. A string is defined as a null-terminated character array. In C, a null is 0. You must.
Arrays.
Array in C# Array in C# RIHS Arshad Khan
Computer Science 210 Computer Organization
Two-Dimensional Arrays
Chapter 8 – Arrays and Array Lists
Computer Programming BCT 1113
multi-dimensional arrays
Two Dimensional Array Mr. Jacobs.
© 2016 Pearson Education, Ltd. All rights reserved.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Lecture-5 Arrays.
Array 9/8/2018.
Chapter 8 Arrays Objectives
Module 2 Arrays and strings – example programs.
C-Programming, continued
Computer Science 210 Computer Organization
Arrays … The Sequel Applications and Extensions
Loops The loop blocks you've used in Scratch can all be recreated in C! Loops allow for the repetition of code until a condition is met. There are three.
Arrays, For loop While loop Do while loop
Arrays An Array is an ordered collection of variables
Nested Loop Review and Two-Dimensional Arrays
Lecture 10 Arrays.
Chapter 8 Arrays Objectives
EKT150 : Computer Programming
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Declaration, assignment & accessing
Review of Arrays and Pointers
Pointer Data Type and Pointer Variables III
Introduction To Programming Information Technology , 1’st Semester
Conditions and Boolean Expressions
Multidimensional Arrays
Announcements & review
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
CS2011 Introduction to Programming I Arrays (I)
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Data Structures (CS212D) Week # 2: Arrays.
Managing Collections of Data
Chapter 8 Arrays Objectives
Arrays in Java.
EECE.2160 ECE Application Programming
Beginning C Lecture 5 Lecturer: Dr. Zhao Qinpei
Arrays.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
ICS103: Programming in C Searching, Sorting, 2D Arrays
Arrays.
Visit for more Learning Resources
Presentation transcript:

Arrays 1 2 3 4 5 Arrays are data structures that allow us to store data of the same type in contiguous memory locations. That was a pretty dense statement! To better understand the concept of an array, think back to the last time you picked up mail in the Science Center basement or your house's mailroom. An array is simply a block of contiguous space in memory (the mail center) that has been partitioned into identically-sized chunks (mailboxes). Each chunk can store a certain amount of data (mail) that can be accessed by an index number (mailbox number). Note that array indices in C always start at 0!

65 87 30 Creating an Array 1 2 <data type> name[<size>]; Example: int temperature[3]; temperature[0] = 65; temperature[1] = 87; temperature[2] = 30; OR int temperature[] = { 65, 87, 30 }; 1 2 65 87 30 Declare an array by specifying the data type it will store, its name, as well as its size. Here, we declare an array of 3 ints called temperature and load it with values. Alternatively, you can declare and initialize the array in a single step (in which case stating the size is optional). Because arrays store data contiguously in memory, array size is fixed after array declaration. You are effectively asking the OS to reserve the appropriate number of contiguous chunks of memory for the array's elements. There's no guarantee that more memory adjacent to your array will be available for use later, so arrays cannot easily grow.

Accessing Array Elements 1 2 65 87 30 for (int i = 0; i < 3; i++) { printf("%d\n", temperature[i]); } Because each element is associated with an array index, we have random access to all of the elements in an array. In other words, we can access any element in a single step by indexing into the array. This is a big deal because algorithms like binary search depend on random access. This code prints out all elements of the temperature array.

#include <stdio.h> #include <cs50.h> #define CLASS_SIZE 30 int main(void) { // declare array int scores_array[CLASS_SIZE]; // populate array for (int i = 0; i < CLASS_SIZE; i++) printf("Enter score for student %d: ", i); scores_array[i] = GetInt(); } } Here's an example of the usage of an array to keep track of student scores. This code prompts the user to enter a score for each student. Scores are stored in scores_array.

Where's the bug? string class[3] = { "Sam", "Jess", "Kim" }; for (int i = 0; i <= 3; i++) { printf("%s\n", class[i]); } What's the index number of the last element in an array of size n?

Multidimensional Arrays 0,0 0,1 0,2 x x 1,0 1,1 1,2 char board[3][3]; board[1][1] = 'o'; board[0][0] = 'x'; board[2][0] = 'o'; board[0][2] = 'x'; o Arrays can be multidimensional. You can think of multidimensional arrays as "arrays of arrays." Here, we declare and initialize a 2-dimensional array tic-tac-toe board. Note that dimensions must be declared explicitly for multidimensional arrays! 2,0 2,1 2,2 o

Accessing Multidimensional Array Elements // print out all elements for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) printf("%c", board[i][j]); printf("\n"); } This code uses nested for loops to print out all elements of the tic-tac-toe board array.