CHAPTER 7: Arrays CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (Feb 2012)

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Chapter 7: Arrays In this chapter, you will learn about
Arrays.
CS 141 Computer Programming 1 1 Arrays. Outline  Introduction  Arrays  Declaring Arrays  Examples Using Arrays  Sorting Arrays  Multiple-Subscripted.
Chapter 7: User-Defined Functions II
Kernighan/Ritchie: Kelley/Pohl:
Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
 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 9: Arrays and Strings
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
1 Chapter 8 Functions, Pointers, and Storage Classes  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
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.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Java Unit 9: Arrays Declaring and Processing Arrays.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Chapter 8 Arrays and Strings
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
ARRAY Prepared by MMD, Edited by MSY1.  Introduction to arrays  Declaring arrays  Initializing arrays  Examples using arrays  Relationship with pointers.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
1. 1. Introduction to Array 2. Arrays of Data 3. Array Declaration 4. Array Initialization 5. Operations on Array 6. Multidimensional Arrays 7. Index.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
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.
Prepared by MMD, Edited by MSY1 CHAPTER 4 ARRAY. Prepared by MMD, Edited by MSY2 Arrays  Introduction to arrays  Declaring arrays  Initializing arrays.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
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.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Arrays. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Windows Programming Lecture 03. Pointers and Arrays.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
Numeric Arrays Numeric Arrays Chapter 4.
Chapter 10: Pointers 1.
7 Arrays.
Lecture 18 Arrays and Pointer Arithmetic
7 Arrays.
Arrays Arrays A few types Structures of related data items
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
C++ Array 1.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Presentation transcript:

CHAPTER 7: Arrays CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (Feb 2012)

Topics 1.Introduction to Arrays 2.Operations on Arrays: a)Declaring arrays b)Initializing arrays c)Assigning values to array elements d)Reading values from array elements e)Passing arrays to a function 3.2 Dimensional (2D) Arrays 2BS (Feb 2012)

INTRODUCTION TO ARRAYS Topic 1 BS (Feb 2012)3

What are arrays? In C, a group of items of the same type can be set up using array. An array is a group of consecutive memory locations related by the fact that they all have the same name and the same type. The compiler must reserve storage (space) for each element/item of a declared array. The size of an array is static (fixed) throughout program execution. To refer to a particular location or element in the array, we specify the name of the array (index or subscript) and the position number of the particular element in the array. BS (Feb 2012)4

Array: Conceptual View (1) BS (Feb 2012)5 score[0] ? Index (subscript) // C array declarations // An array of 4 elements with // no value. int score[4] A subscript can be an integer or an integer expression. For example if x = 1 and y = 2, then score[x+y] is equal to score[3]. Starts with zero Name of the array ??? score[1]score[2]score[3]

Array: Conceptual View (2) BS (Feb 2012)6 score[0] ? Index (subscript) Name of the array ? ? ? score[1] score[2] score[3] // C array declarations // An array of 4 // elements with no value. int score[4]

Array: Conceptual View (3) BS (Feb 2012)7 [0] ? Index (subscript) // C array declarations // An array of 4 elements with no value. int score[4] Name of the array ??? [1][2][3] score

OPERATIONS ON ARRAYS Topic 2 BS (Feb 2012)8

1. Declaring Arrays Array declaration is made by specifying the data type, it’s name and the number of space (size) so that the computer may reserve the appropriate amount of memory. General syntax: Examples: – int my_array[100]; – char name[20]; – int a[27], b[10], c[76]; – double bigval[5*200]; BS (Feb 2012)9 data_type array_name[size]; #define SIZE 20 double test_score[SIZE]; int tax[SIZE+2]; #define SIZE 20 double test_score[SIZE]; int tax[SIZE+2]; expression Constant

2. Initializing Arrays (1) After an array is declared, it must be initialized. 2 ways to initialize an array: compile-time and run-time 1.Compile-time, example: int age[ ] = {1, 2, 3, 4, 5}; int age[3] = {90, 21, 22}; BS (Feb 2012)10 Initialize as many elements as we want. The array size is not specified age[0] age[1] age[2] age[0] age[1] age[2] age[3] age[4] 5 4

2. Initializing Arrays (2) BS (Feb 2012)11 char init[5] = {‘a’,’d’}; int age[5] = {0}; char ans[4] = {0}; aNULLd init age [0] [1] [2] [3] [4] NULL ans [0] [1] [2] [3] Initialize all array elements to zero. [0] [1] [2] [3] [4] Initialize the first two elements to the value of a and d respectively, while the other elements are initialized to NULL for char or zero for numeric identifier.

2. Initializing Arrays (3) 2. Run-time, example: Using for loop to initialize the array elements BS (Feb 2012)12 ????? salary [0] [1] [2] [3] [4] 0.0 salary [0] [1] [2] [3] [4] double salary[5]; for (i = 0; i < 5; i++) salary[i] = 0.0; // scanf(“%d”, &salary[i] is accepted. /*end for */ double salary[5]; for (i = 0; i < 5; i++) salary[i] = 0.0; // scanf(“%d”, &salary[i] is accepted. /*end for */

3. Assigning Values to Array Elements 1.You can assign values to array elements by using the for statement (use the same example of initializing arrays during run-time) 2.You can also assign a value to a specific array element by using its index number. – Example: let’s say we have an array that represent the number of people staying in 5 unit apartments. int apartment[5]={3,2,6,4,5}; – The above initialization indicates that there are 3 people living in apartment[0], 2 people living in apartment[1] and so on. – To assign new value to apartment[3], you may use this statement: apartment[3] = 8; apartment[3] = apartment[3] + 2 BS (Feb 2012)13

4. Reading Values of Array Elements To read a value from a specific array element, refer to the index. For example, let’s say we want to know how many people leaving in apartment[3], we could simply do this: Output: BS (Feb 2012)14 int apartment[5] = {3,2,6,4,5}; int nop; nop = apartment[3]; printf(“Apartment 3 has %d people.”, nop); int apartment[5] = {3,2,6,4,5}; int nop; nop = apartment[3]; printf(“Apartment 3 has %d people.”, nop); Apartment[3] has 4 people.

Example (1) BS (Feb 2012)15 #include #define SIZE 5 void main(void) { int apartment[SIZE] = {3,2,6,4,5}; int index, total = 0; for (index = 0; index < SIZE; index++) { total = total + apartment[index]; } printf("There are total of %d inhabitants",total); } #include #define SIZE 5 void main(void) { int apartment[SIZE] = {3,2,6,4,5}; int index, total = 0; for (index = 0; index < SIZE; index++) { total = total + apartment[index]; } printf("There are total of %d inhabitants",total); } Output?

Example (2) #include void main(void) { int apartment[5] = {3,2,6,4,5}; int index, total = 0; printf("Apt No\t\tNo of people"); for (index = 0; index < 5; index++) { printf(“%d\t\t%d\n",index, apartment[index]); } #include void main(void) { int apartment[5] = {3,2,6,4,5}; int index, total = 0; printf("Apt No\t\tNo of people"); for (index = 0; index < 5; index++) { printf(“%d\t\t%d\n",index, apartment[index]); } BS (Feb 2012)16 Output?

5. Passing Arrays to Functions(2) 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 inside the calling function. When we want to pass an array to a function, we need to know these 3 things. – How to write the function prototype? – How to do function call? – How does the function header would look like? BS (Feb 2012)17

5. Passing Arrays to Functions(2) Assume that we have the following array declaration. int score[3]={45,90,14}; Say for example we want to write a function, called ReadScore(), which will read marks/scores from the user and store the marks inside the score array. Perform these steps: – Function prototype - indicate that the parameter is an array. Example: void ReadScore(int s[]); – Function call: int score[3]={45,90,14}; //Assume we have this ReadScore(score); //declaration – Function definition : void ReadScore(int s[]){ } BS (Feb 2012)18 with NO array size information.

Example (1) #include void test(int s[]); void main(void) { int x[3]={9,7,6}; test(x); } void test(int s[]) { printf("%d\n",s[0]); printf("%d\n",s[1]); } #include void test(int s[]); void main(void) { int x[3]={9,7,6}; test(x); } void test(int s[]) { printf("%d\n",s[0]); printf("%d\n",s[1]); } BS (Feb 2012)19 Output?

Example(2) BS (Feb 2012)20 #include #define size 5 void getMarks(float s[ ]); float calcAverage(float sc[ ]); void main(void) { float marks[size] = {0.0}; //initializing the array getMarks(marks); /* function call */ printf("Average for marks given is %.2f\n“, calcAverage(marks)); } #include #define size 5 void getMarks(float s[ ]); float calcAverage(float sc[ ]); void main(void) { float marks[size] = {0.0}; //initializing the array getMarks(marks); /* function call */ printf("Average for marks given is %.2f\n“, calcAverage(marks)); } void getMarks(float s[ ]) { int i; for (i = 0; i < size; i++) { printf("Marks student %d:",i + 1); scanf("%f",&s[i]); } void getMarks(float s[ ]) { int i; for (i = 0; i < size; i++) { printf("Marks student %d:",i + 1); scanf("%f",&s[i]); } float calcAverage(float sc[ ]) { float total = 0.0; int i; for (i = 0; i < size; i++) { total = total + sc[i]; } return (total / size); } float calcAverage(float sc[ ]) { float total = 0.0; int i; for (i = 0; i < size; i++) { total = total + sc[i]; } return (total / size); }

2 DIMENSIONAL (2D) ARRAYS Topic 3 BS (Feb 2012)21

2D Arrays It is possible to create an array which has more than one dimension. In C, we can go up to 12-dimensional arrays. 2D arrays: – Declaration: Example: BS (Feb 2012)22 Data_type array[row][column] int carrymarks[4][2] Row Column

2D: Conceptual View (1) BS (Feb 2012)23 int m[4][2] = {10, 12, 33, 14, 25, 56, 77, 85}; Column subscript Row subscript [0][1] [0] [1] [2] [3] Column Row This 2D array has 4 rows and 2 columns.

2D: Conceptual View (2) BS (Feb 2012)24 CSEB134 : BS/2008 int m[4][2] = {10, 12, 33, 14, 25, 56, 77, 85}; m[0][0]m[0][1] 1012 m[1][0]m[1][1] 3314 m[2][0]m[2][1] 2556 m[3][0]m[3][1] 7785 Column 0Column 1 Row 0 Row 1 Row 2 Row Row 0Row 1 m[0][0] m[0][1] m[1][0] m[1][1] m[2][0] m[2][1] m[3][0] m[3][1] Storage structure Row 2Row 3 This 2D array has 4 rows and 2 columns.

Initializing 2D Arrays (1) 2 ways to initialize an array: compile-time and run-time 1. Compile-time – Variable initialization can also be done this way: – This method is less confusing since we can see the rows and columns division more clearly. BS (Feb 2012)25 int m[4][2] = {{10,12},{33,14},{25,56},{77,85}};

Initializing 2D Arrays (2) 2. Run-time Using nested for statements: Although it is possible to create a multi-dimensional array, arrays above 2-dimensions are rarely used. BS (Feb 2012)26 for (row = 0; row < 4; row++) { for (column = 0; column < 2; column++) m[row][column] = 0; } for (row = 0; row < 4; row++) { for (column = 0; column < 2; column++) m[row][column] = 0; }

Passing a 2D Array to Functions 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 m[4][5]; – Then the function header which would take m 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. BS (Feb 2012)27

Summary 1.Introduction to Arrays 2.Operations on Arrays: a)Declaring arrays b)Initializing arrays c)Assigning values to array elements d)Reading values from array elements e)Passing arrays to a function 3.2 Dimensional (2D) Arrays – declaring, initializing and passing 2D array to function BS (Feb 2012)28