CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.

Slides:



Advertisements
Similar presentations
Functions in C++. Functions  Groups a number of program statements into a unit & gives it a name.  Is a complete and independent program.  Divides.
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 6: User-Defined Functions I
C++ Functions CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Basic Elements of C++ Chapter 2.
Chapter 4 Procedural Abstraction and Functions That Return a Value.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
1 Remember: Examination is a chance not ability. By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
CHAPTER 6 USER-DEFINED FUNCTIONS Made By- Kartik Belwal.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
FUNCTIONS - What Is A Function? - Advantages Function Declaration
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
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.
Chapter 3: User-Defined Functions I
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
Programming Fundamentals Enumerations and Functions.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 6: User-Defined Functions I
Basic Elements of C++.
Variables A piece of memory set aside to store data
Chapter 6: User-Defined Functions I
User-Defined Functions
Basic Elements of C++ Chapter 2.
User Defined Functions
6 Chapter Functions.
Chapter 6: User-Defined Functions I
Arrays Arrays A few types Structures of related data items
Fundamental Programming
Presentation transcript:

CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi

Arrays Arrays are data structures which hold multiple variables of the same data type. Consecutive group of memory locations having same name and type (int, char, float, double etc.). To refer to an element, specify array name and position number (index) e.g. myList[5]

Declaration Syntax DataType ArrayName[size]; int a[20]; float grades[50]; Means a is an integers array with size 20. Each element in the array is accessed with a subscript i.e. a[0], a[1] ….. a[19]. Each memory element can hold an integer only. First element at position 0 and the maximum position at size ‐1 Memory requirements depend on the size of array. Array declaration is static i.e. the size must be specified at the time of declaration.

Initializing Arrays Declaring, creating, initializing in one step: int a[5]={1,5,7,9,10]; → a[0]=1, a[1]=5,…,a[4]=10 int b[3] ={1};→ b[0]=1,b[1]=0,b[2]=0 int c[2] = {1,2,3,4};→ ERROR To set every element to same value: int d[5] = {0}; If array size omitted, initializers determine size int e[ ] = {1, 2, 3, 4, 5}; //this implies size of a is 5

Initializing Arrays (cont.) If A is an array and B is an array of same dimension (size) we can assign A = B The contents of B will be copied to array A Example: int A[4]={11,33,55,66}, B[4]; B = A;//B[0]= 11,…,B[3] =66

Example #include int main() { double distance[5] = {44.14, , 96.08, , 6.28}; cout << "Members of the array\n"; cout << "Distance 1: " << distance[0] << endl; cout << "Distance 2: " << distance[1] << endl; cout << "Distance 3: " << distance[2] << endl; cout << "Distance 4: " << distance[3] << endl; cout << "Distance 5: " << distance[4] << endl; return 0; } Outputs:

Example #include int main() { const int numberOfItems = 5; double distance[numberOfItems] = {44.14, , 96.08, , 6.28}; cout << "Members of the array\n"; for(int i = 0; i < numberOfItems; ++i) cout << "Distance " << i + 1 << ": " << distance[i] << endl; return 0; } Outputs:

Example // Read a set of integers and find the sum and average void main( ) { int a[20], i, sum=0, n; float average; cout<< "How many integers to be taken: " ; cin>> n; for(i=0; i<n; i++) //Read and add to sum { cout<<"Enter an integer:"; cin>> a[i]; sum = sum + a[i]; } average = sum / (float)n; cout<< “Sum= " << sum << endl << “Average= " << average << endl; } Outputs:

Functions A function is a group of statements that together perform a task. Every C++ program has at least one function which is main(). Using functions facilitates the construction of the programs in a more modular way. There are two kinds of functions: Built-in functions: those supplied to you User-defined functions: those which you are going to write

Advantages of writing functions Reduces the complexity of main Work can be divided Function can be reused many times Easier to read and understand Easier to find-out errors and modify the program

Built-in functions Examples of pre-defined functions: Mathematical Functions pow(x,y), sqrt(x), sin(x), cos(x) String and Character Functions tolower(x), toupper(x), strlen(x), strncmp(x,y) Time, Date and Localization Functions time(&x), difftime (x,y)

Example #include int main() { cout << "2 to the power of 2 is: " << pow(2,2) << endl; cout << "4 to the power of 2 is: " << pow(4,2) << endl; return 0; } Outputs:

User-defined functions - Syntax type name ( parameter1, parameter2,...)// function header { statements } //function body type: is the data type specifier of the data returned by the function. name: is the identifier by which it will be possible to call the function. parameters: Each parameter consists of a data type specifier followed by an identifier, like any regular variable declaration (for example: int x) and which acts within the function as a regular local variable. They allow passing arguments to the function when it is called. The different parameters are separated by commas. Statements: is the function's body. It is a block of statements surrounded by braces { }.

Function Declaration In order to create and use a function, you must send an acknowledgement to the compiler to know. The syntax of declaring a function is: ReturnType FunctionName(Parameters); An assignment, considered as a function, is made of three parts: its purpose, its needs, and the expectation. Based on this formula, the expectation you have from a function is the ReturnType factor. The simplest return type you can use is called, and represented as, void. Using this keyword, the simplest formula we can use is: void FunctionName();

Function Names A function name follows the same rules we have applied to our variables. In addition, use a name that specifies what the function is expected to do. Usually, a verb is appropriate for a function that performs an action. Examples of names of functions are Add, Start, Assign, Play, etc.

The return statement A return statement ends the processing of the current function and returns control to the caller of the function. The following are examples of return statements: return; // Returns no value return result;// Returns the value of result return 1; // Returns the value 1 return (x * x);// Returns the value of x * x

Example Write an example of a function that returns nothing void do-nothing () { } void do-nothing () { return; } /* Can be written with or without retuen */ Write an example of a function that returns some values int do-nothing() { return 0; } int do-nothing () { } /* Error because it musts return an integer value */

Function call a C++ program always starts by calling main. In fact, main is the only function called automatically. The code in any other function is only executed if its function is called from main. A function is called by writing its name, followed by (). If the function takes arguments, the arguments are passed, in the order listed in the function declaration, in the parentheses. Function call syntax: FunctionName (Arguments if any);

Structure of a program #preprocessor directives prototypes void main() { statements; } DataType function1(Parameters if any) { statements; } DataType function2(Parameters if any) { statements; }.

Example // void function example #include void printmessage (); //Prototype int main () { printmessage (); //Call return 0; } void printmessage () //Header { cout << "I'm a function!"; } Outputs:

Example #include int addition(int a, int b); int main() { int x,y,z; cout << "Enter the first integer: "; cin >> x; cout << "Enter the second integer: "; cin >> y; addition(x,y); cout << "Sum of X and Y is:" << z << '\n'; } int addition(int a, int b) { int r; r = a + b; return (r); } Outputs:

Example #include int addition(int a, int b); int main() { int x,y,z; cout << "Enter the first integer: "; cin >> x; cout << "Enter the second integer: "; cin >> y; z = addition(x,y); cout << "Sum of X and Y is:" << z << '\n'; } int addition(int a, int b) { int r; r = a + b; return (r); } Outputs:

Example #include int addition(int a, int b); int main() { int x,y,z; cout << "Enter the first integer: "; cin >> x; cout << "Enter the second integer: "; cin >> y; cout << "Sum of X and Y is:“; cout << addition(x,y); } int addition(int a, int b) { int r; r = a + b; return (r); } Outputs:

Example #include int addition(int a, int b); int main() { int x,y,z; cout << "Enter the first integer: "; cin >> x; cout << "Enter the second integer: "; cin >> y; z = addition(x,y); cout << "Sum of X and Y is:" << z << '\n'; } int addition(int a, int b) { return (a+b); } Outputs:

Example #include int addition(int a, int b); int main() { int x,y,z; cout << "Enter the first integer: "; cin >> x; cout << "Enter the second integer: "; cin >> y; z = addition(x);//Error z = addition(1,2,3); //Error cout << "Sum of X and Y is:" << z << '\n'; } int addition(int a, int b) { return (a+b); } Outputs:

Example #include bool IsEven (int num); int main() { int x; bool z; cout << "Enter an integer number: "; cin >> x; z = IsEven(x); cout << “Result is: " << z << '\n'; z = IsEven(3); cout << “Result is: " << z << '\n'; z = IsEven(x+3); cout << “Result is: " << z << '\n'; } bool IsEven (int num) { if (num%2 == 0) return true; else return false; } Outputs:

Exercise Write a C++ program that will display the calculator menu. The program will prompt the user to choose the operation choice (from 1 to 5). Then it asks the user to input two integer vales for the calculation. See the sample below. MENU 1. Add 2. Subtract 3. Multiply 4. Divide 5. Modulus Enter your choice: 1 Enter your two numbers: Result: 27