CS201 – Introduction to Computing – Sabancı University 1 Built-in Arrays l C++ native array type (not the class version) l Two versions ä fixed size arrays.

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 39. Copy Constructor.
Advertisements

F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
CS201 – Introduction to Computing – Sabancı University 1 First Midterm Exam l November 22, 2008, Saturday, 10:40 – 12:20, max 100 minutes l One A4 size.
Parameter Passing Mechanisms Reference Parameters.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
CMPUT 101 Lab #6 October 29, :00 – 17:00. Array in C/C++ Array is a structure type variable. One dimension of array int: int num[3]; There are.
Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Lesson 7 Arrays CS 1 Lesson 7 -- John Cole1. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored.
Review of C++ Programming Part II Sheng-Fang Huang.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
Searching a vector We can search for one occurrence, return true/false or the index of occurrence Search the vector starting from the beginning Stop searching.
 2006 Pearson Education, Inc. All rights reserved Arrays.
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
C++ Lecture 3 Monday, 14 July Arrays, Pointers, and Strings l Use of array in C++, multi- dimensional array, array argument passing l Pointers l.
Arrays and Vectors Sequential (One-Dimensional) Containers Chapter 12 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Reference parameters (6.2.3) We might need a function to return more than one value Find roots of a quadratic 2 return values. What are they? Get first.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
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.
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.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 7 Arrays.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
Array Declarations Arrays contain a fixed number of variables of identical type Array declaration and allocation are separate operations Declaration examples:
Arrays.
Chapter 7 Arrays Csc 125 Introduction to C++. Topics Arrays Hold Multiple Values Array Operations Arrays as function arguments Two-dimensional arrays.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
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)
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
Announcements Midterm 2 is on TODAY, December 19 th at 19:40. Extra Recitation TODAY at 15:40-18:30 in FENS L062 Midterm Places: if (LastName
CPS120 Introduction to Computer Science Exam Review Lecture 18.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
1 1  Lecture 11 – Structured Data FTMK, UTeM – Sem /2014.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings.
Chapter Structured Data 11. Combining Data into Structures 11.2.
Vectors of structs We can define vectors of structs struct student { unsigned int id; string name, lastname; double gpa; }; vector class(11); // a vector.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
Student Book An Introduction
CSCE 210 Data Structures and Algorithms
Announcements Midterm2 grades were announced
7 Arrays.
Standard Version of Starting Out with C++, 4th Edition
7 Arrays.
CS150 Introduction to Computer Science 1
Presentation transcript:

CS201 – Introduction to Computing – Sabancı University 1 Built-in Arrays l C++ native array type (not the class version) l Two versions ä fixed size arrays array size is fixed and must be specified as a constant expression at the declaration (determined during compile time) we will see this type now ä array pointers array size is dynamically allocated will not see in this course (will be covered in CS204) ä use of both types are the same except definition l vector versus built-in arrays ä vector is a class that is based on built-in arrays ä vector has member functions and operators, built-in arrays do NOT vector is more flexible, but slower

CS201 – Introduction to Computing – Sabancı University 2 Built-in Array declaration l As we said, we will discuss fixed size built-in arrays, not the pointer version with dynamic allocation ä size must be able to be determined at compile time constant, literal or an expression that involves constants and literals only #define ALPHABETSIZE 26 //compile directive preprocessed const int CLASSSIZE = 100; //constant declaration string names[CLASSIZE]; // array of 100 strings double grades[CLASSIZE*5]; // array of 500 doubles int list[200]; // array of 200 integers char abc[ALPHABETSIZE]; // array of 26 characters l The following array declaration is INVALID int size; cout "Enter how many students ? "; cin >> size; string names[size]; // array size cannot be a variable

CS201 – Introduction to Computing – Sabancı University 3 Built-in array initialization at declaration l You may specify a list of initial values at declaration. See following example string dayNames [] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};  dayNames is an array with 7 elements of type string 0 th element is “Sunday”, 1 st is “Monday”,... ä not necessary to specify size (which is 7), since the number of elements make the size clear but you can specify the size, if you wish string dayNames [7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

CS201 – Introduction to Computing – Sabancı University 4 Assignment rules in arrays l vectors with the same element type can be assigned to each other by = ä LHS vector becomes the same as the RHS vector size and capacity also become the same l Built-in arrays cannot be assigned to each other by = int coins[] ={1,5,10,25}; int temp[4]; temp = coins; // illegal temp[1] = coins[2]; // legal – array element assignment How can we assign coins to temp ? ä element by element for (i=0; i<4; i++) temp[i] = coins[i];

CS201 – Introduction to Computing – Sabancı University 5 Passing built-in arrays as parameters l A built-in array can be passed only as reference parameter or const-reference parameter ä cannot be passed as value parameter But, we do not use ampersand character, &, at parameter declaration ä and we do not specify the array size in array parameter  however array size is generally passed as another integer parameter since we do not have a size() member function for built-in arrays void Change(int list[], int numElts); void Print(const int list[], int numElts); reference parameter const-reference parameter

CS201 – Introduction to Computing – Sabancı University 6 Built-in array demo l See fixlist.cpp (slightly modified from the version in book) Why did we use const in Print ?  to avoid accidental changes in array list Why did we pass numElts as parameter for the number of elements in the array? ä because we don’t know the total number of elements in the array while writing the functions

CS201 – Introduction to Computing – Sabancı University 7 Example – Fibonacci numbers l Used in many areas of Mathematics and Computer Science F 0 = 1 F 1 = 1 F n = F n-1 + F n l You can see many examples of Fibonacci numbers in nature ä E.g. Increase of number of branches of trees in time ä See for more exampleshttp:// const int MAX_SIZE = 100; int list[MAX_SIZE]; int k; list[0] = list[1] = 1; for (k=2; k < MAX_SIZE, k++) { list[k] = list[k-1]+list[k-2]; }

CS201 – Introduction to Computing – Sabancı University 8 Use of strings as arrays l Characters in a string can be referred as an array using [ ] string s="cs201";... s[0] = 'n'; // makes 0th character of s 'n'... for (k=0; k<s.length(); k++) cout << s[k] << " "; l In general, s[k] means s.at(k)

CS201 – Introduction to Computing – Sabancı University 9 The Matrix To represent two dimensional arrays – We define a matrix as a vector of vectors vector > mat(rows,vector (cols)); vector > mat(3, vector (5)); mat[2][3] = 100; First index is for row, second is for column Number of rows Number of columns

CS201 – Introduction to Computing – Sabancı University 10 Possible Matrix definitions l Possible matrix declarations ä 4 different declarations vector > matrix_variable_name ; empty matrix (zero rows, zero columns) vector > matrix_variable_name ( rows ); matrix with rows rows; each row is an empty vector vector > matrix_variable_name ( rows, vector ( cols )); matrix with rows*cols elements (initialized via default constructor; if type is int, initialized to zero) vector > matrix_variable_name ( rows, vector ( cols, init_value )); matrix with rows*cols elements all initialized to init_value Use push_back to fill up

CS201 – Introduction to Computing – Sabancı University 11 To get the size of rows and columns matrix_variable_name.size() e.g. mymatrix.size() ä number of rows in matrix matrix_variable_name [0].size() mymatrix[0].size() ä number of columns in matrix  Instead of 0, any valid row index can be used, if each row has equal number of elements. Otherwise, the structure is not a matrix and it is out of scope of CS201 l Example: Let’s briefly check out matdemo.cpp; more detailed explanation will be in labs.