EKT150 : Computer Programming

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Chapter 9: Arrays and Strings
1 ICS103 Programming in C Lecture 12: Arrays I. 2 Outline Motivation for One-dimensional Arrays What is a One-dimensional Array? Declaring One-dimensional.
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.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
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.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization 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.
ICS103 Programming in C Lecture 11: Arrays I
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
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.
Arrays.
Computer Programming for Engineers
Module 1: Array ITEI222 - Advance Programming Language.
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.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Arrays Chapter 7.
ARRAYS.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Chapter 8: Arrays Starting Out with C++ Early Objects Ninth Edition
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
EKT120 : Computer Programming
Arrays Declarations CSCI N305
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Lecture 7 – Arrays (1) PGT 106 : C PROGRAMMING.
Module 2 Arrays and strings – example programs.
Arrays in C.
Lecture 9 : Array Acknowledgment : Lecture notes from Ohio Supercomputing Center.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, For loop While loop Do while loop
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
C Arrays.
7 Arrays.
CNG 140 C Programming (Lecture set 8)
Declaration, assignment & accessing
Introduction To Programming Information Technology , 1’st Semester
EKT120: Computer Programming
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
7 Arrays.
Lecture 14: Problems with Lots of Similar Data
C++ Array 1.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
ICS103 Programming in C Lecture 12: Arrays I
Programming Fundamental
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Presentation transcript:

EKT150 : Computer Programming Lecture 7 – Arrays (1) UniMAP Sem I - 10/11 EKT150 : Computer Programming

EKT150 : Computer Programming Outline 1. Introduction 2. Arrays of Data 3. Array Declaration 4. Array Initialization 5. Operations on Array 6. Multidimensional Arrays 7. Index out of bound UniMAP Sem I - 10/11 EKT150 : Computer Programming

EKT150 : Computer Programming What is an Array? The variables that we have used so far have all common characteristics: Each variable could only store a single value at a time. Example: int count, length, num; double average, total; char selection, choice; An array is a collection of a fixed number of components wherein all of the components are of the same type UniMAP Sem I - 10/11 EKT150 : Computer Programming

What is an Array? (Example) Example: Let us work with a list of five integers: 5, 10, 15, 20, and 25. Previously we would declare five variables: int num1, num2, num3, num4, num5; By using array, since they are all of the same data type, we could just write: int num[5]; UniMAP Sem I - 10/11 EKT150 : Computer Programming

What is an Array? (Example) num 5 components or elements in this array. Elements are referred to index. Element num[2] has index 2 and value 15. 5 10 15 20 25 num[0] num[1] num[2] num[3] num[4] UniMAP Sem I - 10/11 EKT150 : Computer Programming

EKT150 : Computer Programming Arrays of Data Engineering applications usually involve large chunk of data (of common type) Arrays provide easy and efficient concept for data storage or management Arrays are usually processed through loops (processing is very common) Arrays are accessed by indicating an address or index UniMAP Sem I - 10/11 EKT150 : Computer Programming

EKT150 : Computer Programming Arrays in C Arrays can assume any type (including the primitive data types) int, char, string, double, float, etc. Like any other instances, arrays must be declared before use. UniMAP Sem I - 10/11 EKT150 : Computer Programming

EKT150 : Computer Programming Array Declaration Format: data_type array_name[int value]; Example: int list[5]; const int Max_List_Size = 10; int hours[Max_List_Size]; const int SIZE = 100; double amount[SIZE]; const int Max_List_Size = 6; char alphas[Max_List_Size]; #define N 10 double b[N]; UniMAP Sem I - 10/11 EKT150 : Computer Programming

Multiple Instances vs. Array int value1, value2, value3; printf (“Enter first value: “); scanf (“%d”, &value1); printf(“Enter second value: “); scanf(“%d”, &value2); printf (“Enter third value: “); scanf(“%d”, &value3); // process or display // array int valueArray[3]; for(int count=0;count<3;count++) { printf (“Enter value : ”); printf (“%d : ”, count+1); scanf (“%d”, &valueArray[count]); } // process or display UniMAP Sem I - 10/11 EKT150 : Computer Programming

Arrays - Memory Allocation Arrays are allocated bulk memory Single reference used for multiple locations Items are accessed based on index (address) with reference to first item UniMAP Sem I - 10/11 EKT150 : Computer Programming

EKT150 : Computer Programming Arrays Arithmetic Operations on arrays are similar to that on basic variables. sum = num[0] + num[1] + num[2] + num[3]; mult = 3 * num[1]; remainder = num[3] % 3; total = num[1] * num[2]; UniMAP Sem I - 10/11 EKT150 : Computer Programming

EKT150 : Computer Programming Array Initialization Arrays can be initialized directly, but assignments are done using loops Like any other simple variable, arrays can also be initialized while they are being declared. double sales[5] = {12.25, 32.50, 16.90, 23, 45.68}; sales[0]=12.25, sales[1]= 32.50, sales[2]=16.90, sales[3]= 23.00, sales[4]=45.68; UniMAP Sem I - 10/11 EKT150 : Computer Programming

Array Initialization (cont…) Initializers: If not enough initializers, rightmost element becomes 0 int n[ 7 ] = { 1, 2, 3, 4, 5 }; => n[5] = n[6] = 0 All elements = 0 int n[ 5 ] = { 0 } ; ▪ If size is omitted, initializers determine the size int n[ ] = { 1, 2, 3, 4, 5 }; 5 initializers, therefore 5 element array UniMAP Sem I - 10/11 EKT150 : Computer Programming

EKT150 : Computer Programming Sample Program Initializes the first 2 elements of the a[]array. All the other elements are then automatically set to zero Because no array size is given (the brackets are empty) and three values are given in braces, the array is automatically declared to have a size of 3 with the value shown being the initial element values. #include <stdio.h> int main() { int a[3]= {11,22}, b[]={44, 55, 66},i; double x[2],y[10]; printf("a[0]=%2d, a[1]=%2d, a[2]=%2d \n" "b[0]=%2d, b[1]=%2d, b[2]=%2d \n\n", a[0],a[1],a[2],b[0],b[1],b[2]); printf("Please enter two real numbers\n"); scanf("%lf %lf",&x[0], &x[1]); printf("x[0] = %.1lf x[1] = %.1lf\n\n", x[0], x[1]); for (i=0;i<10;i++) y[i]= i*100.0; printf("y[%1d]=%.2lf\n", i, y[i]); } return 0; Using a loop to fill all the elements of the y[] array. UniMAP Sem I - 10/11 EKT150 : Computer Programming

EKT150 : Computer Programming Sample Program Output: a[0]=11, a[1]=22, a[2]= 0 b[0]=44, b[1]=55, b[2]=66 Please enter two real numbers 77.0 88.0 x[0] = 77.0 x[1] = 88.0 y[0]=0.00 y[1]=100.00 y[2]=200.00 y[3]=300.00 y[4]=400.00 y[5]=500.00 y[6]=600.00 y[7]=700.00 y[8]=800.00 y[9]=900.00 UniMAP Sem I - 10/11 EKT150 : Computer Programming

Array Initialization During Declaration When declaring and initializing arrays, it is not necessary to specify the size of the array. The size of the array is determined by the number of initial values in the braces. double sales[] = {12.25, 32.50, 16.90, 23, 45.68}; UniMAP Sem I - 10/11 EKT150 : Computer Programming

EKT150 : Computer Programming A simple example The program declares and initializes the array y. It uses a ‘for’ loop with index i to access the successive elements of y. For each loop iteration, the value accessed id is added to the variable total which is finally displayed. Note that the loop index i starts from 0 to 4 (not from 1 to 5). Also, note that the array size n is declared in the define statement. UniMAP Sem I - 10/11 EKT150 : Computer Programming

A simple example (cont..) #include<stdio.h> #define n 5 // define number of n in the array void main() { int i, total = 0; // variable declaration int y[n]={9,6,20,5,12}; // array declaration and // initialization for (i=0;i<n;i++) total = total + y[i]; printf ("\nTotal = %d\n”, total); } UniMAP Sem I - 10/11 EKT150 : Computer Programming

EKT150 : Computer Programming Notes The defined constants, #define is used to ease any future amendments of the codes, for instance, if the array is to be widen to an n of 10 instead of 5, it would be adequate by modifying the line: #define n 5  #define n 10 there is no need to make any other changes to the program, thus making the life of programmer easier. UniMAP Sem I - 10/11 EKT150 : Computer Programming

EKT150 : Computer Programming Operations on Array Reading data in an array for (index = 0; index < 10; index++) scanf (“%d”, &sale[index]); Printing an array printf (“%d ”, sale[index]); UniMAP Sem I - 10/11 EKT150 : Computer Programming

EKT150 : Computer Programming Parallel Arrays Two (or more) arrays are called parallel if their corresponding components hold related information. int student_Id[50]; char student_Grade[50]; UniMAP Sem I - 10/11 EKT150 : Computer Programming

Multi-Dimensional Arrays Arrays can have multiple dimensions Most used is the 2-dimensional array (for matrix implementation) Actual implementation is a single array (segmented) Nested loop structure usually used to access items UniMAP Sem I - 10/11 EKT150 : Computer Programming

2-Dimensional Array (Example) UniMAP Sem I - 10/11 EKT150 : Computer Programming

Multi-Dimensional Arrays (cont..) A collection of the same type of data stored in contiguous and increasing memory locations. Declaration of multi-dimensional array: int b[2][3] = {51, 52, 53, 54, 55, 56}; array_type array_name Array dimension = 2 two rows three columns first row initial values second row initial values UniMAP Sem I - 10/11 EKT150 : Computer Programming

Multi-Dimensional Arrays (cont..) Multi-dimensional array can be initialized directly in the declaration statement. For example: int b[2][3] = {51, 52, 53, 54, 55, 56}; which initializes the elements to be b[0][0] = 51 b[0][1] = 52 b[0][2] = 53 b[1][0] = 54 b[1][1] = 55 b[1][2] = 56 * note that C begins its subscripts at 0. The rightmost subscript is incremented first. UniMAP Sem I - 10/11 EKT150 : Computer Programming

Multi-Dimensional Arrays (cont..) can use braces ({ }) to separate rows in 2-dimensional arrays. For example: int c [4][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10,11,12}}; int c [4][3] = {{1, 2}, {4, 5, 6}, {7}, initializes c[0][2], c[2][1] and c[2][2] to be zero int c [ ][3] = {{1, 2, 3}, implicitly declares the number of rows to be 4 3 columns 4 rows rows columns UniMAP Sem I - 10/11 EKT150 : Computer Programming

EKT150 : Computer Programming Notes on Arrays Arrays enable better and easier data management system Closely related to loops Indexing is zero-based (0 to n-1 for an array with n locations) Multi-dimensional arrays require nested loop structure (e.g. 2-dimensional array) UniMAP Sem I - 10/11 EKT150 : Computer Programming

EKT150 : Computer Programming Index out of bounds ‘Out of bounds’ is when (index < 0) or (index > arraySize - 1) It is a run-time error, happens when an index is outside the valid boundaries of the array. Example: int a[10]; int x = 10 a[9] = 3 ; //ok a[x] = 4 ; //10 is not within the range 0..9 UniMAP Sem I - 10/11 EKT150 : Computer Programming

EKT150 : Computer Programming Index out of bound In C, no guard against this problem Does not check whether index value is within range or not Can result in accessing data of wrong memory location UniMAP Sem I - 10/11 EKT150 : Computer Programming

EKT150 : Computer Programming How to overcome? Use defined loops for (i = 0; i < 10; i ++) list [ i ] = 0; UniMAP Sem I - 10/11 EKT150 : Computer Programming

EKT150 : Computer Programming End – Arrays (1) Q & A! UniMAP Sem I - 10/11 EKT150 : Computer Programming