Arrays CE 102 Algorithms and Programming KTO Karatay University Arrays are data structures consisting of data items of the same type Arrays are not dynamic.

Slides:



Advertisements
Similar presentations
Arrays. Introduction Arrays –Structures of related data items –Static entity - same size throughout program A few types –C-like, pointer-based arrays.
Advertisements

 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
 2003 Prentice Hall, Inc. All rights reserved Introduction Arrays –Structures of related data items –Static entity (same size throughout program)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring Arrays 6.4Examples Using Arrays 6.5Passing.
 2006 Pearson Education, Inc. All rights reserved Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Arrays Dale Roberts, Lecturer
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
Chapter 8 Arrays and Strings
 2007 Pearson Education, Inc. All rights reserved. 1 C Arrays.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
CS1061 C Programmuing Lecture 12 Arrays A. O’Riordan, 2004.
Systems Programming Concepts
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 8 Arrays and Strings
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify.
Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify –Array name –Position number Format: arrayname.
C Lecture Notes 1 Arrays Lecture 6. C Lecture Notes 2 6.1Introduction Arrays –Structures of related data items –Static entity – same size throughout program.
C Arrays Systems Programming. Systems Programming: Arrays 22 ArraysArrays  Arrays  Defining and Initializing Arrays  Array Example  Subscript Out-of-Range.
1 Lecture 8 Arrays Part II Sorting Arrays Sorting data  Important computing application  Virtually every organization must sort some data Massive.
1 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing Arrays to Functions 4.6Sorting Arrays 4.7Case.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays.
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
C Lecture Notes 1 Arrays (...cont.). C Lecture Notes 2 6.6Sorting Arrays Sorting data –Important computing application –Virtually every organization must.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring.
CHAPTER 3 ARRAYS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Arrays 3.Declaring Arrays 4.Examples Using Arrays 5.Multidimensional Arrays 6.Multidimensional.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Consecutive group of memory locations –Same name and type To refer to an element, specify.
CSC 113: C OMPUTER P ROGRAMMING (T HEORY = 03, L AB = 01) Computer Science Department Bahria University, Islamabad.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays + Functions Outline 6.5Passing Arrays to Functions.
C Arrays.
C Arrays Systems Programming.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
C Arrays.
7 Arrays.
Dale Roberts, Lecturer IUPUI
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Chapter 9 - Arrays Outline 6.1 Introduction 6.2 Arrays
Arrays Kingdom of Saudi Arabia
Arrays Outline Introduction Arrays Declaring Arrays
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Arrays Strings and Parameter Passing CSCI N305
6 C Arrays.
7 Arrays.
To refer to an element, specify
Capitolo 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Arrays Arrays A few types Structures of related data items
Arrays.
Chapter 3 Arrays Dr. A. PHILIP AROKIADOSS Assistant Professor
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Arrays CE 102 Algorithms and Programming KTO Karatay University Arrays are data structures consisting of data items of the same type Arrays are not dynamic entities they remain the same size once they are created they don’t grow or shrink as the program executes

Arrays CE 102 Algorithms and Programming KTO Karatay University arr[ 11 ] arr[ 10 ] arr[ 9 ] arr[ 8] arr[ 7 ] arr[ 4 ] arr[ 3 ] arr[ 2 ] arr[ 1 ] arr[ 0 ] arr[ 6 ] arr[ 5 ] Position number (index or subscript) of the element within array “arr” arr” Name of array (Note that all elements of this array have the same name, “ arr” ) First element is at index “0”

Declaring Arrays When defining arrays, specify When defining arrays, specify NameName Type of arrayType of array Number of elementsNumber of elements arrayType arrayName[ numberOfElements ]; Examples:Examples: int c[ 10 ]; float myArray[ 3284 ]; Defining multiple arrays of same type Defining multiple arrays of same type Format similar to regular variablesFormat similar to regular variables Example:Example: int a[ 100 ], b[ 27 ]; CE 102 Algorithms and Programming KTO Karatay University

Declaring Arrays Initializers Initializers int n[ 5 ] = { 1, 2, 3, 4, 5 }; If not enough initializers, rightmost elements become 0If not enough initializers, rightmost elements become 0 int n[ 5 ] = { 0 }  All elements 0 If too many, a syntax error is producedIf too many, a syntax error is produced int n[ 5 ] = { 0, 5, -7, 9, 2, 1, 8 } If size is omitted, initializers determine it If size is omitted, initializers determine it int n[ ] = { 1, 2, 3, 4, 5 }; 5 initializers, therefore 5 element array5 initializers, therefore 5 element array CE 102 Algorithms and Programming KTO Karatay University

Example – Declaring and Using Arrays CE 102 Algorithms and Programming KTO Karatay University

Example – Using Arrays as Index and Histogram Printing CE 102 Algorithms and Programming KTO Karatay University

Character Arrays Strings are in fact sequences of characters, we can represent them also as plain arrays of char elements. Strings are in fact sequences of characters, we can represent them also as plain arrays of char elements. For example, the following array: For example, the following array: char name[20] is an array that can store up to 20 elements of type char. It can be shown as: In this array, we can store sequences of characters up to 20 characters long. In this array, we can store sequences of characters up to 20 characters long. But we can also store shorter sequences. But we can also store shorter sequences. A special character is used to signal the end of the valid sequence: the null character which can be written as ‘\0’ A special character is used to signal the end of the valid sequence: the null character which can be written as ‘\0’ name Deet\0mDeetFiliz m CE 102 Algorithms and Programming KTO Karatay University

Character Arrays Initializing on declaration: Initializing on declaration: char myword [] = { 'H', 'e', 'l', 'l', 'o', '\0' }; char myword [] = "Hello"; //initialization by a literal A literal can not be assigned to char array after the declaration: A literal can not be assigned to char array after the declaration: char text[10]; text = “some text”; text = {‘H’,‘i’,‘/0’}; Content of char array can be modified letter by letter after initialization: Content of char array can be modified letter by letter after initialization: char text[30] = "Here is some text!"; printf(“%s\n”,text); text[13] = 'w'; text[14] = 'i'; text[15] = 'n'; text[16] = 'e'; printf(“%s\n”,text); CE 102 Algorithms and Programming KTO Karatay University

Character Arrays CE 102 Algorithms and Programming KTO Karatay University ExampleExample char string1[] = "first"; Null character '\0' terminates strings Null character '\0' terminates strings string1 actually has 6 elements string1 actually has 6 elements It is equivalent toIt is equivalent to char string1[] = { 'f', 'i', 'r', 's', 't', '\0' }; Can access individual charactersCan access individual characters string1[ 3 ] is character ‘s’ Array name is address of array, so “&” not needed for scanfArray name is address of array, so “&” not needed for scanf scanf( "%s", string2 ); Reads characters until whitespace encountered Reads characters until whitespace encountered Can write beyond end of array, be careful Can write beyond end of array, be careful

Example - Character Arrays CE 102 Algorithms and Programming KTO Karatay University

Passing Arrays to Functions CE 102 Algorithms and Programming KTO Karatay University Passing arrays Passing arrays To pass an array argument to a function, specify the name of the array without any bracketsTo pass an array argument to a function, specify the name of the array without any brackets int myArray[ 24 ]; myFunction( myArray, 24 ); Array size usually passed to function Array size usually passed to function Arrays passed call-by-referenceArrays passed call-by-reference Name of array is address of first elementName of array is address of first element Function knows where the array is storedFunction knows where the array is stored Modifies original memory locations Modifies original memory locations Passing array elements Passing array elements Passed by call-by-valuePassed by call-by-value Pass subscripted name (i.e., myArray[ 3 ]) to functionPass subscripted name (i.e., myArray[ 3 ]) to function

Passing Arrays to Functions Function prototype Function prototype void modifyArray( int b[], int arraySize ); Parameter names optional in prototypeParameter names optional in prototype int b[] could be written int [] int b[] could be written int [] int arraySize could be simply int int arraySize could be simply int void modifyArray(int [], int) CE 102 Algorithms and Programming KTO Karatay University

Example – Array “Name” is equal to &Name[0] CE 102 Algorithms and Programming KTO Karatay University

Example – Passing Arrays to Functions CE 102 Algorithms and Programming KTO Karatay University

Example – Passing Arrays to Functions CE 102 Algorithms and Programming KTO Karatay University

Example – const type qualifier with arrays CE 102 Algorithms and Programming KTO Karatay University

Sorting Arrays CE 102 Algorithms and Programming KTO Karatay University Sorting data Sorting data Important computing applicationImportant computing application Virtually every organization must sort some dataVirtually every organization must sort some data Bubble sort (sinking sort) Bubble sort (sinking sort) Several passes through the arraySeveral passes through the array Successive pairs of elements are comparedSuccessive pairs of elements are compared If increasing order (or identical ), no change If increasing order (or identical ), no change If decreasing order, elements exchanged If decreasing order, elements exchanged RepeatRepeat Example: Example: original: original: pass 1: pass 1: pass 2: pass 2: Small elements "bubble" to the topSmall elements "bubble" to the top BubbleSort.c

Searching Arrays : Linear Search Search an array for a key value Search an array for a key value Linear search Linear search SimpleSimple Compare each element of array with key valueCompare each element of array with key value Useful for small and unsorted arraysUseful for small and unsorted arrays Example  LinearSearch.c Example  LinearSearch.c CE 102 Algorithms and Programming KTO Karatay University

Searching Arrays : Binary Search Binary search Binary search For sorted arraysFor sorted arrays Compares middle element with keyCompares middle element with key If equal, match found If equal, match found If key < middle, looks in first half of array If key < middle, looks in first half of array If key > middle, looks in last half If key > middle, looks in last half Repeat Repeat Very fast; at most n steps, where 2 n > number of elementsVery fast; at most n steps, where 2 n > number of elements 30 element array takes at most 5 steps 30 element array takes at most 5 steps 2 5 > 30 so at most 5 steps2 5 > 30 so at most 5 steps BinarySearch.c CE 102 Algorithms and Programming KTO Karatay University

Multi-dimensional Arrays Multidimensional arrays can be described as "arrays of arrays". Multidimensional arrays can be described as "arrays of arrays". A two dimentional (2D) array can be imagined as a table made of elements all having the same data type A two dimentional (2D) array can be imagined as a table made of elements all having the same data type Arrays can be 1D, 2D, 3D or even more... Arrays can be 1D, 2D, 3D or even more... 2D array having size 3x5 has 15 elements2D array having size 3x5 has 15 elements 3D array having size 4x5x3 has 60 elements3D array having size 4x5x3 has 60 elements As dimension increase, array size grows quicklyAs dimension increase, array size grows quickly CE 102 Algorithms and Programming KTO Karatay University

Multi-dimensional Arrays Decleration of a 2D array: Decleration of a 2D array: int matrix[3][5];int matrix[3][5]; type name[row_size][column_size];type name[row_size][column_size]; Access to elements: matrix[1][3] = 12; Access to elements: matrix[1][3] = 12; CE 102 Algorithms and Programming KTO Karatay University

Multi-dimensional Arrays Row 0 Row 1 Row 2 Column 0Column 1Column 2Column 3 a[ 0 ][ 0 ] a[ 1 ][ 0 ] a[ 2 ][ 0 ] a[ 0 ][ 1 ] a[ 1 ][ 1 ] a[ 2 ][ 1 ] a[ 0 ][ 2 ] a[ 1 ][ 2 ] a[ 2 ][ 2 ] a[ 0 ][ 3 ] a[ 1 ][ 3 ] a[ 2 ][ 3 ] Row subscript Array name Column subscript CE 102 Algorithms and Programming KTO Karatay University

Array Initialization int a[3][4] = {1,2,3,4,5,6,7,8,9,0,1,2}; int b[3][4] = {{1,2,3,4},{5,6,7,8},{9,0,1,2}}; int c[3][4] = {{1,2,3,4},{5,6},{}}; Examples  ArrayInitialization.c Examples  ArrayInitialization.cStudentGrades.c Unspecified elements are set to zero Unspecified elements are set to zero CE 102 Algorithms and Programming KTO Karatay University

Multi-dimensional Arrays (3D) Decleration of a 3D array: Decleration of a 3D array: int cube[4][4][4];int cube[4][4][4]; type name[row_size][column_size][depth_size];type name[row_size][column_size][depth_size]; Access to elements: cube[2][3][0] = 8; cube[0][3][2] = 5; cube[0][1][3] = 6; depth columns rows CE 102 Algorithms and Programming KTO Karatay University

Homework-2 CE 102 Algorithms and Programming KTO Karatay University Write a C program that calculates mean, median and mode of an array entered by the user. Mean – average Mean – average Median – number in middle of sorted list Median – number in middle of sorted list 1, 2, 3, 4, 51, 2, 3, 4, 5 3 is the median3 is the median Mode – number that occurs most often Mode – number that occurs most often 1, 1, 1, 2, 3, 3, 4, 51, 1, 1, 2, 3, 3, 4, 5 1 is the mode1 is the mode The program will display the histogram of the array after finding the mode.