CSC 113: C OMPUTER P ROGRAMMING (T HEORY = 03, L AB = 01) Computer Science Department Bahria University, Islamabad.

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

4.1Introduction Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based arrays (C-like) –Arrays.
 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.
 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.
Review (Week1) C++_ the unit of programming is the class from which objects are eventually instantiated. C++ classes contain functions that implement class.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring Arrays 6.4Examples Using Arrays 6.5Passing.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
 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.
 2003 Prentice Hall, Inc. All rights reserved Multiple-Subscripted Arrays Multiple subscripts –a[ i ][ j ] –Tables with rows and columns –Specify.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
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.
Searching Arrays Linear search Binary search small arrays
 2007 Pearson Education, Inc. All rights reserved. 1 C Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
 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.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 19 Thanks for Lecture Slides:
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Examples Using Arrays Passing arrays to functions Class GradeBook: store student grades.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
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.
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.
1 Arrays as Lists with examples. 2 Review Arrays –Structures of related data items of the same type –Data items (called array elements) are stored at.
 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.
1 Lecture 8 Arrays Part II Sorting Arrays Sorting data  Important computing application  Virtually every organization must sort some data Massive.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Vectors.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Multidimensional Arrays Case Study: Computing Mean, Median and Mode Using Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
 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.
C Lecture Notes 1 Arrays (...cont.). C Lecture Notes 2 6.6Sorting Arrays Sorting data –Important computing application –Virtually every organization must.
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig04_03.cpp (1 of 2) 1 // Fig. 4.3: fig04_03.cpp 2 // Initializing an array. 3 #include 4 5.
CHAPTER 07 Arrays and Vectors (part II). OBJECTIVES In this part you will learn:  To pass arrays to functions.  Basic searching techniques.
C++ Programming Lecture 16 Arrays – Part III 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.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
CHAPTER 3 ARRAYS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Arrays 3.Declaring Arrays 4.Examples Using Arrays 5.Multidimensional Arrays 6.Multidimensional.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Arrays Case Study.
EC-111 Algorithms & Computing Lecture #9 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Consecutive group of memory locations –Same name and type To refer to an element, specify.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays + Functions Outline 6.5Passing Arrays to Functions.
Arrays Outline 1 Introduction 2 Arrays 3 Declaring Arrays
C++ Programming Lecture 15 Arrays – Part II
CSC 113: Computer Programming (Theory = 03, Lab = 01)
آرايه ها اصول كامپيوتر 1.
C++ Programming Lecture 15 Arrays – Part II
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
C Arrays.
Arrays Kingdom of Saudi Arabia
Arrays Outline Introduction Arrays Declaring Arrays
CISC181 Introduction to Computer Science Dr
C++ Programming Lecture 16 Arrays – Part III
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
4.1 Introduction Arrays A few types Structures of related data items
Chapter 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Capitolo 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Arrays Arrays A few types Structures of related data items
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:

CSC 113: C OMPUTER P ROGRAMMING (T HEORY = 03, L AB = 01) Computer Science Department Bahria University, Islamabad

A RRAYS -II Week # 12

3 M ULTIPLE -S UBSCRIPTED A RRAYS Multiple subscripts - tables with rows, columns Like matrices: specify row, then column. Initialize int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } }; Initializers grouped by row in braces int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } }; 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

4 M ULTIPLE -S UBSCRIPTED A RRAYS Referenced like normal cout << b[ 0 ][ 1 ]; Will output the value of 0 Cannot reference with commas cout << b( 0, 1 ); Will try to call function b, causing a syntax error

S ELF P RACTICE Declare an array table to be an integer array with 3 rows and 3 columns. How many elements does the array contain? Using a for structure, initialize each element to the sum of its subscripts. Write a piece of code to print each element in tabular format of 3x3.

P ASSING A RRAYS AS FUNCTION ARGUMENTS Week # 12

7 P ASSING A RRAYS TO F UNCTIONS Specify the name without any brackets To pass array myArray declared as int myArray[ 24 ]; to function myFunction, a function call would resemble myFunction( myArray, 24 ); Array size is usually passed to function Arrays passed call-by-reference Value of name of array is address of the first element Function knows where the array is stored Modifies original memory locations Individual array elements passed by call-by-value pass subscripted name (i.e., myArray[ 3 ] ) to function

8 P ASSING A RRAYS TO F UNCTIONS Function prototype: void modifyArray( int b[], int arraySize ); Parameter names optional in prototype int b[] could be simply int [] int arraysize could be simply int

 2000 Prentice Hall, Inc. All rights reserved. Outline 9 1. Define function to take in arrays 1.1 Initialize arrays 2. Modify the array (call by reference) 1// Fig. 4.14: fig04_14.cpp 2// Passing arrays and individual array elements to functions 3#include 4 5using std::cout; 6using std::endl; 8#include 10using std::setw; 11 12void modifyArray( int [], int ); // appears strange 13void modifyElement( int ); 15int main() 16{ 17 const int arraySize = 5; 18 int i, a[ arraySize ] = { 0, 1, 2, 3, 4 }; 20 cout << "Effects of passing entire array call-by-reference:" 21 << "\n\nThe values of the original array are:\n"; 23 for ( i = 0; i < arraySize; i++ ) 24 cout << setw( 3 ) << a[ i ]; 26 cout << endl; 28 // array a passed call-by-reference 29 modifyArray( a, arraySize ); 31 cout << "The values of the modified array are:\n"; Functions can modify entire arrays. Individual array elements are not modified by default. No parameter names in function prototype. The values of the original array are: The values of the modified array

 2000 Prentice Hall, Inc. All rights reserved. Outline Modify an element (call by value) 3. Print changes. 3.1 Function Definitions 33 for ( i = 0; i < arraySize; i++ ) 34 cout << setw( 3 ) << a[ i ]; 36 cout << "\n\n\n" 37 << "Effects of passing array element call-by-value:" 38 << "\n\nThe value of a[3] is " << a[ 3 ] << '\n'; 40 modifyElement( a[ 3 ] ); 42 cout << "The value of a[3] is " << a[ 3 ] << endl; 44 return 0; 45} 49void modifyArray( int b[], int sizeOfArray ) 50{ 51 for ( int j = 0; j < sizeOfArray; j++ ) 52 b[ j ] *= 2; 53} 57void modifyElement( int e ) 58{ 59 cout << "Value in modifyElement is " 60 << ( e *= 2 ) << endl; 61} Parameter names required in function definition Effects of passing array element call-by-value: The value of a[3] is 6 Value in modifyElement is 12 The value of a[3] is 6

 2000 Prentice Hall, Inc. All rights reserved. Outline 11 Program Output Effects of passing entire array call-by- reference: The values of the original array are: The values of the modified array are: Effects of passing array element call-by-value: The value of a[3] is 6 Value in modifyElement is 12 The value of a[3] is 6

S ORTING A RRAYS Week # 12

13 S ORTING A RRAYS Sorting data Important computing application Virtually every organization must sort some data Massive amounts must be sorted Bubble sort (sinking sort) Several passes through the array Successive pairs of elements are compared If increasing order (or identical), no change If decreasing order, elements exchanged Repeat these steps for every element

14 S ORTING A RRAYS Example: Original: Pass 1: Pass 2: Small elements "bubble" to the top

E XAMPLE : B UBBLE S ORT //Sorting array in ascending order #include int main() { const int arraySize = 10; int a[arraySize]= {2,4,6,8,10,12,89,68,45,37}; int hold; cout<<“Data in original order”<<endl; for(int i=0; i<arraySize; i++) cout<<a[i]; for(int pass=1; pass<arraySize; pass++) for(i=0; i<arraySize-1; i++) if(a[i]>a[i+1]){ hold=a[i]; a[i]=a[i+1]; a[i+1]=hold;} cout<<endl<<“Data items in ascending order”<<endl; for(i=0; i<arraySize; i++) cout<<a[i]; cout<<endl; return 0; }

17 E XAMPLES : C OMPUTING M EAN, M EDIAN AND M ODE U SING A RRAYS Mean Average Median Number in middle of sorted list 1, 2, 3, 4, 5 (3 is median) Mode Number that occurs most often 1, 1, 1, 2, 3, 3, 4, 5 (1 is mode)

S EARCHING A RRAYS Week # 12

19 S EARCHING A RRAYS Search array for a key value Linear Search Binary Search Linear search Compare each element of array with key value Useful for small and unsorted arrays Binary search Can only be used on sorted arrays Compares middle element with key If equal, match found If key < middle, repeat search through the first half of the array If key > middle, repeat search through the last half of the array Very fast; at most n steps, where 2^ n > # of elements 30 element array takes at most 5 steps 2^5 > 30

E XAMPLE : L INEAR S EARCH //Linear Search of an array #include int linearSearch(int[], int, int); int main() { const int arraySize = 100; int a[arraySize], searchKey, element; for(int i=0; i<arraySize; i++) a[i]=2*i; cout<<“Enter integer search key:” <<endl; cin>>searchKey; element=linearSearch(a, searchKey, arraySize); if(element!=-1) cout<<“Found value in element” <<element<<endl; else cout<<“Value not found”<<endl; return 0; } int linearSearch(int array[], int key, int sizeOfArray) { for(int n=0; n<sizeOfArray; n++) if(a[n]==key) return element; else return -1; }

E XAMPLE : B INARY S EARCH //Linear Search of an array #include int binarySearch(int[], int, int, int, int); int main() { const int arraySize = 100; int a[arraySize], searchKey, element; for(int i=0; i<arraySize; i++) a[i]=2*i; cout<<“Enter integer search key:” <<endl; cin>>searchKey; element=binarySearch(a, searchKey, 0, arraySize-1, arraySize); if(element!=-1) cout<<“Found value in element” <<element<<endl; else cout<<“Value not found”<<endl; return 0; } int binarySearch(int array[], int key, int low, int high, int sizeOfArray) { int middle; while(low<=high) { middle=(low+high)/2; if(key==array[middle]) return middle; else if(key<array[middle]) high=middle-1; else low=middle+1; } return -1; }

B INARY SEARCH