 CS105 C++ Lecture 2 Arrays. Parameter passing  2 types  Pass-by-value  Pass-by-reference 2.

Slides:



Advertisements
Similar presentations
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
Advertisements

C++ Spring 2000 Arrays1 C++ Arrays. C++ Spring 2000 Arrays2 C++ Arrays An array is a consecutive group of memory locations. Each group is called an element.
 2003 Prentice Hall, Inc. All rights reserved Introduction Arrays –Structures of related data items –Static entity (same size throughout program)
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
#include using namespace std; void main() { int a[3]={10,11,23}; for(int i=0;i
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
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 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
Chapter 9: Arrays and Strings
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
C++ Functions CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Chapter 8 Arrays and Strings
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.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
CS 31 Discussion, Week 6 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
C Static Arrays Pepper. What is an array? Memory locations – same type – next to each other (contiguous) – Same name – Indexed by position number of type.
 2006 Pearson Education, Inc. All rights reserved Arrays.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Chapter 8 Arrays and Strings
ITEC 320 C++ Examples.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
CSIS 113A Lecture 8 Parameters.  Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
1 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
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.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
EC-111 Algorithms & Computing Lecture #6 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
 2003 Prentice Hall, Inc. All rights reserved Storage Classes Variables have attributes –Have seen name, type, size, value –Storage class How long.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
CSIS 113A Lecture 10 Arrays Glenn Stevenson CSIS 113A MSJC.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
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.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Pass-by-Value - default passing mechanism except.
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
C Arrays.
7 Arrays.
Arrays Kingdom of Saudi Arabia
Variables have attributes
7 Arrays.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Arrays Arrays A few types Structures of related data items
CS31 Discussion 1H Fall18: week 6
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

 CS105 C++ Lecture 2 Arrays

Parameter passing  2 types  Pass-by-value  Pass-by-reference 2

Parameter passing  2 types  Pass-by-value  Argument copied  Caller and callee each have own copy  Pass-by-reference  Only 1 copy! Caller and callee share it  Beware – it can be modified by everyone 3

Pass-by-value int squareByValue(int number) { return number *= number; } int main() { int x = 3; int x_squared = squareByValue(x); //what is x and what is x_squared? } 4 Return: main Parameters: number Locals: (none) squareByValue Parameters: (none) Locals: x, x_squared main 3 3 9

Pass-by-reference void squareByReference(int &number) { number *= number; } int main() { int x = 3; squareByReference(x); //what is x? } 5 Return: main Parameters: number Locals: (none) squareByReference Parameters: (none) Locals: x main 39

Pass-by-reference  void myFunction(int &x);  x is a reference to an int.  Kinda like sharing the address of x with myFunction.  Be careful – you are giving callee method power to change your variable  To save copying space, but protect your variable, use const  void myFunction(const int &x);  Now myFunction cannot modify x. 6

References as Aliases int count = 1; int &cRef = count; cRef++; //count is ?  Reference variables must be initialized in their declaration and cannot be reassigned later. 7

Returning References  Returning references from a function is dangerous  Variable declared on stack cannot be returned  Can return static variable, but might not be what you want  Dangling reference = reference to undefined variable 8

Arrays!  Indexed data structure  Starts at zero!  How do we declare an array? 9

Arrays!  Indexed data structure  Starts at zero!  How do we declare an array?  type arrayName[arraySize];  Ex: int array[5]; 10

Array Initialization  Loop  Initializer list  Use const array size 11

Array Initialization  Loop int array[5]; for (int i = 0; i < 5; i++) { array[i] = i; }  Initializer list  Use const array size 12

Array Initialization  Loop  Initializer list  int array[5] = {99, 88, 77, 66, 55};  int array2[5] = {}; //what does this do?  int array[] = {44, 33, 22, 11};  Use const array size 13

Array Initialization  Loop  Initializer list  Use const array size const int arraySize = 10; int array[arraySize];  const variables must be initialized when declared, are constant  Only constants can be used to declare size of automatic and static arrays. WHY? 14

Differences from Java  No automatic “.length” for arrays  No guaranteed compiler array bounds checks – if you go outside [0 through (arraySize-1)], undefined behavior  Arrays always contiguous in memory 15

Character Arrays  char chArray[] = “hello”;  What size is the array above? 16

Character Arrays  char chArray[] = “hello”;  What size is the array above? 6  Char arrays are terminated with null character!  char chArray[] = {‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’}; 17

Strings  C++ does have string type  #include  string hello = “hello”;  Some useful string functions:  char chArray[] = hello.data( ); //get string’s char array (w/ ‘\0’)  hello.length(); //get length – returns unsigned int  char oneChar = hello[1]; //can index strings – but unchecked  char anotherChar = hello.at(1); //this is checked access  string wstr = “world”; hello.append(wstr, 0, wstr.length()); //append wstr onto hello to get “helloworld”  wstr.copy(chArray, wstr.length(), 0); //copy wstr string into char array – does *not* put ‘\0’ at end of chArray. 18

Character Arrays  char chArray[20];  cin >> chArray;  cin reads in a word (until whitespace) and appends null character to end  Make sure input from user <= 19 characters, otherwise error  For a line at a time:  cin.getline(chArray, chArraySize);  string myStr; getline(cin, myStr); 19 for (int i = 0; chArray[i] != ‘\0’; i++) { cout << chArray [i] << ‘ ‘; }

Passing Arrays to Functions void modifyArray(int b[], int arrSize) { for (int i = 0; i < arrSize; i++) { b[i] *= 2; } 20 void modifyArray(int [], int); int main() { const int arraySize = 5; int a[arraySize] = {0, 1, 2, 3, 4}; modifyArray(a, arraySize); return 0; }

Passing Arrays to Functions  Arrays are passed by reference (WHY?)  Name of array is address in memory of 1 st element  Need to pass size too – unlike Java  Use const to make sure function can’t change array  void cannotModifyArray(const int b[], int size);  const is like saying array is “read-only” 21 Otherwise copy whole array! If copy, need to specify size!

Static Local Arrays void staticArrayInit( void ) { static int array1[3]; for (int i = 0; i < 3; i++) { arrays1[i] += 5; cout << arrays1[i] << “ “; } } //what if array is not static? 22 void staticArrayInit(); int main() { staticArrayInit(); return 0; }

Multidimensional Arrays  int array[2][3] = {{1, 2, 3}, {4, 5, 6}};  int array[2][3] = {1, 2, 3, 4};  int array[2][3] = {{1, 2}, {4}};  Different from Java – contiguous in memory 23

Multidimensional Arrays  int array[2][3] = {{1, 2}, {4}};  Different from Java – contiguous in memory  Conceptually:  Actual layout: 24 [0][0] = 1[0][1] = 2[0][2] = 0 [1][0] = 4[1][1] = 0[1][2] = 0 [0][0] = 1 [0][1] = 2 [0][2] = 0 [1][0] = 4 [1][1] = 0 [1][2] = 0

Passing Multi-D Arrays void printArray( const int[][3], int numRows ); int main() { int array1[2][3] = {1, 2, 3, 4}; printArray(array1, 2); return 0; } 25 void printArray( const int[][3], //why? int numRows) { for (int i = 0; i < numRows; i++) { for (int j = 0; j < 3; j++) { cout << a[i][j] << ‘ ‘; } cout << endl; } 2 nd dimension needs to be known when passing to a function

2D as 1D array  int array1[2][3] = {}; 26 [0][0] = 0 [0][1] = 0 [0][2] = 0 [1][0] = 0 [1][1] = 0 [1][2] = 0  // to access array1[1][0] – we need to skip over 1 st row then go over to element 0 in second row  //number of entries per row = number of columns  array1[3 * 1 + 0] == array1[1][0];  //formula: numColumns * 1stIndex + 2ndIndex

typedef  typedef is a keyword that declares synonyms (aliases) for previously defined data types  Does not create a data type, it creates a type name (usually shorter, simpler) that maybe be used in the program  typedef unsigned long int ulint;  ulint myNum;  size_t is a typedef for unsigned int (used for string’s length()) 27

sizeof Operator  sizeof does exactly what you’d expect – give it a variable or type, it will return the size of it in bytes.  return type: not int, size_t (unsigned int) int x = 5; cout << sizeof x << endl; //can omit parens with variable cout << sizeof( int ) << endl; 28

sizeof Array int main() { double array[20]; cout << sizeof(array) << endl; //to get array size cout << (sizeof(array) / sizeof(double)) << endl; return 0; } 29

Extra Slides 30

Inputting from a File #include using namespace std; int main() { ifstream inputFile(”file.in", ios::in); if (!inputFile) { cerr << "File could not be opened" << endl; exit(1); //or return -1 } int numPpl; inputFile >> numPpl; cout << "Num people is " << numPpl << endl; return 0; } 31

Vector  #include  using std::vector;  vector integers1(5); //already initialized to zero  cout << integers1.size() << endl; //type is actually size_t  integers1[3] = 89; //will check bounds  vector integers2(integers1); //copies 1 into 2 32

Function Templates  Related to function overloading int boxVolume(int side) { return side * side * side; } double boxVolume(double side) { return side * side * side; }  Why define 2 functions that look identical, but have different types?  Overloading that is more compact and convenient = function templates. Only write it once! 33

Function Templates  Template template //or template T boxVolume(T side) { return side * side * side; }  C++ compiler automatically generates separate function template specializations for each type the function is called with.  T is placeholder for actual data type  int result = boxVolume(3); double result = boxVolume(6.2); 34