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.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
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.
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,
5 5 Arrays. OBJECTIVES In this lecture we will learn:  Case switch  To use the array data structure to represent a set of related data items.  To declare.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
10/06/08MET CS Fall Arrays and Vectors 1 Multidimensional Types: 5. Arrays and Vectors Need: ability to define and manipulate variable objects.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
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.
 2006 Pearson Education, Inc. All rights reserved Arrays.
 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 ICS103 Programming in C Lecture 12: Arrays I. 2 Outline Motivation for One-dimensional Arrays What is a One-dimensional Array? Declaring One-dimensional.
Slides prepared by Rose Williams, Binghamton University Chapter 6 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.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Arrays One-Dimensional initialize & display Arrays as Arguments Part I.
 2006 Pearson Education, Inc. All rights reserved Arrays.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
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.
1 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
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.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Static arrays.
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
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.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 8 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 :
Copyright Curt Hill Arrays in C/C++ What? Why? How?
Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
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);
Arrays.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
CSIS 113A Lecture 10 Arrays Glenn Stevenson CSIS 113A MSJC.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Arrays Chapter 12. One-Dimensional Arrays If you wanted to read in 1000 ints and print them in reverse order, it would take a program that’s over 3000.
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.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
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?
New Structure Recall “average.cpp” program
Multiple Files Revisited
Previous Lecture Review
7 Arrays.
7 Arrays.
Arrays Arrays A few types Structures of related data items
Multiple Files Revisited
Presentation transcript:

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 call-by-reference? how is it different syntactically? l can expression be passed by reference? Previous Lecture Review 1

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 collection of similar variables with two names: n name of the array – 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. Above statement declares 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 3

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

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 an 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 score[index] } Array Usage 5

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 6

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. these 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 7

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 zeroes): 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 named constants for array size is good style: const int numStudents = 55; int score[numStudents]; Initializing Array 8

l array elements can be passed as arguments to functions: int i=5, n, a[10]; myfunc(n);// scalar variable as argument myfunc(a[3]); // element as argument myfunc(a[i]); // which element is passed? l entire array can be passed as argument 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 named 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 9

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 n that is, it turns accidental value change into compile-time error void printArray(const int [], int); // prototype void printArray(const int a[], int size){ // definition a[0] = 33; // not allowed, compile-time error!!! for(int i=0; i < size; ++i) cout << a[i]; } l function prototype and head have to agree on type modifiers l function invocation is the same regardless of modifiers const Parameter Type Modifier 10

l what is an aggregate construct? l what is an array? l what is name of the array? index? l what is indexed variable? wlement of the array? scalar variable? l What is array size? l what is array’s base type? l how is array declared? l what number do indexes start from? l what is out-of-range error? is it a syntax error or a bug? l can arrays be passed as arguments to functions? If yes by value or by reference? If yes, how is size of array passed? what does const mean in the following declaration? void myfunc(const int []); l how is array initialized at declaration? Review Questions 11