L what is a void-function? l what is a boolean function? l is it possible for a function to have no parameters? l what is program stack? function frame?

Slides:



Advertisements
Similar presentations
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Advertisements

Simple Arrays COMP104 Lecture 11 / Slide 2 Arrays * An array is a collection of data elements that are of the same type (e.g., a collection of integers,characters,
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
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 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
Simple Arrays Programming COMP104 Lecture 12 / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g., a collection.
Arrays.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Chapter 7 Arrays. Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3 Programming with Arrays 7.4 Multidimensional Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Chapter 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
L what are predefined functions? l what is? n function name n argument(s) n return value n function call n function invocation n nested function call l.
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.
L what are executable/non-executable statements l out of the ones below which constructs are executable #include p=3.14; const double PI=3.14; int myfunc(int);
CSIS 113A Lecture 10 Arrays Glenn Stevenson CSIS 113A MSJC.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
L what is a void-function? l what is a predicate? how can a predicate be used? l what is program stack? function frame? l what’s call-by-value? l what’s.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Test 2 Review Outline.
Review 1.
What Is? function predefined, programmer-defined
Chapter 6 Arrays in C++ 2nd Semester King Saud University
Predefined Functions Revisited
Arrays 2/4 By Pius Nyaanga.
Chapter 6 Arrays Lecturer: Mrs Rohani Hassan
Chapter 7 Arrays Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Pointers and Pointer-Based Strings
New Structure Recall “average.cpp” program
Pointers Revisited What is variable address, name, value?
Dynamic Memory Allocation
Multiple Files Revisited
Previous Lecture Review
7 Arrays.
CS150 Introduction to Computer Science 1
Namespaces How Shall I Name Thee?.
Review of Everything Arrays
Fundamental Programming
CISC181 Introduction to Computer Science Dr
CMSC202 Computer Science II for Majors Lecture 03 – Arrays and Functions Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
7 Arrays.
Pointers and Pointer-Based Strings
Suggested self-checks: Section 7.11 #1-11
Arrays Arrays A few types Structures of related data items
Multiple Files Revisited
Predefined Functions Revisited
What Is? function predefined, programmer-defined
ICS103 Programming in C Lecture 12: Arrays I
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
Chapter 7 Arrays. Chapter 7 Arrays Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3 Programming with Arrays 7.4 Multidimensional.
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

l what is a void-function? l what is a boolean function? l is it possible for a function to have no parameters? l what is program stack? function frame? l what’s call-by-value? l what’s call-by-reference? l can expression be passed by reference? Previous Lecture Review

Arrays

l programmers sometimes need to manipulate variables of similar nature l aggregate construct – a construct that allows manipulation of multiple entities as a single element n array is an aggregate construct l array - a list of similar variables with two names: n name of the array it is the same for all variables in the array index (or subscript) - different for every variable, put in square brackets [] example: array score may have following variables: …, score[2], score[3], score[4], … l variables in the array are indexed variables or elements of the array l all variables in the array have the same type called base type of the array l the number of indexed variables is the size of the array array is declared as follows: int score[5]; the number in brackets – 5 is array size l the indexes start from 0. Statement above declared the following variables: score[0], score[1], score[2], score[3], score[4] note, score[5] is not there! l non-array (regular) variable is scalar variable Array Definition

Array Terms Again array base type baseType id [ sizeExpession ] ; array name expression specifies number of array elements double X[100]; // subscripts are 0 through 99 !

l indexed variables can be used anywhere a scalar variable can be: cin >> score[4] >> score[2]; max = score[4] + score[2]; score [4]=max; cout << score[2] << ” ” << score[4]; l index can be any expression: int student=2; score[student]=99; score[student+1]=100; cout << score[1] << ” ” << score[student]; l loops are ideal for arrays: for (int index=0; index < arraySize; ++index){ // do something with myarray[index] } Array Usage

int main(){ int numbers[5]; // array of numbers cout << "Enter the numbers: "; for(int i=0; i < 5; ++i) cin >> numbers[i]; // finding the minimum int minimum=numbers[0]; // assume the first element for (int i=1; i < 5; ++i) // start from second if (minimum > numbers[i]) minimum=numbers[i]; cout << "The smallest number is: " << minimum << endl; } Array with Loops Example

l array elements are placed in memory consequently: int a[10], myvar=20; l no range checking is done on index l referring to index that is not present in the array results in an out of range error n it is a logical error (bug/run-time error) with unpredictable consequences. n these examples are both out-of-rage errors a[10]=55; a[myvar]=5; Arrays in Memory, Index Out of Range -- a[4]a[5]a[6]a[3]a[0]a[2]a[8]a[9]a[7]a[1] myvar other vars array

l assigning values to the array variables at declaration is initalization int a[10]={0, 10, 20, 30, 40, 50, 60, 70, 80, 90}; l do not have to initialize all elements (rest hold arbitrary values): int a[10]={0, 10, 20}; l may skip array size at initialization (size computed to hold all values) int a[]={10,20,30}; // array size is three l using global named constants for array size is good style: int score[NUM_STUDENTS]; Initializing Array

l array elements can be passed as arguments to functions: int i=5, n, a[10]; myfunc(n);// ordinary variable as argument myfunc(a[3]); // element as argument myfunc(a[i]); // which element are we passing? l entire arrays can be passed as arguments n always passed by reference (no & needed) n function does not know array size, it is usually passed as a separate variable –alternatively – make size a global constant, using a literal constant is bad style l example: void fillUp(int [], int); // prototype void fillUp(int a[], int size){ // definition cout << ”Enter ” << size << ” numbers:”; for(int i=0; i < size; ++i) cin >> a[i]; } fillUp(score, 5); // invocation, note no brackets Arrays and Functions

l array is always passed by reference n may lead to accidental value changes – run time error const type modifier specifies that the parameter shall not be modified in the function are not modified in the function n that is, it turns accidental value change into syntactic error n this allows the compiler to detect it an alert the programmer void printArray(const int [], int); // prototype void printArray(const int a[], int size){ // definition a[0] = 33; // not allowed, syntax error!!! for(int i=0; i < size; ++i) cout << a[i]; } l function prototype and head have to agree on modifiers l function invocation is the same regardless of modifiers const Parameter Type Modifier