Previous Lecture Review

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.
 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.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
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 © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
 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 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.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
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?
ARRAYS.
Test 2 Review Outline.
Review 1.
What Is? function predefined, programmer-defined
Chapter 6 Arrays in C++ 2nd Semester King Saud University
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Arrays Low level collections.
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.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Pointers and Pointer-Based Strings
New Structure Recall “average.cpp” program
Pointers Revisited What is variable address, name, value?
C++ Arrays.
Dynamic Memory Allocation
Multiple Files Revisited
Engineering Problem Solving with C++, Etter/Ingber
Array Data Structure Chapter 6
7 Arrays.
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Arrays Kingdom of Saudi Arabia
Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.
Namespaces How Shall I Name Thee?.
Review of Everything Arrays
Fundamental Programming
CMSC202 Computer Science II for Majors Lecture 03 – Arrays and Functions Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
7 Arrays.
Array Data Structure Chapter 6
Pointers and Pointer-Based Strings
Arrays Arrays A few types Structures of related data items
Multiple Files Revisited
What Is? function predefined, programmer-defined
C++ Array 1.
ICS103 Programming in C Lecture 12: Arrays I
Programming Fundamental
Arrays, Part 1 of 2 Topics Definition of a Data Structure
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:

Previous Lecture Review what is a void-function? what is a predicate? how can a predicate be used? what is program stack? function frame? what’s call-by-value? what’s call-by-reference? how are the two different syntactically? can expression be passed by reference?

aggregating varabiles Arrays aggregating varabiles

Array Terms aggregate construct – a construct that allows manipulation of multiple entities as a single entity array - a collection of variables called (array) elements or indexed variables array is an aggregate construct elements have two names name of the array – the same for all elements of single array index (or subscript) - different for element, put in square brackets [] example: array score may have following elements: …, score[2], score[3], score[4], … (array) base type – type of the array elements all elements have the same type array size – number of elements array declaration: int score[5]; the number in brackets: 5 is array size 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! scalar variable – non array (regular) variable

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

Array Usage indexed variable 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]; index can be an expression: int student=2; score[student]=99; score[student+1]=100; cout << score[1] << ” ” << score[student]; loops are ideal for arrays: for (int index=0; index < arraySize; ++index){ // do something with score[index] }

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

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] 20 myvar other vars array array elements are placed in memory consequently: int a[10], myvar=20; no range checking is done on index out-of-range error – referring to index that is not in array it is a logical error (bug/run-time error) with unpredictable consequences. these are both out-of-range errors a[10]=55; a[myvar]=5;

Initializing Array what is initialization again? array elements can be initialized at once: int a[10]={0, 10, 20, 30, 40, 50, 60, 70, 80, 90}; do not have to initialize all elements (rest hold zeroes): int a[10]={0, 10, 20}; may skip array size at initialization (size computed to hold all values) int a[]={10,20,30}; // array size is three using named constants for array size is good style: const int numStudents = 55; int score[numStudents];

Arrays and Functions 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? entire array can be passed as argument always passed by reference (no & needed) 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 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

const Parameter Type Modifier array is always passed by reference may lead to accidental value changes: run-time error const type modifier specifies that the parameter shall not be modified in the function 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]; } function prototype and head have to agree on type modifiers function invocation is the same regardless of modifiers

Review Questions what is an aggregate construct? what is an array? what is name of the array? index? what is indexed variable? element of the array? scalar variable? what is array size? what is array’s base type? how is array declared? what number do indexes start from? what is out-of-range error? is it a syntax error or a bug? how is array initialized? 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 []);