Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Arrays. Introduction Arrays –Structures of related data items –Static entity - same size throughout program A few types –C-like, pointer-based arrays.
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.
 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
Chapter 8 Arrays and Strings
 2007 Pearson Education, Inc. All rights reserved. 1 C Arrays.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
CS1061 C Programmuing Lecture 12 Arrays A. O’Riordan, 2004.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
Systems Programming Concepts
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 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
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.
 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.
 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.
CHAPTER 07 Arrays and Vectors (part II). OBJECTIVES In this part you will learn:  To pass arrays to functions.  Basic searching 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.
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.
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.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
 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.
© 2016 Pearson Education, Ltd. All rights reserved.
C Arrays.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
C Arrays.
7 Arrays.
Dale Roberts, Lecturer IUPUI
7 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.
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
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI

2 One-Dimensional Arrays Often, programs use homogeneous data. As an example, if we want to manipulate some grades, we might declare int grade0, grade1, grade2; If we have a large number of grades, it becomes cumbersome to represent/manipulate the data using unique identifiers. Arrays allow us to refer to a large number of the same data type using a single name. For instance, int grade[3];

3 One-Dimensional Arrays Makes available the use of integer variables grade[0], grade[1], grade[2] in a program. Declaration syntax: Type array_name[number_of_elements] WARNING: arrays are zero-indexed (numbering always starts at 0). Now, to access elements of this array, we can write grade[expr], Example: for( i = 0; i < 3; i++ ) sum += grade[i];

4

5 Initializers int n[ 5 ] = { 1, 2, 3, 4, 5 }; –If not enough initializers, rightmost elements become 0 int n[ 5 ] = { 0 } All elements 0 –If too many initializers, a syntax error occurs –C arrays have no bounds checking If size omitted, initializers determine it int n[ ] = { 1, 2, 3, 4, 5 }; –5 initializers, therefore 5 element array

6 Initializers –If there are fewer initializations than elements in the array, then the remaining elements are automatically initialized to 0. int n[5] = {0} /* All elements 0 */ int a[10] = {1, 2} /* a[2] to a[9] are initialized to zeros */ int b[5] = {1, 2, 3, 4, 5, 6} /* syntax error */

7 for loop initializes each array element separately for loop outputs all array elements

8 initializer list initializes all array elements simultaneously

9 #define directive tells compiler to replace all instances of the word SIZE with 10 SIZE is replaced with 10 by the compiler, so array s has 10 elements for loop initializes each array element separately Defining the size of each array as a symbolic constant makes programs more scalable.

10 Tip Use only uppercase letters for symbolic constant names. This makes these constants stand out in a program and reminds you that symbolic constants are not variables.

11 #define directives create symbolic constants frequency array is defined with 11 elements responses array is defined with 40 elements and its elements are initialized subscript of frequency array is given by value in responses array

12

13 nested for loop prints n[ i ] asterisks on the i th line

14

15 for loop uses one array to track number of times each number is rolled instead of using 6 variables and a switch statement

16

17 Character arrays –Character arrays can be initialized using string literals char string1[] = "first"; Null character '\0' terminates strings string1 actually has 6 elements –It is equivalent to char string1[] = { 'f', 'i', 'r', 's', 't', '\0' }; –Can access individual characters string1[ 3 ] is character ‘s’ –Array name is address of array, so & not needed for scanf scanf( "%s", string2 ); Reads characters until whitespace encountered Be careful not to write past end of array, as it is possible to do so

18

19 static array is created only once, when staticArrayInit is first called

20 automatic array is recreated every time automaticArrayInit is called

21

22 Passing arrays –To 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 –Arrays passed call-by-reference –Name of array is address of first element –Function knows where the array is stored Modifies original memory locations Passing array elements –Passed by call-by-value –Pass subscripted name (i.e., myArray[ 3 ] ) to function

23 Function prototype void modifyArray( int b[], int arraySize ); –Parameter names optional in prototype int b[] could be written int [] int arraySize could be simply int

24 Tip Passing arrays by reference makes sense for performance reasons. If arrays were passed by value, a copy of each element would be passed. For large, frequently passed arrays, this would be time consuming and would consume considerable storage for the copies of the arrays.

25 Function prototype indicates function will take an array Array a is passed to modifyArray by passing only its name

26 Array element is passed to modifyElement by passing a[ 3 ]

27

28 const qualifier tells compiler that array cannot be changed Any attempts to modify the array will result in errors

29 Tip The const type qualifier can be applied to an array parameter in a function definition to prevent the original array from being modified in the function body. Functions should not be given the capability to modify an array unless it is absolutely necessary.

30 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort some data 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 Example: –original: –pass 1: –pass 2:

31

32 If any two array elements are out of order, the function swaps them

33 Computing Mean, Median and Mode Using Arrays Mean – average Median – number in middle of sorted list –1, 2, 3, 4, 5 –3 is the median Mode – number that occurs most often –1, 1, 1, 2, 3, 3, 4, 5 –1 is the mode

34

35

36 Once the array is sorted, the median will be the value of the middle element

37

38

39

40

41

42 Searching Arrays Search an array for a key value Linear search –Simple –Compare each element of array with key value –Useful for small and unsorted arrays

43

44

45 Linear search algorithm searches through every element in the array until a match is found

46 Searching Arrays Binary search –For sorted arrays only –Compares middle element with key If equal, match found If key < middle, looks in first half of array If key > middle, looks in last half Repeat –Very fast; at most n steps, where 2 n > number of elements 30 element array takes at most 5 steps –2 5 > 30 so at most 5 steps

47

48

49 If value is found, return its index If value is too high, search the left half of array If value is too low, search the right half of array

50

51

52

53 Multiple-Subscripted Arrays Multiple subscripted arrays –Tables with rows and columns ( m by n array) –Like matrices: specify row, then column Initialization –int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } }; –Initializers grouped by row in braces –If not enough, unspecified elements set to zero int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } }; Referencing elements –Specify row, then column printf( "%d", b[ 0 ][ 1 ] );

54 Tip Referencing a double-subscripted array element as a[ x, y ] instead of a[ x ][ y ]. C interprets a[ x, y ] as a[ y ], and as such it does not cause a syntax error.

55 Double-subscripted array with three rows and four columns.

56 array1 is initialized with both rows full array2 and array3 are initialized only partially

57

58 Each row in the array corresponds to a single student’s set of grades

59 average function is passed a row of the array

60

61

62

63

64 Referance Ioannis A. Vetsikas, Lecture notes Dale Roberts, Lecture notes