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.

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

What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Arrays, A1 COMP 401, Fall 2014 Lecture 4 8/28/2014.
5 5 Arrays. OBJECTIVES In this lecture we will learn:  Case switch  To use the array data structure to represent a set of related data items.  To declare.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
 2006 Pearson Education, Inc. All rights reserved Arrays.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
Chapter 8 Arrays and Strings
TODAY’S LECTURE Review Chapter 2 Go over exercises.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
C Static Arrays Pepper. What is an array? Memory locations – same type – next to each other (contiguous) – Same name – Indexed by position number of type.
 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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
C++ Lecture 3 Monday, 14 July Arrays, Pointers, and Strings l Use of array in C++, multi- dimensional array, array argument passing l Pointers l.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Other Variable Types Dim lab as String makes a box that can store a label tag Dim ColHead As String ColHead = “function” ColHead function Dim lab as Boolean.
Chapter 8: Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
Pointers *, &, array similarities, functions, sizeof.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Computer And Programming Array and Pointer. Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to.
Variables Symbol representing a place to store information
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
Computer Programming for Engineers
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
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)
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Arrays and Indexers Programming in C# Arrays and Indexers CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Arrays. Arrays are objects that help us organize large amounts of information.
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
Chapter 9 Introduction to Arrays Fundamentals of Java.
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.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Chapter VII: Arrays.
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
EKT150 : Computer Programming
Building Java Programs
Suggested self-checks: Section 7.11 #1-11
Variables and Constants
Presentation transcript:

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 name –All of these numbers must be of identical data type Similar to a multi-dimensional vector

How to Define Declaration type [size]; Example –To define an integer array called numbers of size int numbers[5]; numbers

How to Use Arrays How to refer to an individual element of an array –Accessed by their position (index) in the array, e.g. numbers[1] = 2; Array elements are always numbered beginning with 0 numbers[0] numbers[1] numbers[2] numbers[3] numbers[4] 2

Example – 0 Get a set of numbers from the user int numbers[5]; int i; for( i = 0; i < 5; i++ ) { scanf("%i", &numbers[i]); }

Example – I Calculate statistics of a set of numbers –Summation int sum = 0; for( i = 0; i < 5; i++ ) { sum = sum + numbers[i]; } –Finding the maximum value int maxValue = numbers[0]; for( i = 1; i < 5; i++ ) { if (numbers[i] > maxValue) maxValue = numbers[i]; }

Fibonacci Numbers in an Array n F(n)

Code #include #define SIZE 15 int main() { int fib[SIZE], i; fib[0] = 0; //set initial values fib[1] = 1; //set initial values for ( i = 2; i < SIZE; i++ ) { fib[i] = fib[i - 2] + fib[i - 1]; } for ( i = 0; i < SIZE; i++ ) { printf(”fibonacci[%i] = %i\n", i, fib[i]); } return 0; }

Prime Number Definition (could be you already know this?) –A positive integer that has no positive integer divisors other than 1 and itself. Testing whether a number p is a prime number Is there an integer divisor between 2 and p-1 for p?

Approaches – I Base version 1 int d; /* possible divisor */ int p; /* number whose “primeness” we are testing. */ for (d = 2; d < p; d++) { if (p % d == 0) { printf(“%i is not a prime!\n”, p); break; }

Approaches – II Version 2 #define TRUE 1 #define FALSE 0 int isPrime = TRUE; int d,p; for (d = 2; d < p; d++) { if (p % d == 0) { isPrime = FALSE; break; } if (isPrime) printf(“%i is prime\n”, p); else printf(“%i is not prime\n”, p);

Approaches – III Observation –Only need to test whether there is a integer divisor between 2 and for p for (d = 2; d * d <= p; d++) { if (p % d == 0) { isPrime = FALSE; break; }

Approaches – IV Observation –Only need to check whether a prime number between 2 and is a divisor –Need to record which numbers are prime so we only test them once Using arrays!

Code Declare variables int p, i; int primes[PRIMES]; // for noting prime numbers int primeIndex; // next place to store a prime int isPrime; // boolean variable Initial setup primes[0] = 2;// we know 2 is prime primes[1] = 3;// we know 3 is prime primeIndex = 2; // the next array slot is [2]

Code – cont for (p = 5; p <= PRIMES; p += 2 ) { isPrime = TRUE; // start by assuming p is prime i = 1; // let’s skip the first prime (its 2) while (i < primeIndex) { if ( p % primes[i] == 0 ) { isPrime = FALSE; break;// leave loop, no point to // testing more divisors } i++; } if (isPrime) { primes[primeIndex] = p;// Store prime p ++primeIndex;// advance index if (primeIndex > PRIMES) break; // prime array is full }

Initializing arrays Initializing an array using a comma- separated list of constants in { } –int number[] = { 5, 7, 2 }; Size of an array is automatically set as the number of elements within { }

Character Arrays and Strings A character array contains characters (naturally) –char word[ ] = {‘H’,’e’,’l’,’l’,’o’,’!’} Strings –A special character ‘\0’ (called a “null” character) at the end of a character array indicates the end of a string. –Use %s in printf to print a string –initialization char word[]="hello"; hello\0

The const qualifier When we need the const qualifier –Specify a variable whose value is constant, i.e., value will not be changed How to use –Put const in front of variable definition const double pi = ; What if I attempt to change its value –E.g. pi = pi * 2; –I will get a compiler Warning/Error: “Assignment of read-only variable ‘pi’”

Multidimensional Arrays Declaration –type name[ nrows][ncols]; e.g. int m[5][10]; Access –m[3][4]; Not m[3,4], or m(3)(4) n columns j changes m–by–n matrix m rows i changes

Multidimensional Arrays Initialization int M[2][3] = {{1, 2, 3}, {4, 5,6}}; –Or int M[2][3] = {1, 2, 3, 4, 5, 6}; Numbers will fill up the first row, then the second row,…