Arrays. int Data [8] [0] [1] [2] [3] [4] [5] [6] [7]

Similar documents
Chapter 7 Array. Array. C++, How to Program

CS 240 Computer Programming 1 Arrays

9. Arrays. Compound Data Types: type name [elements]; int billy [5];

Thus, to declare billy as shown above it would be something as simple as the following sentence:

Object Oriented Programming(OOP).

C++ Lecture 5 Arrays. CSci 588: Data Structures, Algorithms and Software Design.

11. Arrays. For example, an array containing 5 integer values of type int called foo could be represented as:

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

STRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY

How to declare an array in C?

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

DELHI PUBLIC SCHOOL TAPI


Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

BITG 1113: Array (Part 1) LECTURE 8

Lab #10 Multi-dimensional Arrays

C++ Arrays. C++ Spring 2000 Arrays 1

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2013 CISC2200 Yanjun Li 1. Fall 2013 CISC2200 Yanjun Li 2

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2017 CISC2200 Yanjun Li 1. Fall 2017 CISC2200 Yanjun Li 2

Pointers II. Class 31

Array. Prepared By - Rifat Shahriyar

Array. Array Declaration:

C++ PROGRAMMING SKILLS Part 4: Arrays

Learning Objectives. Introduction to Arrays. Arrays in Functions. Programming with Arrays. Multidimensional Arrays

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)

Exercise: Inventing Language

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them.

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Pointer Data Type and Pointer Variables

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

BLUE PRINT SUBJECT: - COMPUTER SCIENCE(083) CLASS-XI. Unit Wise Marks

Chapter 2: Introduction to C++

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays

Multiple-Subscripted Arrays

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Review of Important Topics in CS1600. Functions Arrays C-strings

COMPUTER SCIENCE (083)

Pointers, Dynamic Data, and Reference Types


Here, type declares the base type of the array, which is the type of each element in the array size defines how many elements the array will hold

Module 6: Array in C

Functions. Introduction :

Chapter 4 Computer Science with C++ Name: Review Worksheet A Mr. Ferwerda

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

LECTURE 02 INTRODUCTION TO C++

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

cast.c /* Program illustrates the use of a cast to coerce a function argument to be of the correct form. */

Chapter 6 SINGLE-DIMENSIONAL ARRAYS

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

In the previous lecture, we learned about how to find the complexity of algorithms and search algorithms on array.

Downloaded S. from Kiran, PGT (CS) KV, Malleswaram STRUCTURES. Downloaded from

FORM 2 (Please put your name and form # on the scantron!!!!)

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

CSC 211 Intermediate Programming. Arrays & Pointers

Exam 3 Chapters 7 & 9

Structured Data. CIS 15 : Spring 2007

MEMORY ADDRESS _ REPRESENTATION OF BYTES AND ITS ADDRESSES

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

CS101 Computer Programming and Utilization

Data types. CISC 1600/1610 Computer Science I. Array syntax. Memory allocation. Zero-indexing 4/4/2016. Arrays

Computer Science & Engineering 150A Problem Solving Using Computers

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

The University Of Michigan. EECS402 Lecture 05. Andrew M. Morgan. Savitch Ch. 5 Arrays Multi-Dimensional Arrays. Consider This Program

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.

Array Initialization

Chapter 10. Arrays and Strings

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

AMCAT Automata Coding Sample Questions And Answers

ENCE 688R Civil Information Systems. The Java Language. Mark Austin.

primitive arrays v. vectors (1)

HOLIDAYS HOMEWORK CLASS : XII. Subject : Computer Science

Principles of Programming. Chapter 6: Arrays

Arrays. Week 4. Assylbek Jumagaliyev

CSE 230 Intermediate Programming in C and C++ Arrays and Pointers

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

CS201- Introduction to Programming Current Quizzes

Computer Programming. Dr. Fahad Computer Science Deptt.

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Homework #3 CS2255 Fall 2012

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 26, 2017

1. Answer the following : a) What do you mean by Open Source Software. Give an example. (2)

CHAPTER 3 ARRAYS. Dr. Shady Yehia Elmashad

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

Chapter Overview. Pointers and Dynamic Arrays. Pointers. Pointers. Declaring Pointers. Pointers Tell Where To Find A Variable. 9.

Object Oriented Programming. Java-Lecture 6 - Arrays

C++ for Java Programmers

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

CS 161 Exam II Winter 2018 FORM 1

Introduction to C++ with content from

For example, let s say we define an array of char of size six:

Chapter 9. Pointers and Dynamic Arrays

TWO DIMENSIONAL ARRAY OR MATRIX common name scores Defined as scores[10][5] IE 10 rows x 5 columns showing name of each position; some values below

QUIZ: loops. Write a program that prints the integers from -7 to 15 (inclusive) using: for loop while loop do...while loop

CSE101-Lec#18. Multidimensional Arrays Application of arrays. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

Transcription:

Arrays Arrays deal with storage of data, which can be processed later. Arrays are a series of elements (variables) of the same type placed consecutively in memory that can be individually referenced by adding an index to a unique name. Arrays Singe dimensional array Multi dimensional array (two dimensional array) Single Dimensional Arrays int Data [8] [0] [1] [2] [3] [4] [5] [6] [7] Array of 8 integer elements. The above declaration allocates 32 bytes of memory to the array Data. Accessing the array elements The following notation is used to access the array elements name of the array[associated index] Data[0],Data[1]., Data[7] are variables which have the same name Data, but are distinguished by the index associated with each element. In C++ array indexing starts from 0 by default. index(subscript) = (position of the variable in the array ) 1;

index of the last element = (length of the array ) 1; thus the array index varies from 0.(length of array) 1. Referencing array elements a = 4, b = 3; c[a+b] = c[5] + 3; adds 3 to the element at the 6 th position in the array and assigns it to the element stored at position 6. Index of an array cannot be a floating point number. for e.g. c[2.445] is invalid array reference. Index of an array cannot be negative for e.g. c[-4] is invalid array reference. Declaring an array Syntax type name of the array[length of the array] e.g. int c[12]; // declares an array of integers similarly char c[12]; float c[12]; int i = 23, int h[i] is valid array declaration. Initializing an Array I) int c[12]; for(int i = 0;i<12;i++) c[i] = i+1; assigns c[0] = 1,c[1] = 2,..c[11] = 12 II) int c[12] = 1, 2, 3, 4, 5, 6; assigns c[0] = 1,c[1] = 2,..c[5] = 6 also int c[ ]= 1, 2, 3, 4, 5, 6; this declaration has the same effect as the previous declaration, but in this

case the C++ compiler determines the size of the array. To assign one array to another, the assignment should take effect on element by element basis. for(int i = 0 ; i<=11; ++i) c[i] = a[i]; To assign an array element to a variable, the following statement is used a = c[2]; Operations on array elements x = c[6] /2.0; //divides the element at position 7 with 2 and assigns it to x; cout<<c[0] + c[4] + c[7] <<endl; refer to example[26] of the in class example section on the class website. Multidimensional Arrays (Two Dimensional Arrays) Two dimensional array can be visualized as a matrix where each element has a unique row-index and column- index associated with it. int Data[3][3]; row number 0 1 2 Array indices Colnumbers 0 [0][0] [0][1] [0][2] 1 [1][0] [1][1] [1][2] 2 [2][0] [2][1] [2][2] Declaring a two dimensional array int Data[3][3]; A matrix of dimension (3X3)

Initializing a two-dimensional array I) int Data[3][3] = 1,2,34,5,67,8,9); Data 0 1 2 0 1 2 3 1 4 5 6 2 7 8 9 II) for (int i = 0; i<=2;i++) for(int j = 0; j<=2; j++) a[i][j] = i*3 + j +1 Data 0 1 2 0 1 2 3 1 4 5 6 2 7 8 9 i = row index j = column index Refer to the inclass example[28]. Applications of Multidimensional arrays 1) Matrix manipulations ( refer to inclass example[31] ) a) inverse of a matrix. b) transpose of a matrix. 2) Solutions of a set of linear equations using Gauss methods. 3) Pattern Generation. Passing Arrays as a Parameter to functions In C++ arrays cannot be passed by value to the function i.e a complete chunk of memory cannot be passed to the function by value,but it is allowed to pass the array by reference to the function. Thus the address of the array needs to be passed to the function. The address of the array is the name of the array and it points to the first location of the array.

Data [0] [1] [2] [3] [4] [5] [6] [7] The address of the array Data is the name Data. Example: #include <iostream.h> void printarray (int arg[], int length) for (int n=0; n<length; n++) cout << arg[n] << " "; cout << "\n"; int main () int firstarray[] = 5, 10, 15; int secondarray[] = 2, 4, 6, 8, 10; printarray (firstarray,3); printarray (secondarray,5); return 0; Output 5 10 15 2 4 6 8 10 It is a good practice to pass the length of the array to the function, otherwise a logic to determine the length of the array has to incorporated in the program. It is also possible to pass multidimensional arrays to functions

#include<iostream.h> void printarray(int [][3]); int main() int a[2][3] = 1,2,34,5,6; cout<< values in the array by row are : <<endl; printarray(a); return 0; 2-D Array passed by reference to the void function printarray void printarray(int b[][3]) for(int i = 0;i<2;++i) for(int j = 0;j<3;j++) cout<<b[i][j]<< ; cout<<endl; Output: Values in the array a by row are 1 2 3 4 5 6 References: [1] C++ for Engineers and Scientists, Gary J. Bronson. [2] C++ How To Program, Deitel and Deitel. [3] Object Oriented Programming in Turbo C++, Robert Lafore. [4] www.cplusplus.com