1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.

Slides:



Advertisements
Similar presentations
CHAPTER 10 ARRAYS II Applications and Extensions.
Advertisements

TK1914: C++ Programming Array II. Objective In this chapter you will explore how to manipulate data in a two-dimensional array. 2FTSM :: TK1914,
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
1 Lecture 21:Arrays and Strings(cont.) Introduction to Computer Science Spring 2006.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Introduction to Programming with C++ Fourth Edition
C++ for Engineers and Scientists Third Edition
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.
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006.
Arrays.
Arrays One-Dimensional initialize & display Arrays as Arguments Two-dimensional initialize & display Part I.
Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Arrays Multi-dimensional initialize & display Sample programs Sorting Searching Part II.
Chapter 8 Arrays and Strings
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
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.
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 9: Arrays J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second Edition.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
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)
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
Arrays Multi-dimensional initialize & display Sorting Part II.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
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.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 8 Arrays.
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Arrays.
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
Module 1: Array ITEI222 - Advance Programming Language.
Opening Input/Output Files ifstream infile; ofstream outfile; char inFileName[40]; char outFileName[40]; coutinFileName;
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
Arrays. Arrays are objects that help us organize large amounts of information.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
1 Why do we need arrays? Problem - Input 5 test scores => int test1,test2,test3,test4,test5 100 test scores? 10,000 employees? A structured data type is.
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.
1 Arrays and Variable Length Parameter List  The syntax to declare a variable length formal parameter (list) is: dataType... identifier.
Chapter 9: Arrays J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
EGR 2261 Unit 10 Two-dimensional Arrays
EGR 2261 Unit 9 One-dimensional Arrays
Chapter 8: Arrays Starting Out with C++ Early Objects Ninth Edition
Computer Programming BCT 1113
Basic Array Definition
Array Data Structure Chapter 6
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Lecture 9 Objectives Learn about arrays.
Array Data Structure Chapter 6
Dr. Khizar Hayat Associate Prof. of Computer Science
Presentation transcript:

1 Topic: Array Topic: Array

2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data into arrays Explore how to declare and manipulate data into arrays Become familiar with the restrictions on array processing Become familiar with the restrictions on array processing Discover how to pass an array as a parameter to a function Discover how to pass an array as a parameter to a function Example of 1-D array Example of 1-D array Discover how to manipulate data in a two-dimensional array Discover how to manipulate data in a two-dimensional array Example of 2-D array Example of 2-D array Homework Homework

3 A data type is called simple if variables of that type can store only one value at a time. A data type is called simple if variables of that type can store only one value at a time. A structured data type (or data structure) is one in which each data item is a collection of other data items. A structured data type (or data structure) is one in which each data item is a collection of other data items. An array is a collection of a fixed number of components wherein all of the components are of the same data type.  A group of same data type is called array.  An array is of three types: a)One Dimensional array a)One Dimensional array b)Two Dimensional array b)Two Dimensional array c)Multi Dimensional array c)Multi Dimensional array one-dimensional array is an array in which the components are arranged in a list form. The general form of declaring a one-dimensional array is: one-dimensional array is an array in which the components are arranged in a list form. The general form of declaring a one-dimensional array is: DataType arrayName[intExp];

4 Example: The statement int list[10]; declares an array list of 10 components. Each component is of the type int. The components are list[0], list[1] … list [9].

5 Accessing Array Components The general form (syntax) of accessing an array component is: arrayName[indexExp] where indexExp, called index, is any expression whose value is a nonnegative integer.  Index value specifies the position of the component in the array.  In C++, [] is an operator, called the array subscripting operator.  In C++ array index starts at 0.

6 list[5] = 34;

7 Processing One-Dimensional Arrays  Some of the basic operations performed on a one-dimensional array are initialize, input data, output data stored in an array, find the largest and/or smallest element.  Each of these operations requires the ability to step through the elements of the array.  Stepping-through the elements of an array is easily accomplished by a loop.

8 Consider a list declared as an array of the SIZE 100  The following for loop steps-through each element of the array list starting at the first element of list. for(i = 0; i < SIZE; i++) process list[i] process list[i]  If processing list requires inputting data into list, the statement in Line 2 takes the from of an input statement, such as the cin statement.  for(i = 0; i >list[i];

9 Finding the sum and average of an array: sum = 0; for(index = 0; index < SIZE; index ++) sum = sum + list[index]; average = sum / SIZE;

10 Array Problems  In C++, there is no guard against indices that are out of bounds.  Assignment, comparison of arrays, reading data into an array and printing the contents of an array must be done component-wise. cin>>x; //illegal if (x < y) …; //illegal y = x; // illegal  In order to copy one array into another array use a loop. for(j = 0; j < arraySize; j++) y[j] = x[j]; y[j] = x[j];

11 Arrays as Parameters to Functions  By Reference Only: In C++, arrays are passed by reference only.  Since arrays are passed by reference only, we do not use the symbol & when declaring an array as a formal parameter. void initialize(datatype list, int SIZE) { int count; for(count = 0; count < SIZE; count++) list[count] = 0; list[count] = 0;}

12 Example of 1D array To print the elements of a list in acsending order #include #include void bubble_sort(int a[], int N) { int i, j, temp; for (i = 0; i < N; i++) for (j = 0 ; j < N-1; j++) }

13 if (a[j] > a[j+1]) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; }

14 void main() { int A[10]; clrscr(); cout << "Enter the array elements : "; for (int i = 0; i<10; i++) cin >> A[i]; bubble_sort(A, 10); cout << "Sorted elements are : "; for (i = 0; i<10; i++) cout << A[i] << "\n"; }

15 Two-dimensional Arrays A two-dimensional array is a collection of a fixed number of components arranged in two dimensions, and all components are of the same type.The syntax for declaring a two-dimensional array is: A two-dimensional array is a collection of a fixed number of components arranged in two dimensions, and all components are of the same type.The syntax for declaring a two-dimensional array is: DataType arrayName[intexp1][intexp2]; where intexp1 and intexp2 are expressions yielding positive integer values. The two expressions intexp1 and intexp2 specify the number of rows and the number of columns, respectively, in the array. double sales[10][5]; declares a two-dimensional array sales of 10 rows and 5 columns and every component is of the type float.

16

17 Accessing Array Components The syntax to access a component of a two-dimensional array is arrayName[indexexp1][indexexp2] arrayName[indexexp1][indexexp2] where indexexp1 and indexexp2 are expressions yielding nonnegative integer values. indexexp1 specifies the row position and indexexp2 specifies the column position. The statement sales[5][3] = 25.75; stores into row numbered 5 and column numbered 3 (that is, 6th row and 4th column) of the array sales. If int i = 5; int j = 3; then the previous statement is equivalent to sales[i][j] = 25.75;

18

19 Processing Two-dimensional Arrays A two-dimensional array can be processed in three different ways. 1. Process the entire array. 2. Process a particular row of the array, called row processing. 3. Process a particular column of the array, called column processing. When processing a particular row or column of a two-dimensional array we employ algorithms similar to when we process one- dimensional arrays.

20 Suppose that we want to process row number, say 5 of matrix. The components of row number 5 of matrix are:

21 const int ROWS = 7; const int COLUMNS = 6; int matrix[ROWS][COLUMNS]; int row; int col; int sum; int largest; int temp; //In these components the first index is fixed at 5. row = 5; //The second index ranges from 0 to 5. for(col = 0; col < COLUMNS; col++) process matrix[row][col]

22 Similarly, suppose that we want to process column number 2 of matrix, that is, the third column of matrix. The components of this column are:

23 The second index is fixed at 2 and the first index ranges from 0 to 6. The second index is fixed at 2 and the first index ranges from 0 to 6. The following for loop processes column 2 of matrix. The following for loop processes column 2 of matrix. col = 2; for(row = 0; row < ROWS; row++) process matrix[row][col] process matrix[row][col]

24Print The following nested for loops print the components of matrix, one row per line. for(row = 0; row < ROWS; row++) { for(col = 0; col < COLUMNS; col++) for(col = 0; col < COLUMNS; col++) cout<<setw(5)<<matrix[row][col]<<" "; cout<<setw(5)<<matrix[row][col]<<" "; cout<<endl; cout<<endl;}

25 Passing Two-dimensional Arrays as Parameters to Functions Two-dimensional arrays can be passed as parameters to a function and they are passed by reference. Two-dimensional arrays can be passed as parameters to a function and they are passed by reference. When storing a two-dimensional array in the computer’s memory, C++ uses the row order form. That is, first the first row is stored, followed by the second row, which is followed by the third row and so on. When storing a two-dimensional array in the computer’s memory, C++ uses the row order form. That is, first the first row is stored, followed by the second row, which is followed by the third row and so on. void initialize(datatype table) {int row; int col; int col; for(row = 0; row < ROWS; row++) for(col = 0; col < COLUMNS; col++) for(col = 0; col < COLUMNS; col++) table[row][col] = 0; table[row][col] = 0;}

26 Example of 2-D array To print Sum of Column of a matrix To print Sum of Column of a matrix void Col_sum(int R[7][7]) { int sum, i, j; for (i=0; i<7; i++) { sum = 0; for (j = 0; j<7; j++) sum = sum + R[j][i]; cout << "Sum of Column : " << i+1 << " is : " << sum << endl; }}

27 void main() {clrscr(); int ar[7][7]; int i, j; for (i=0; i<7; i++) for (j = 0; j<7; j++) cin >> ar[i][j]; Col_sum(ar);}

28 Homework Write a program in C++ to arrange the elements of a list in descending order. (1-D array). Write a program in C++ to arrange the elements of a list in descending order. (1-D array). Write a program in C++ to enter elements in Write a program in C++ to enter elements in 2-D matrix (3x3) and display the matrix on the output screen. 2-D matrix (3x3) and display the matrix on the output screen.