Arrays (Operations on Arrays)

Slides:



Advertisements
Similar presentations
2000 Prentice Hall, Inc. All rights reserved. 1 Capitolo 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
Advertisements

Introduction to C Programming
Chapter 7: Arrays In this chapter, you will learn about
Searching for Data Relationship between searching and sorting Simple linear searching Linear searching of sorted data Searching for string or numeric data.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
© 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.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Searching Arrays Linear search Binary search small arrays
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Arrays in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Topics to be covered  Introduction to array Introduction to array  Types of array Types of array  One dimensional array One dimensional array  Declaration.
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.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
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.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
 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.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Arrays Department of Computer Science. C provides a derived data type known as ARRAYS that is used when large amounts of data has to be processed. “ an.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays + Functions Outline 6.5Passing Arrays to Functions.
Searching Arrays Linear search Binary search small arrays
Chapter 9: Sorting and Searching Arrays
16 Searching and Sorting.
Data Structures I (CPCS-204)
Arrays Declarations CSCI N305
C Arrays.
Linear and Binary Search
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
C Arrays.
7 Arrays.
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
CS-161 Computer Programming Lecture 14: Arrays I
EKT150 : Computer Programming
Arrays Outline Introduction Arrays Declaring Arrays
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Review of Arrays and Pointers
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
Search,Sort,Recursion.
24 Searching and Sorting.
Data Structures (CS212D) Week # 2: Arrays.
6 C Arrays.
Arrays Week 2.
7 Arrays.
Search,Sort,Recursion.
To refer to an element, specify
Arrays in Java.
Capitolo 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Arrays Arrays A few types Structures of related data items
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Chapter 3 Arrays Dr. A. PHILIP AROKIADOSS Assistant Professor
Programming Fundamental
Arrays, Part 1 of 2 Topics Definition of a Data Structure
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
ADT LIST So far we have seen the following operations on a “list”:
Presentation transcript:

Arrays (Operations on Arrays) @LPU CSE202 C++ Programming

Outline To declare an array To initialize an array Operations on array @LPU CSE202 C++ Programming

Introduction Arrays Collection of related data items of same data type. Static entity – i.e. they remain the same size throughout program execution Quick yak: Ask students where they encounter array in routine ex: Buttons on mobile…(huh now it is touch screen!!!) @LPU CSE202 C++ Programming

Arrays Array To refer to an element, specify: Format: Name of array (Note that all elements of this array have the same name, c) Position number of the element within array c 3 c[6] -45 6 72 -89 62 -3 1 6453 78 c[0] c[1] c[2] c[3] c[11] c[10] c[9] c[8] c[7] c[5] c[4] Array Group of consecutive memory locations Same name and data type To refer to an element, specify: Array name Position number in square brackets([]) Format: arrayname[position_number] First element is always at position 0 Eg. n element array named c: c[0], c[1]...c[n – 1] @LPU CSE202 C++ Programming

Arrays An array is an ordered list of values c The entire array has a single name Each value has a numeric index c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] 79 87 94 82 67 98 87 81 74 91 An array of size N is indexed from zero to N-1 This array holds 10 values that are indexed from 0 to 9 @LPU CSE202 C++ Programming

Arrays Array elements are like normal variables c[0] = 3;/*stores 3 to c[0] element*/ scanf (“%d”, &c[1]);/*reads c[1] element*/ printf (“%d, %d”, c[0], c[1]); /*displays c[0] & c[1] element*/ The position number inside square brackets is called subscript/index. Subscript must be integer or an integer expression c[5 - 2] = 7; (i.e. c[3] = 7) @LPU CSE202 C++ Programming

Defining Arrays When defining arrays, specify: Name Data Type of array Number of elements datatype arrayName[numberOfElements]; Examples: int students[10]; float myArray[3284]; Defining multiple arrays of same data type Format is similar to regular variables Example: int b[100], x[27]; @LPU CSE202 C++ Programming

Initializing Arrays Initializers Quick yak: Probe on the example below ask what happens: for (i=1;i<5;i++) for (i=0;i<5;i+=2) Initializers int n[5] = { 1, 2, 3, 4, 5 }; If not enough initializers given, then rightmost elements become 0 int n[5] = { 0 }; // initialize all elements to 0 C arrays have no bounds checking. If size is omitted, initializers determine it int n[] = { 1, 2, 3, 4, 5 }; 5 initializers, therefore 5 element array. @LPU CSE202 C++ Programming

Initializing Arrays Array is same as the variable can prompt for value from the user at run time. Array is a group of elements so we use for loop to get the values of every element instead of getting single value at a time. Example: int array[5]; // array of size 5 for(int i=0;i<5;i++){// loop begins from 0 to 4 cin>>&array[i]; } @LPU CSE202 C++ Programming

Program of Initializing an array to zero using loop. @LPU CSE202 C++ Programming

Element Value 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 Quick yak: Discussion can be quickly done on \t endl Used in the program n[6] n[0] n[1] n[2] n[3] n[9] n[8] n[7] n[5] n[4] @LPU CSE202 C++ Programming

Program of Initializing an array element with calculations using loop. @LPU CSE202 C++ Programming

Element Value 0 2 1 4 2 6 3 8 4 10 5 12 6 14 7 16 8 18 9 20 10 n[6] 4 6 8 12 14 16 18 20 n[0] n[1] n[2] n[3] n[9] n[8] n[7] n[5] n[4] @LPU CSE202 C++ Programming

Operations on arrays Insertion of element into an array Deletion of element from an array Search of element in an array @LPU CSE202 C++ Programming

Program to insert an element into an array #include<iostream.h> #include<conio.h> int main() { int a[100],i,n,k, item; cout<<"how many no to store in array"; cin>>n; cout<<"Enter the number"; for(i=0;i<=n-1;i++) cin>>a[i]; cout<<"Enter the no. and its position"; cin>>tem>>k; k=k-1; for(i=n-1;i>=k;i--) a[i+1]=a[i]; } a[k]=item; cout<<"Contents of the array\n"; for(i=0;i<=n;i++) cout<<a[i]; getch(); Program to insert an element into an array @LPU CSE202 C++ Programming

Output How many no to store in array: 4 Enter the number: 12 14 5 11 Enter the no. and the position: 20 3 Content of the array 12 20 Output @LPU CSE202 C++ Programming

Program to delete an element from an array #include<iostream.h> #include<conio.h> int main() { int a[100],i,n,k; cout<<"how many no to store in array"<<endl; cin>>n; cout<<"enter the number"<<endl; for(i=0;i<n;i++) cin>>a[i]; cout<<"enter the position"; cin>>k; k=k-1; for(i=k;i<n;i++) a[i]=a[i+1]; } cout<<"contents of the array"<<endl; for(i=0;i<n-1;i++) cout<<a[i]; getch(); Program to delete an element from an array @LPU CSE202 C++ Programming

Output How many no to store in array: 4 Enter the number: 12 14 5 11 Enter the position: 3 Content of the array 12 Output @LPU CSE202 C++ Programming

Searching in Arrays The process of finding a particular element of an array is called searching. Search an array for a key value. Two searching techniques: Linear search Binary search @LPU CSE202 C++ Programming

Linear search Linear search Simple Compare each element of array with key value Useful for small and unsorted arrays It simply examines each element sequentially, starting with the first element, until it finds the key element or it reaches the end of the array. Example: If you were looking for someone on a moving passenger train, you would use a sequential search. @LPU CSE202 C++ Programming

Program of linear search in an array. #include<iostream.h> #include<conio.h> int main() { int a[20],key,i,n, c=0; cout<<“Enter the number of elements:\t"; cin>>n; cout<<"Enter the elements:\t"; for(i=0;i<n;i++) cin>>a[i]; cout<<"Enter the element to be found \t"; cin>>key; if(a[i]==key) //comparison cout<<“Key found at location \t"<<i; c++; break; } if (c==0) cout<<"element not found in the list"; getch(); return 0; Program of linear search in an array. @LPU CSE202 C++ Programming

Output Enter the number of elements: 4 Enter the element: 12 14 5 11 Enter a number to be found: 14 Key found at location 2 Output @LPU CSE202 C++ Programming

Binary search Binary search Applicable for sorted arrays The algorithm locates the middle element of the array and compares it to the key value. Compares middle element with the key If equal, match found If key < middle, looks in left half of middle If key > middle, looks in right half of middle Repeat (the algorithm is repeated on one-quarter of the original array.) @LPU CSE202 C++ Programming

Binary search It repeatedly divides the sequence in two, each time restricting the search to the half that would contain the element. This is a tremendous increase in performance over the linear search that required comparing the search key to an average of half of the array elements. You might use the binary search to look up a word in a dictionary @LPU CSE202 C++ Programming

Program of binary search in an array. #include<iostream.h> #include<conio.h> int main() { int ar[100],beg,mid,end,i,n,search; cout<<"How many numbers in the array: "; cin>>n; cout<<"Enter "<<n<<" numbers in ascending order --> "; for(i=0;i<n;i++) cin>>ar[i]; beg=0;end=n-1; cout<<"Enter a number to search: "; cin>>search; while(beg<=end) mid=(beg+end)/2; if(ar[mid]==search) cout<<"\nItem found at position"<<(mid+1); if(search>ar[mid]) beg=mid+1; else end=mid-1; } cout<<"\nSorry! "<<search<<" doesnot found."; getch(); Program of binary search in an array. @LPU CSE202 C++ Programming

Output How many numbers in the array: 4 Enter 4 numbers in ascending order12 14 26 47 Enter a number to search:26 Item found at position 3 Output @LPU CSE202 C++ Programming

Next Class: Pointers @LPU CSE202 C++ Programming