Introduction to Computers and Programming Class 21 Arrays Professor Avi Rosenfeld.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Arrays.
C Language.
Structure.
Structures Spring 2013Programming and Data Structure1.
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
Introduction to Computers and Programming - Class 2 1 Introduction to Computers and Programming Class 2 Introduction to C Professor Avi Rosenfeld.
1 ICS103 Programming in C Lecture 12: Arrays I. 2 Outline Motivation for One-dimensional Arrays What is a One-dimensional Array? Declaring One-dimensional.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 12: Arrays.
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
CS0007: Introduction to Computer Programming Introduction to Arrays.
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
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.
 2006 Pearson Education, Inc. All rights reserved Arrays.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Chapter 8 Arrays and Strings
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
Java Arrays [Lists] in 10 Minutes. Declaring an array (and its types) int[] myIntArray; double[] myDoubleArray; String[] myStrings; //What’s the pattern?
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
Week 91 Introduction to Programming Ms. Knudtzon C Period Quarter 2 – Lecture 20 Monday, November 1 st.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
Array, Structure and Union
CPS120: Introduction to Computer Science Lecture 15 Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
ICS103 Programming in C Lecture 11: Arrays I
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
CS201 – Introduction to Computing – Sabancı University 1 Built-in Arrays l C++ native array type (not the class version) l Two versions ä fixed size arrays.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Arrays.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Structures Declarations.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
Module 1: Array ITEI222 - Advance Programming Language.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
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.
CSI 3125, Preliminaries, page 1 Arrays. CSI 3125, Preliminaries, page 2 Arrays Group of related typed variables that referred to a common name Each data.
Arrays (Chapter 5)‏ Definition Applications One-Dimensional –Declaration –Initialization –Use Multidimensional.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Comp 248 Introduction to Programming Chapter 6 Arrays Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
EGR 2261 Unit 10 Two-dimensional Arrays
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
Arrays Declarations CSCI N305
Basic notes on pointers in C
7 Arrays.
Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.
CS2011 Introduction to Programming I Loop Statements (II)
By Yogesh Neopaney Assistant Professor Department of Computer Science
Structures Declarations CSCI 230
Presentation transcript:

Introduction to Computers and Programming Class 21 Arrays Professor Avi Rosenfeld

The Limit of Arrays You cannot have a variable for the size –ie. int x[y] is not legal if y is a variable. Arrays require the programmer to know in advance how much space (s)he will need Other structures (vectors, linked lists, stacks, and queues) which are not covered by this course deal with this limitation.

Initializing an Array You can initialize when you declare it, as you do with other variables Syntax is slightly different, as you are now initializing more than one element at a time One way at declaration, using initializers: int iMyFirstArray[ 5 ] = { 0, 0, 0, 0, 0 } ; Note the braces around the initial zeroes which themselves are separated by commas

Initializing an Array (cont’d) If you specify fewer initializing values than you have elements, all the rest are initialized to a value of 0! For examples, int iMyFirstArray[ 5 ] = { 0 }; would set all elements to 0 int iMyFirstArray[ 5 ] = { 4 }; would set the zeroth element to 4 and the rest to 0!

Initializing an array without specifying size You can also initialize and set the number of elements with the same step: int iMyFirstArray[ ] = { 1, 2, 0, 4, -2 } ; Note there is NO size specified; C automatically makes it 5 elements since that’s how many initial values you gave it

Initializing array with a for loop After declaring an array, you can initialize it in the body of your program by using a for loop: int i = 0, iMyFirstArray[ 5 ] ; /* size is 5*/ for ( i = 0 ; i <= 4 ; i++ ) { iMyFirstArray[ i ] = 0 ; } /* end for i */ Note the upper bound is 4, not 5! That is you loop through 0 to 4 to initialize an array with 5 elements

Accessing elements with for loop Can use a for loop to print the contents of an array #define SIZE 5 int i = 0, iMyFirstArray[ SIZE ] ; /* go through all element index */ for ( i = 0 ; i < SIZE ; i++ ) { printf( “ %d ”, iMyFirstArray[ i ]) ; } /* end for i */