EKT120: 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.
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.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
A First Book of ANSI C Fourth Edition
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.
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.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
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.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
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.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Arrays in C. What is Array? The variables we have used so far can store a single value. Array is a new type of variable capable of storing many values.
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.
EGR 2261 Unit 10 Two-dimensional Arrays
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
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 7 – Arrays (1) PGT 106 : C PROGRAMMING.
Module 2 Arrays and strings – example programs.
Pointers and Arrays S.Bhuvaneshwari Assistant Professor/CSE
C Arrays.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
C Arrays.
7 Arrays.
CNG 140 C Programming (Lecture set 8)
Arrays Kingdom of Saudi Arabia
EKT150 : Computer Programming
Declaration, assignment & accessing
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Introduction To Programming Information Technology , 1’st Semester
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
7 Arrays.
Arrays Arrays A few types Structures of related data items
Lecture 14: Problems with Lots of Similar Data
C++ Array 1.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
ICS103 Programming in C Lecture 12: Arrays I
Programming Fundamental
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Presentation transcript:

EKT120: Computer Programming Week 8 Arrays (Part 1)

EKT120 : Computer Programming Outline Introduction Arrays of Data Array Declaration Array Initialization Operations on Array Multidimensional Arrays Index out of bound

EKT120 : 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 iCount, iLength, iNum; double dAverage, dTotal; char cSelection, cChoice; An array is a collection of a fixed number of components wherein all of the components are of the same type

What is an Array? (Example) EKT120 : 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 iNum1, iNum2, iNum3, iNum4, iNum5; By using array, since they are all of the same data type, we could just write: int aiNum[5];

What is an Array? (Example) EKT120 : Computer Programming What is an Array? (Example) aiNum 5 components or elements in this array. Elements are referred to index. Element aiNum[2] has index 2 and value 15. 5 10 15 20 25 aiNum[0] aiNum[1] aiNum[2] aiNum[3] aiNum[4]

EKT120 : 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

EKT120 : 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.

EKT120 : Computer Programming Array Declaration Format: data_type array_name[int value]; Example: int aiList[5]; const int Max_List_Size = 10; int aiHours[Max_List_Size]; const int SIZE = 100; double adAmount[SIZE]; const int Max_List_Size = 6; char acAlphas[Max_List_Size]; #define N 10 double adB[N];

Multiple Instances vs. Array EKT120 : Computer Programming Multiple Instances vs. Array // multiple instance int iValue1, iValue2, iValue3; printf (“Enter first value: “); scanf (“%d”, &iValue1); printf(“Enter second value: “); scanf(“%d”, &iValue2); printf (“Enter third value: “); scanf(“%d”, &iValue3); // process or display // array int aiValue[3]; for(int iCount=0; iCount<3; iCount++) { printf (“Enter value : ”); scanf (“%d”, &aiValue[iCount]); } // process or display

Arrays - Memory Allocation EKT120 : 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 int aiValue[8]; aiValue[0]=23; aiValue[1]=56; aiValue[2]=100; aiValue[3]=0; aiValue[4]=12; aiValue[5]=234; aiValue[6]=666; aiValue[7]=4; index aiValue 1 2 3 4 5 6 7 23 56 100 12 234 666 4

EKT120 : Computer Programming Arrays Arithmetic Operations on arrays are similar to that on basic variables. iSum = aiNum[0] + aiNum[1] + aiNum[2] + aiNum[3]; iMult = 3 * aiNum[1]; iRemainder = aiNum[3] % 3; iTotal = aiNum[1] * aiNum[2];

EKT120 : 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 adSales[5] = {12.25, 32.50, 16.90, 23, 45.68}; adSales[0]=12.25, adSales[1]=32.50, adSales[2]=16.90, adSales[3]=23.00, adSales[4]=45.68;

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

EKT120 : Computer Programming Sample Program Initializes the first 2 elements of the aiA[]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 aiA[3]= {11,22}, aiB[]={44, 55, 66},iLoop; double adX[2],adY[10]; printf(“aiA[0]=%2d, aiA[1]=%2d, aiA[2]=%2d \n" “aiB[0]=%2d, aiB[1]=%2d, aiB[2]=%2d \n\n", aiA[0],aiA[1],aiA[2],aiB[0],aiB[1],aiB[2]); printf("Please enter two real numbers\n"); scanf("%lf %lf",&adX[0], &adX[1]); printf(“adX[0] = %.1lf adX[1] = %.1lf\n\n", adX[0], adX[1]); for (iLoop=0;iLoop<10;iLoop++) adY[iLoop]= iLoop*100.0; printf(“adY[%1d]=%.2lf\n", iLoop, adY[i]); } return 0; Using a loop to fill all the elements of the adY[] array.

EKT120 : Computer Programming Sample Program Output: aiA[0]=11, aiA[1]=22, aiA[2]= 0 aiB[0]=44, aiB[1]=55, aiB[2]=66 Please enter two real numbers 77.0 88.0 adX[0] = 77.0 adX[1] = 88.0 adY[0]=0.00 adY[1]=100.00 adY[2]=200.00 adY[3]=300.00 adY[4]=400.00 adY[5]=500.00 adY[6]=600.00 adY[7]=700.00 adY[8]=800.00 adY[9]=900.00

Array Initialization During Declaration EKT120 : 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 adSales[] = {12.25, 32.50, 16.90, 23, 45.68};

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

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

EKT120 : 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.

EKT120 : Computer Programming Operations on Array Reading data in an array for (iIndex = 0; iIndex < 10; iIndex++) scanf (“%d”, &aiSale[iIndex]); Printing an array printf (“%d ”, aiSale[iIndex]);

EKT120 : Computer Programming Parallel Arrays Two (or more) arrays are called parallel if their corresponding components hold related information. int aiStudentId[50]; char acStudentGrade[50];

Multi-Dimensional Arrays EKT120 : 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

2-Dimensional Array (Example) EKT120 : Computer Programming 2-Dimensional Array (Example) int aiValue[4][2]; aiValue[2][1]=5; index aiValue 1 2 3 4 5 6 7 5 Row 0 Row 1 Column 0 1 1 2 3 5 Row Address Resolution = Row*(MaxCol) + Col

Multi-Dimensional Arrays (cont..) EKT120 : 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 aiB[2][3] = {51, 52, 53, 54, 55, 56}; array_type array_name Array dimension = 2 two rows second row initial values three columns first row initial values

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

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

EKT120 : 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)

EKT120 : 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 aiA[10]; int iX = 10 aiA[9] = 3 ; //ok aiA[iX] = 4 ; //10 is not within the range 0..9

EKT120 : 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

EKT120 : Computer Programming How to overcome? Use defined loops for (iLoop = 0; iLoop < 10; iLoop ++) aiList [ iLoop ] = 0;

EKT120: Computer Programming End Week 8 Q & A! UniMAP Sem II – 12/13 NI- EKT 120/4 (SEM 2 2012/2013)