Fundamental Programming

Slides:



Advertisements
Similar presentations
Chapter 10.
Advertisements

Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
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.
Multiple-Subscripted Array
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
Arrays.
 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.
Fundamental Programming: Fundamental Programming Introduction to C++
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.
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.
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.
 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.
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. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Fundamental Programming Fundamental Programming Introduction to Functions.
© 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.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
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.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Objectives You should be able to describe: One-Dimensional Arrays
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
310201: Fundamental Programming Fundamental Programming Introduction to Arrays.
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.
EGR 2261 Unit 9 One-dimensional Arrays
© 2016 Pearson Education, Ltd. All rights reserved.
Pointers and Pointer-Based Strings
New Structure Recall “average.cpp” program
Student Book An Introduction
C++ Arrays.
DATA HANDLING.
Array Data Structure Chapter 6
Previous Lecture Review
Arrays Skill Area 315 Part A
7 Arrays.
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Pointers, Dynamic Data, and Reference Types
Arrays Kingdom of Saudi Arabia
6 Chapter Functions.
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Declaration, assignment & accessing
CPS120: Introduction to Computer Science
CMSC202 Computer Science II for Majors Lecture 03 – Arrays and Functions Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
7 Arrays.
CS150 Introduction to Computer Science 1
Array Data Structure Chapter 6
CS150 Introduction to Computer Science 1
Pointers and Pointer-Based Strings
Fundamental Programming
Suggested self-checks: Section 7.11 #1-11
Arrays Arrays A few types Structures of related data items
CS250 Introduction to Computer Science II
CS150 Introduction to Computer Science 1
C++ Array 1.
Pointers, Dynamic Data, and Reference Types
Arrays.
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

310201 Fundamental Programming More on Arrays

Review simple variables hold a single value in the previous class we saw that arrays hold a collection of related values also in the last class, we saw the basics of declaring, initialising and using arrays in a C++ array, the first value in an array is held at position 0, not 1

More on Arrays in this class we cover some more of the details of implementing arrays in C++ some jargon more on initialisation more on referencing arrays a common error using arrays passing arrays to functions passing array elements to functions

Some Jargon Used With Arrays element : a value held in an array index : an integer value that refers to an element in an array; we say C++ arrays are zero- indexed – the first position is zero subscript : can be used instead of index – as in “subscript out of range”; can also be used to describe the expression that appears between the brackets to refer to an element in an array dimension : the number of elements in an array - the size of the array – as in “the dimensions of my office are 3 meters by 4; the room size” How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.

More on Initialising Arrays if the array size is omitted when it is declared, the compiler can automatically determine the size from initial values int Primes[] = {1, 3, 5, 7, 11}; is equivalent to int Primes[5] = {1, 3, 5, 7, 11}; How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.

More on Referencing Arrays we normally use an integer literal, or an integer variable, to refer to a value of interest in an array Results[0] Results[ResultNbr] also, an integer expression can be used Results[CurrentPos - 1] How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.

A Common Error Using Arrays a common error when using arrays is to refer to an element that does not exist that is: to use an index value that is outside the acceptable range... How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.

Activity can you see how this code refers to an array element that does not exist? const NBR_RESULTS = 3; int Results[NBR_RESULTS] = {0}; for (int ResultNbr = 0; ResultNbr <= NBR_RESULTS; ResultNbr++) cout << Results[ResultNbr]; How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.

Activity Break

Activity Feedback in the last trip through the loop below, the code refers to Results[3], which does not exist const int NBR_RESULTS = 3; int Results[NBR_RESULTS] = {0}; for (int ResultNbr = 0; ResultNbr <= NBR_RESULTS; ResultNbr++) cout << Results[ResultNbr] << endl; How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.

A Common Error Using Arrays C++ does not give an error when using an index value that is out-of-range it is the programmer’s responsibility to ensure that array references are always within the acceptable range if, in an expression, you refer to an array element that does not exist: results are unpredictable – the program may give erroneous output, or go into an infinite loop How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.

A Common Error Using Arrays if you assign a value to an array element that does not exists: with luck, you’ll get a memory violation error – so you know there’s a problem without luck, results are unpredictable: erroneous output produced infinite loops lock-up the program memory used by the operating system is overwritten causing a system crash How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.

Passing Arrays to Functions arrays are always passed to functions by reference – but, no ampersand (&) appears before the parameter name consider a program using an array called Results as shown on the next slide... How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.

Passing Arrays to Functions main Results NbrResults Results Results NbrResults GetResults DisplayResults Results NbrResults SortResults How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.

notes: here, the maximum number of results is defined void GetResults(int Results[], int &NbrResults); void SortResults(int Results[], int NbrResults); void DisplayResults(int Results[], int NbrResults); const int MAX_NBR_RESULTS = 10; void main (void) { int Results[MAX_NBR_RESULTS] = {0}, NbrResults = 0; GetResults(Results, NbrResults); SortResults(Results, NbrResults); DisplayResults(Results, NbrResults); } How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area. notes: here, the maximum number of results is defined as a global constant – it is used to define the size of the Results array and will also be used to avoid an out-of-range error in the GetResults function

notes: the NbrResults parameter is an output of the void GetResults(int Results[], int &NbrResults); void SortResults(int Results[], int NbrResults); void DisplayResults(int Results[], int NbrResults); const int MAX_NBR_RESULTS = 10; void main (void) { int Results[MAX_NBR_RESULTS] = {0}, NbrResults = 0; GetResults(Results, NbrResults); SortResults(Results, NbrResults); DisplayResults(Results, NbrResults); } How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area. notes: the NbrResults parameter is an output of the GetResults function, so an ampersand appears before its name in the GetResults header and declaration

notes: the Results array is also an output of the void GetResults(int Results[], int &NbrResults); void SortResults(int Results[], int NbrResults); void DisplayResults(int Results[], int NbrResults); const int MAX_NBR_RESULTS = 10; void main (void) { int Results[MAX_NBR_RESULTS] = {0}, NbrResults = 0; GetResults(Results, NbrResults); SortResults(Results, NbrResults); DisplayResults(Results, NbrResults); } How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area. notes: the Results array is also an output of the GetResults function, but an ampersand does not appear before its name in GetResults header and declaration

notes: square brackets appear in header and declaration void GetResults(int Results[], int &NbrResults); void SortResults(int Results[], int NbrResults); void DisplayResults(int Results[], int NbrResults); const int MAX_NBR_RESULTS = 10; void main (void) { int Results[MAX_NBR_RESULTS] = {0}, NbrResults = 0; GetResults(Results, NbrResults); SortResults(Results, NbrResults); DisplayResults(Results, NbrResults); } How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area. notes: square brackets appear in header and declaration to show that a parameter is an array

notes: square brackets do not appear in the function call void GetResults(int Results[], int &NbrResults); void SortResults(int Results[], int NbrResults); void DisplayResults(int Results[], int NbrResults); const int MAX_NBR_RESULTS = 10; void main (void) { int Results[MAX_NBR_RESULTS] = {0}, NbrResults = 0; GetResults(Results, NbrResults); SortResults(Results, NbrResults); DisplayResults(Results, NbrResults); } How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area. notes: square brackets do not appear in the function call

notes: one does not need to specify the size of an array void GetResults(int Results[], int &NbrResults); void SortResults(int Results[], int NbrResults); void DisplayResults(int Results[], int NbrResults); const int MAX_NBR_RESULTS = 10; void main (void) { int Results[MAX_NBR_RESULTS] = {0}, NbrResults = 0; GetResults(Results, NbrResults); SortResults(Results, NbrResults); DisplayResults(Results, NbrResults); } How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area. notes: one does not need to specify the size of an array that appears as a parameter in a function header or declaration – it is normal practice to not specify the size of a parameter that is an array

notes: however, called functions need to know how many void GetResults(int Results[], int &NbrResults); void SortResults(int Results[], int NbrResults); void DisplayResults(int Results[], int NbrResults); const int MAX_NBR_RESULTS = 10; void main (void) { int Results[MAX_NBR_RESULTS] = {0}, NbrResults = 0; GetResults(Results, NbrResults); SortResults(Results, NbrResults); DisplayResults(Results, NbrResults); } How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area. notes: however, called functions need to know how many values have been stored in the array

Passing Arrays to Functions the DisplayResults function may look like this… void DisplayResults(int Results[], int NbrResults) { cout << "Results: " << endl; for (int ResultNbr = 0; ResultNbr < NbrResults; ResultNbr++) cout << " Result[" << ResultNbr << "]: " << Results[ResultNbr] << endl; } How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.

Passing Arrays to Functions it is possible to protect the elements of an array passed as an input to a function: put const in front of the parameter declaration any attempt by the function to assign a value to the array will result in an error when compiled How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area. void DisplayResults(const int Results[], int NbrResults)

Activity an interesting, and timely, example of passing an array to a function is shown on the next slide what output do you think this program will produce? How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.

#include <iostream.h> void Transform(char Array[], int NbrElements); void main (void) { const int NBR_CHARS = 3; char Name[NBR_CHARS] = {'I','B','M'}; int Index; cout << "Name before Transform is: " ; for (Index = 0; Index < NBR_CHARS; Index++) cout << Name[Index]; Transform(Name, NBR_CHARS); cout << endl << "Name after Transform is: " ; cout<<Name[Index]; } void Transform(char Array[], int NbrElements) for (int Index = 0; Index < NbrElements; Index++) --Array[Index]; How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.

Activity Break

Activity Feedback output produced by this program is: recall that characters are stored as an integer - the following will output H char MyChar = 'I'; cout << --MyChar; you may know that the computer in 2001: A Space Odyssey was called HAL Name before Transform is: IBM Name after Transform is: HAL

Passing Arrays Elements to Functions the previous example showed how to pass an entire array to a function we can also pass a single array element to a function whole arrays are always reference parameters individual elements of an array can be passed as a value or reference parameter How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.

Activity what output do you think the following code will produce? How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.

Activity #include <iostream.h> void PassByValue(int ArrayElement); void PassByReference(int &ArrayElement); void main(void) { int Marks[3] = {10,20,30}; PassByValue(Marks[0]); PassByReference(Marks[1]); for (int Mark = 0; Mark <= 2; Mark++) cout << “Marks[“ << Mark << “]: “ << Marks[Mark] << endl; } void PassByValue(int ArrayElement) ArrayElement++; void PassByReference(int &ArrayElement) How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.

Activity Break

Activity Feedback output produced by this program is: Marks[0]: 10

Activity write a function named Smallest, that accepts an array of double precision real numbers, and returns the smallest value stored in the array

Activity Break

Summary in this class we covered: some of the jargon used with arrays automatic sizing of arrays from initial values non-integer array references referencing non-existing array elements assignment to non-existing array elements passing arrays to functions passing arrays elements to functions