Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Chapter 7: Arrays In this chapter, you will learn about
Programming and Data Structure
Kernighan/Ritchie: Kelley/Pohl:
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
11.5 SORTING ARRAY Sorting is the process of transforming a list into an equivalent list, in which the elements are arranged in ascending or descending.
© 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.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Introduction to C Programming CE Lecture 9 Data Structures Arrays.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Programming Arrays. Question Write a program that reads 3 numbers from the user and print them in ascending order. How many variables do we need to store.
Java Unit 9: Arrays Declaring and Processing Arrays.
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.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
ARRAY Prepared by MMD, Edited by MSY1.  Introduction to arrays  Declaring arrays  Initializing arrays  Examples using arrays  Relationship with pointers.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
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.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Chapter 8 Arrays Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 3.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
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.
CHAPTER 7: Arrays CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (Feb 2012)
Prepared by MMD, Edited by MSY1 CHAPTER 4 ARRAY. Prepared by MMD, Edited by MSY2 Arrays  Introduction to arrays  Declaring arrays  Initializing arrays.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
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.
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.
SORTING Sorting is storage of data in some order, it can be in ascending or descending order. The term Sorting comes along-with the term Searching. There.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays + Functions Outline 6.5Passing Arrays to Functions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
Windows Programming Lecture 03. Pointers and Arrays.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Computer Programming BCT 1113
Lecture 7 Arrays 1. Concept of arrays Array and pointers
Hassan Khosravi / Geoffrey Tien
Numeric Arrays Numeric Arrays Chapter 4.
Object Oriented Programming COP3330 / CGS5409
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
7 Arrays.
Arrays Outline Introduction Arrays Declaring Arrays
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Review of Arrays and Pointers
Data Structures (CS212D) Week # 2: Arrays.
7 Arrays.
Presentation transcript:

Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function Simple sorting: bubble sort Simple searching: linear sort 2-dimensional array

Introduction to Arrays An array is a group of memory locations related by the fact that they all have the same name and the same type. To refer to a particular location or element in the array, we specify the name of the array and the position number of the particular element in the array. The size of an array is static (fixed) throughout program execution.

Let say we have an array called a a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] Name of the array Position number of the element within the array The position number within the square brackets is formally called a subscript. A subscript can be an integer or an integer expression. For example if x = 1 and y = 2, then a[x+y] is equal to a[3]. Notice that the position starts from 0.

Declaring Arrays Array declaration is made by specifying the data type and the number of space so that the computer may reserve the appropriate amount of memory. General syntax: data_type array_name[size] Examples: –int my_array[100]; –char name[20]; –double bigval[5*200]; –int a[27], b[10], c[76];

Initializing Arrays There are 2 ways to initialize an array: during compilation and during execution. During compilation: –arr[] = {1, 2, 3, 4, 5}; We can define however many elements that we want since the array size is not given. –arr[3] = {90, 21, 22}; We can define only 3 elements since the array size is already given. During execution: int arr[3], j; for (j = 0; j < 3; j++) arr[j] = 0;

Examples Using Arrays #include #define SIZE 5 void main(void) { int temp[SIZE], i; for (i = 0; i < SIZE; i++) temp[i] = i*9; printf(“%s %13s\n”, “Element”, “Value”); for (i = 0; i < SIZE; i++) printf(“%7d%13d\n”, i, temp[i]); } Output: ElementValue In the example above, an array with the name temp and size 5 has been declared. The elements in the array has been given the value position*9. The first for loop is equivalent to this: temp[0] = 0*9, temp[1] = 1*9, temp[2] = 2*9, temp[3] = 3*9, temp[4] = 4*9

#include #define SIZE 10 void main(void) { int i = 0, list[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9}, total = 0; for (; i < SIZE; i++) { total += list[i]; } printf(“Total of array element values is %d\n”, total); } Output: Total of array element values is 45

Relationship with Pointers The name of an array is actually a pointer to the first element in the array. Therefore, if we have: int test[3] = {9, 10, 11}; printf(“%d”, *test); The output would be: 9 There are a few ways to traverse an array: int test[3] = {9, 10, 11}, k; for (k = 0; k < 3; k++) printf(“%d\n”, test[k]); int test[3] = {9, 10, 11}, k; int *ptr = test; for (k = 0; k < 3; k++, ptr++) printf(“%d\n”, *ptr);

Array Passing to a Function A function that is going to receive an array as one of the arguments can be declared in 2 ways: void Process(char name[]) OR void Process(char *name) When we pass an array to a function, we are actually passing the pointer to the first element in the array to the function. Therefore, any changes to the array inside the function will also change the actual array. Either the array is passed using [] or using *, the array can be accessed in the same way.

#include #define SIZE 10 void Read(int []); int CountAvg(int *); void main(void) { int grades[SIZE]; Read(grades); printf(“The average of the grades given is %d\n”, CountAvg(grades)); } void Read(int grades[]) { int i, temp; for (i = 0; i < SIZE; i++) { printf(“Enter grade %d\n”, i); scanf(“%d”, temp) grades[i] = temp; } int CountAvg(int *grades) { int i, total; for (i = 0, total = 0; i < SIZE; i++) total += grades[i]; return (total/SIZE); }

The Read() and CountAvg() functions can also be written this way: void Read(int grades[]) { int i, temp; int *ptr = grades; for (i = 0; i < SIZE; i++, ptr++) { printf(“Enter grade %d\n”, i); scanf(“%d”, temp) *ptr = temp; } int CountAvg(int *grades) { int i, total; int *ptr = grades; for (i = 0, total = 0; i < SIZE; i++, ptr++) total += *ptr; return (total/SIZE); }

Simple Sorting Sorting is the process of placing data into a particular order such as ascending or descending. There are many sorting algorithms that are usually used. Among them are bubble sort, selection sort, insertion sort and shell sort. Here, we will discuss how bubble sort can be used to sort the elements in an array.

Bubble Sort We are given a list of 5 numbers: int list[] = {34, 53, 21, 23, 4}; Sort the numbers in an ascending order using bubble sort. Pass 1: Do not swap Swap

Pass 2: Swap

Pass 3: Do not swap Swap

Pass 4: Final result: Swap

Let us look at the C code: void main(void) { int list[] = {34, 53, 21, 23, 4}; BubbleSort(list); } void BubbleSort (int list[]) { int i, j, temp, swapped = 1; for (i = 0; i < 5 && swapped == 1; i++) { swapped = 0; for (j = 0; j < (5-i); j++) { if (list[j] > list[j+1]) { // Swap temp = list[a]; list[a] = list[a+1]; list[a+1] = temp; swapped = 1; }

Simple Searching Searching is the process of determining whether an array contains a value that matches a certain key value. Same as in sort, there are more than one algorithms that can be used to do a search. Here, we will discuss how can we do a linear search on an array. Linear search is a simple searching algorithm where: –data are stored in an array –a key value is compared with each elements in the array starting from the first element

void main(void) { int list[] = {34, 53, 21, 23, 4}; int i, key_value, found = 0; printf(“Enter the number that you want to find: ”); scanf(“%d”, &key_value); for (i = 0; i < 5; i++) { if (list[i] == key_value) { found = 1; printf(“The number %d is found at location %d\n”, key_value, i); break; } if (found == 0) printf(“The number %d cannot be found in the list\n”, key_value); }

2-Dimensional Array It is possible to create an array which has more than one dimension. For example: –2D array: int array[4][2]; –3D array: int array[4][2][10]; Graphical representation of a 2D array: int myarray[4][2] = {1, 2, 3, 4, 5, 6, 7, 8}; This array has 4 rows and 2 columns.

Variable initialization can also be done this way: – int myarray[4][2] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}}; This method is less confusing since we can see the rows and columns division more clearly. To initialize a 2D array during execution, we need to use a double loop: for (i = 0; i < 4; i++) { for (j = 0; j < 2; j++) myarray[i][j] = 0; } Although it is possible to create a multi- dimensional array, arrays above 2-dimensions are rarely used.

When a 2D (or higher dimensional) array is passed to a function, the size of the second (or subsequent) subscript needs to be specified. For example, if we have: int twoD[4][5]; Then a function which would take twoD as an argument should be declared like this: void Process2D(int td[][5]) An array is stored consecutively in memory regardless of the number of dimensions. Therefore, specifying the subscripts in the function parameter will help the compiler to know the boundary of the different dimensions.