Lecture (07) Arrays. By: Dr. Ahmed ElShafee. Dr. Ahmed ElShafee, ACU : Fall 2015, Programming I

Similar documents
To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

C++ Arrays. To declare an array in C++, the programmer specifies the type of the elements and the number of elements required by an array as follows

Classification s of Data Structures

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

UNIT-1. Chapter 1(Introduction and overview) 1. Asymptotic Notations 2. One Dimensional array 3. Multi Dimensional array 4. Pointer arrays.

ARRAYS(II Unit Part II)

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

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

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

CHAPTER 3 ARRAYS. Dr. Shady Yehia Elmashad

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

ONE DIMENSIONAL ARRAYS

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

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

2-D Arrays. Of course, to set each grid location to 0, we have to use a loop structure as follows (assume i and j are already defined):

Functions, Arrays & Structs

C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections

Arrays. Lecture 11 CGS 3416 Fall October 26, 2015

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

Arrays a kind of data structure that can store a fixedsize sequential collection of elements of the same type. An array is used to store a collection

Arrays. Lecture 11 CGS 3416 Spring March 6, Lecture 11CGS 3416 Spring 2017 Arrays March 6, / 19

C++ PROGRAMMING SKILLS Part 4: Arrays

Object Oriented Programming. Java-Lecture 6 - Arrays

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

Arrays and Strings. Arash Rafiey. September 12, 2017

General Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;

Procedural Programming

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

Computer Programming

Duhok Polytechnic University Amedi Technical Institute/ IT Dept. Halkawt Rajab Hussain

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

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

Pointers. Memory. void foo() { }//return

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

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

Chapter 9. Pointers and Dynamic Arrays

Computers Programming Course 10. Iulian Năstac

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

CSI33 Data Structures

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Computer Programming: C++

Lecture 17. For Array Class Shenanigans

V2 3/5/2012. Programming in C. Introduction to Arrays. 111 Ch 07 A 1. Introduction to Arrays

CISC220 Lab 2: Due Wed, Sep 26 at Midnight (110 pts)

Types. C Types. Floating Point. Derived. fractional part. no fractional part. Boolean Character Integer Real Imaginary Complex

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

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

Arrays. Systems Programming Concepts

Introduction to Computer Science Midterm 3 Fall, Points

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

Objectives. Order (sort) the elements of an array Search an array for a particular item Define, use multidimensional array

Lab #10 Multi-dimensional Arrays

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:

Chapter 7 : Arrays (pp )

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

type arrayname [ size type arrayname [ size ] = { item ,, item size size

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

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

by Pearson Education, Inc. All Rights Reserved. 2

Multiple-Subscripted Arrays

tablica: array: dane_liczbowe

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

C++ For Science and Engineering Lecture 12

Lab Session # 6 Functions. ALQUDS University Department of Computer Engineering

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

FORTRAN - ARRAYS. For example, to declare a one-dimensional array named number, of real numbers containing 5 elements, you write,

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

Array Session 9

KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

Class C{ int a; } what variables below are objects : a. C c; b. String str; c. Scanner scanner; d. int num; e. float f;

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) A few types

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program)

Module 201 Object Oriented Programming Lecture 10 - Arrays. Len Shand

Computers Programming Course 11. Iulian Năstac

C Pointers. 6th April 2017 Giulio Picierro

C Programming Review CSC 4320/6320

Arrays. CSE / ENGR 142 Programming I. Chapter 8. Another Motivation - Averaging Grades. Motivation: Sorting. Data Structures.

Array Elements as Function Parameters

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

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.

array Indexed same type

Pointers and Dynamic Arrays

Functions. Introduction :

Computer Programming

Arrays. Week 4. Assylbek Jumagaliyev

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

TEMPLATES AND EXCEPTION HANDLING

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Chapter 6: Arrays. Starting Out with Games and Graphics in C++ Second Edition. by Tony Gaddis

Array Basics: Outline

7/21/10 MOTIVATION. Did you see Inception this weekend? COMP 10 EXPLORING COMPUTER SCIENCE. Lecture 6 Arrays

C++ assign array values. C++ assign array values.zip

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA.

Transcription:

Lecture (07) Arrays By: Dr Ahmed ElShafee ١ introduction An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type Instead of declaring individual variables, such as number0, number1,, and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and, numbers[99] to represent individual variables A specific element in an array is accessed by an index All arrays consist of contiguous memory locations The lowest address corresponds to the first element and the highest address to the last element ٢

Declaring Arrays type arrayname [ arraysize ]; This is called a single dimension array The arraysize must be an integer constant greater than zero and type double balance[10]; ٣ Initializing Arrays double balance[5] = {10000, 20, 34, 170, 500}; The number of values between braces { } can not be larger than the number of elements that we declare double balance[] = {10000, 20, 34, 170, 500}; You will create exactly the same array as you did balance[4] = 500; statement assigns element number 5th in the array a value of 500 ٤

Accessing Array Elements double salary = balance[9]; Cout<< balance[10]; ٥ Example 01 ٦

٧ Multi dimensional arrays the general form type name[size1][size2][sizen]; three dimensional int threedim[5][10][4]; ٨

Two Dimensional Arrays The simplest form of the multidimensional array type arrayname [ x ][ y ]; A two dimensional array can be think as a table ٩ Initializing Two Dimensional Arrays initialized by specifying bracketed values for each row int a[3][4] = { {0, 1, 2, 3}, /* initializers for row indexed by 0 */ {4, 5, 6, 7}, /* initializers for row indexed by 1 */ {8, 9, 10, 11} /* initializers for row indexed by 2 */ } infested braces, which indicate the intended row, are optional The following initialization is equivalent to previous example int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11}; ١٠

Accessing Two Dimensional Array Elements Using row index and column index of the array int val = a[2][3]; ١١ Example 02 ١٢

١٣ Pointer to an array An array name is a constant pointer to the first element of the array double balance[50]; balance is a pointer to &balance[0], which is the address of the first element of the array balance double *p; double balance[10]; p = balance; Therefore, *(balance + 4) is a legitimate way of accessing the data at balance[4] ١٤

Example 03 ١٥ ١٦

Passing arrays to functions C++ does not allow to pass an entire array as an argument to a function However, You can pass a pointer to an array by specifying the array's name without an index Way-1 void myfunction(int *param) } { ١٧ Way-2 void myfunction(int param[10]) } { Way-3 void myfunction(int param[]) } { ١٨

Example 04 ١٩ ٢٠

Return array from functions If you want to return a single dimension array from a function, you would have to declare a function returning a pointer int * myfunction() } } C++ does not advocate to return the address of a local variable to outside of the function so you would have to define the local variable as static variable ٢١ Example 05 ٢٢

٢٣ Example 06 Build a GUI application that calculates average a number entered by the user as follow ٢٤

٢٥ ٢٦

Thanks, Wish you all the best ٢٧