Computer Programming: C++

Similar documents
Computer Programming: C++

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

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

Computer Programming: C++

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

Computer Science & Engineering 150A Problem Solving Using Computers

Instructor: Eng.Omar Al-Nahal

Arrays. Eng. Mohammed Abdualal

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

Advanced Computer Programming

Lab 3. Pointers Programming Lab (Using C) XU Silei

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

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

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

Computer Programming, I. Laboratory Manual. Final Exam Solution

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

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

How to declare an array in C?

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

Computer Programming, I. Laboratory Manual. Experiment #7. Methods

Arrays. COMS W1007 Introduction to Computer Science. Christopher Conway 10 June 2003

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

Computer Programming, I. Laboratory Manual. Experiment #9. Multi-Dimensional Arrays

Principles of Programming. Chapter 6: Arrays

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.

Object oriented programming C++

Last Class. Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it

Introduction to Programming Using Java (98-388)

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

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

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

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

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

Unit 7. Functions. Need of User Defined Functions

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

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

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

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

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

Arrays. Chapter 7 (Done right after 4 arrays and loops go together, especially for loops)

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Assignment 6. Methods Homework Spring 2017 P Question 1 all

For loops, nested loops and scopes. Jordi Cortadella Department of Computer Science

Functions. Arash Rafiey. September 26, 2017

Computer Programming, I. Laboratory Manual. Experiment #6. Loops

Last Class. More on loops break continue A bit on arrays

Declaring Array Variable

Outline. For loops, nested loops and scopes. Calculate x y. For loops. Scopes. Nested loops. Algorithm: repeated multiplication x x x x

Procedural Programming

Array. Prepared By - Rifat Shahriyar

Lecture 5: Methods CS2301

Lecture 8 Tao Wang 1

CSCI 2132 Software Development. Lecture 18: Functions

Programming for Engineers Arrays

C Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

C++ for Engineers and Scientists. Third Edition. Chapter 12 Pointers

Arrays. Lecture 11 CGS 3416 Fall October 26, 2015

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

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Array Basics: Outline

Array Basics: Outline

Lab Session # 5 Arrays. ALQUDS University Department of Computer Engineering

DataBase Lab JAVA-DATABASE CONNECTION. Eng. Haneen El-masry

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Operating Systems Lab

Government Polytechnic Muzaffarpur.

1 Lexical Considerations

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Two Dimensional Arrays

ITP 342 Mobile App Dev. Fundamentals

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

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

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd

CpSc 1111 Lab 9 2-D Arrays

Array Basics: Outline

Methods. Eng. Mohammed Abdualal

Arrays. Chapter 7. Walter Savitch Frank M. Carrano

Programming & Data Structure Laboratory. Arrays, pointers and recursion Day 5, August 5, 2014

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Final Exam. COMP Summer I June 26, points

C++ PROGRAMMING SKILLS Part 4: Arrays

Lab # 4. Files & Queues in C

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

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

Assignment 1: grid. Due November 20, 11:59 PM Introduction

Example: Computing prime numbers

Java Arrays. What could go wrong here? Motivation. Array Definition 9/28/18. Mohammad Ghafari

Methods. CSE 114, Computer Science 1 Stony Brook University

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

Lexical Considerations

Introduction to Computer Science Midterm 3 Fall, Points

Chapter 6: Using Arrays

Loops. Eng. Mohammed Abdualal. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department

Transcription:

The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2003 Muath i.alnabris Computer Programming: C++ Experiment #7 Arrays Part II

Passing Array to a Function At some point, we may need to pass an array to a function as a parameter. In C++, it is not possible to pass the entire block of memory represented by an array to a function directly as an argument. But what can be passed instead is its address. In practice, this has almost the same effect, and it is a much faster and more efficient operation. To accept an array as parameter for a function, the parameters can be declared as the array type, but with empty brackets, omitting the actual size of the array. For example: void procedure (int arg[]) This function accepts a parameter of type "array of int" called arg. In order to pass to this function an array declared as: int myarray [40]; it would be enough to write a call like this: procedure (myarray); Example 1: Passing One-dimensional Array to a Function 2

In the code above, the first parameter (int arg[]) accepts any array whose elements are of type int, whatever its length. For that reason, we have included a second parameter that tells the function the length of each array that we pass to it as its first parameter. This allows the for loop that prints out the array to know the range to iterate in the array passed, without going out of range. Example 2: Passing Multidimensional Array to a Function C++ Program to display the elements of two dimensional array by passing it to a function 3

In the above program, the multi-dimensional array num is passed to the function display.)( Inside, display() function, the array n (num) is traversed using a nested for loop. The program uses 2 for loops to iterate over the elements inside a 2-dimensional array. If it were a 3-dimensional array, you should use 3 for loops. Finally, all elements are printed onto the screen. Note: Multidimensional array with dimension more than 2 can be passed in similar way as two dimensional array. Scope of variables: A scope is a region of the program and broadly speaking there are three places, where variables can be declared Inside a function or a block which is called local variables, - In the definition of function parameters which is called formal parameters - Outside of all functions which is called global variables. 4

This section special with function Pass By Reference: The call by reference method of passing arguments to a function copies the reference of an argument into the formal parameter. Inside the function, the reference is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument. To pass the value by reference, argument reference is passed to the functions just like any other value. So accordingly you need to declare the function parameters as reference types as in the following function swap(), which exchanges the values of the two integer variables pointed to by its arguments. For now, let us call the function swap() by passing values by reference as in the following example 5

When the above code is put together in a file, compiled and executed, it produces the following result 6

Lab work: 1- Write function to display a matrix as shown below. The diagonal of the matrix fills with 0. The lower side fills will -1s and the upper side fills with 1s. 2- Write program Take a numbers from user and insert a random new values in array Then write function to sort this numbers 7

Homework: 1- Write program multiple two matrices using function, saves it in another matrix (twodimensional array) and displays it on the screen. 2- write C++ program to display a table of numbers as shown below: 8