2/17/2016IT 2791 Homogeneous aggregate // Using array string name[20]; int mid1[20]; int final[20]; double GPA[20]; name[0] = "Tom"; GPA[2] = 3.21; name:

Slides:



Advertisements
Similar presentations
CSE Lecture 12 – Linked Lists …
Advertisements

SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Strings.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Function tax and buy_one double tax (double price, double tr) { return price*tr; } double buy_one() { double p; cout > p;
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.
1 Homogeneous aggregate // Using array string name[20]; int mid1[20]; int final[20]; double GPA[20]; name[0] = "Tom"; GPA[2] = 3.21; name: Tom, John, student-3,
Lecture 03a: C++ classes Topics: basic classes and objects constructor, destructor Miscellaney An intro to the STL (Standard Template Library)
1 LoopsBranching Condition Statement list T F Condition Statement list T F.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Chapter 5 Arrays and Strings C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (4)
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
References types and Value types With a very brief introduction to struct and enum Reference types and Value types1.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
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.
C++ Tutorial Hany Samuel and Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2006 by Douglas.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
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.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
CS342 Data Structures End-of-semester Review S2002.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
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.
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.
CSci 162 Lecture 2 Martin van Bommel. Enumeration –process of listing all of the elements in the domain of a type Enumerated type –type defined via enumeration.
Data Types Storage Size Domain of all possible values Operations 1.
Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output.
2/19/2016IT 279, Chung-Chih Li1 Branching Condition Statement list 1 T F Statement list 2 Condition Statement list T F.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
Pointers & References. Pointers Pointer arithmetic Pointers and arrays Pointer-related typedef’s Pointers and const References.
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.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.
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.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
1 A Pointer: an address, a reference, a location of the computer memory A pointer of what? int, char, bool, double, or any kind of data type need to know.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Reminders: Have you filled out the questionnaire in Moodle? (3 left – as of last night). Office hours.
1 1  Lecture 11 – Structured Data FTMK, UTeM – Sem /2014.
Copyright © Curt Hill STL Priority Queue A Heap-Like Adaptor Class.
3/21/2016IT 2751 Tow kinds of Lists Array What can be done? What can be easily done? student 1 student 2 student 3 student 4 Linked List student 2 student.
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.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
Introduction to olympic programming
Popping Items Off a Stack Using a Function Lesson xx
Data Structure #1: The Array
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Popping Items Off a Stack Lesson xx
Chapter 9 One-Dimensional Arrays
Chapter 12 Pointers and Memory Management
Introduction to Programming
Homogeneous aggregate
Lab4 problems More about templates Some STL
Presentation transcript:

2/17/2016IT 2791 Homogeneous aggregate // Using array string name[20]; int mid1[20]; int final[20]; double GPA[20]; name[0] = "Tom"; GPA[2] = 3.21; name: Tom, John, student-3, student_4, , student-20 mid1: 70, 67, 86, 59, , 80 final: 69, 77, 79, 64, , 90 GPA: 3.02, 2.89, 3.21, 2.78, , 3.67 // Using tvector class #include "tvector.h" tvector name(20); tvector mid1(20); tvector final(20); tvector GPA(20); name[1] = "John"; GPA[19] = 3.67; in STL (Standard Template Library)

2/17/2016IT 2792 Search in a tvector (or an array) // Using tvector class #include "tvector.h" tvector name(20); tvector mid1(20); tvector final(20); tvector GPA(20);..... // What is Susan's GAP? for (int i=0; i < name.length(); i++) { if (name[i] == "Susan") cout << GPA[i]; } Sequentially

2/17/2016IT 2793 What can be in an Array // Using tvector class..... struct student { string name; int mid1; int final; double GPA; }; struct student class279[20];..... // What is Susan's GAP? for (int i=0; i < class279.length(); i++) { if ( (class279[i]).name == "Susan") cout << (class279[i]).GPA; } A bit old fashioned

2/17/2016IT 2794 The index can be an Enumerated Type enum day {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}; tvector MyclassHr(7); MyclassHr[Monday] = 2; MyclassHr[Tuesday] = MyclassHr[Thursday] = 0; MyclassHr[Wednesday] = 4; enum day ThreeDays[3]; enum day ADay; ThreeDay[0]=Saturday; if (ADay == Saturday || ADay == Sunday) cout << "It's weekend"; day FirstDay = day(0); Sunday Monday Tuesday Wednesday Thursday Friday Saturday Monday Tuesday Wednesday Thursday Friday Saturday Sunday

2/17/2016IT 2795 Two dimensional array, Matrix // Using array int A[3][3]; int B[3][3]; int C[3][3]; A[1][2] = 3; A[1][1] = 1; C[2][1] = A[2][1]+B[2][1]; // Using apmatrix class #include "apmatrix.h" apmatrix A(3,3); apmatrix B(3,3); apmatrix C(3,3); A[1][2] = 3; A[1][1] = 1; C[2][1] = A[2][1]+B[2][1]; =

2/17/2016IT 2796 in STL (Standard Template Library) #include struct student { string name; int ssn; }; student s; vector IT279; while (true) { cout << "Input SSN:"; cin >> s.ssn; if (s.ssn == 0) break; cout << "Input name:"; cin >> s.name; IT279.push_back(s); } for (int i=0;i< IT279.size();i++) { cout << "\nName:" << IT279[i].name << " "; cout << "SSN:" << IT279[i].ssn; } student 1 student 2 student 3 student 4 IT279 IT279[3] IT279[2] IT279[1] IT279[0]

2/17/2016IT 2797 Resize a vector student 1 student 2 student 3 student 4 CS CS[3] CS[2] CS[1] CS[0] CS[4] CS[5] the last push back here CS.resize(6)

2/17/2016IT 2798 Reserve a vector student 1 student 2 student 3 student 4 CS CS[3] CS[2] CS[1] CS[0] CS[4] CS[5] the last push back here CS.resize(6) CS.reserve(8)

2/17/2016IT 2799 pop back a vector student 1 student 2 student 3 student 4 CS2 CS2[3] CS2[2] CS2[1] CS2[0] CS2[4] CS2[5].... CS2.resize(6); CS2.pop_back(); reserved size

2/17/2016IT Stack and Queue Stack: LCFS (Last Come First Service) c1c2 c3 c5 c4 c2c3c4 c5 c1 Queue: FCFS (First Come First Service) Front Back

2/17/2016IT in STL (Double Ended Queue) #include deque CS2; while (true) { cout << "Input SSN:"; cin >> s.ssn; if (s.ssn == 0) break; cout << "Input name:"; cin >> s.name; CS2.push_front(s); CS2.push_back(s); } for (int i=0;i<CS2.size();i++) { cout << "\nName:" << CS2[i].name << " "; cout << "SSN:" << CS2[i].ssn; } student 2 student 1 student 2 CS2 CS2[3] CS2[2] CS2[1] CS2[0]

2/17/2016IT pop back and pop front a deque student 1 student 2 student 3 student 4 student 5 CS2 CS2[3] CS2[2] CS2[1] CS2[0] CS2[4].... CS2.pop_front(); CS2.pop_back(); student 1 student 2 student 3 student 4 student 5 CS2 CS2[2] CS2[1] CS2[0] CS2[4]

2/17/2016IT A Pointer: an address, a reference, a location of the computer memory A pointer of what? int, char, bool, double, or any kind of data type need to know so the computer knows how to read or store the contents of the memory. Can be

2/17/2016IT // define pointers int a, *ap; // can’t use a and *a at the same time; double b, *bp; token k, *kp; a is an integer, ap is a pointer to an integer; b is a double, bp is a pointer to a double; k is a token, kp s a pointer to a token. Define pointers

2/17/2016IT A Pointer points to a location of the computer memory int a=5; int *ap; double b=2.3; double *bp; token k; token *kp; bp = new double; *bp = 3.14; *ap = 7; // not new-ed yet a ap b bp k kp 4 bytes } ? ? FFFFA080 FFFFA

2/17/2016IT A Pointer points to a location of the computer memory double a=5.1; double *ap; double *b,*c; ap = &a; // &a :the address of a *ap=1.7; b = new double; *b = 3.14; b = &a; c = b; a ap b c 4 bytes } new FFFFA080 FFFFA

2/17/2016IT The name of an array is a pointer int a[4]={5,60,700,8000}; int *p; p=a; cout << *p << endl; cout << *p+1 << endl; cout << *(p+1) << endl; cout << *(p+2) << endl; cout << *(p+3) << endl; cout << *(p+4) << endl; apap 4 bytes } FFFFA080 FFFFA

2/17/2016IT Dynamic arrays, resizable arrays p = new int[3]; for (int i=0; i<3; i++) p[i] = i; 4 apaapa p = new int[a]; for (int i=0; i<4; i++) p[i] = i*i; int a[3]={5,60,700}; int *p,a=4;

2/17/2016IT Create and Delete Dynamic arrays int a[3]={5,60,700}; int *p,a=4; p = new int[3]; for (int i=0; i<3; i++) p[i] = i; delete [] p; p = new int[4]; for (int i=0; i<4; i++) p[i] = i*i; a p a

2/17/2016IT Forget to Delete Dynamic arrays int a[3]={5,60,700}; int *p,i; p = new int[3]; for (i=0; i<3; i++) p[i] = i; // delete [] p; p = new int[4]; for (i=0; i<4; i++) p[i] = i*i; a p i

2/17/2016IT An array  pointers int a[4]={0,1,4,9} int *p; p = new int[3]; for (i=0; i<=3; i++) { p[i] = i; *(p+i) *= 2; } // a[4] = 16; *a = a[0]*a[0]; *(a+1) = a[1]*a[1]; *(a+2) = a[2]*a[2]; *(a+3) = a[3]*a[3]; //*(a+4) = a[4]*a[4]; apap X X

2/17/2016IT Arrays  Pointers int *a[4]; int **p; a[1] = new int[2]; a[2] = new int[3]; a[3] = new int[3]; p = new int*[3]; p[2] = new int[2]; p[2][0]=5; p[2][1]=7; apap 22 a[0] a[1] a[2] a[3] p[0] p[1] p[2]

2/17/2016IT Call by value double fun(int n, double p) { for (int i=0; i < 2; i++) p = p-n; return p; } npinpi int main() { int i=1,k=4; double p, s=123.4; p = fun(i+k,s); cout << i << “\n“ << k << “\n“ << p << “\n“ << s; } ikpsikps 1+4=

2/17/2016IT Call by reference double fun(int n, double & p) { for (int i=0;i < 2;i++) p = p-n; return p; } npinpi int main() { int i=1,k=4; double p, s=123.4; p = fun(i+k,s); cout << i << “\n“ << k << “\n“ << p << “\n“ << s; } ikpsikps &s 1+4=

2/17/2016IT Call by Value vs. Call by Reference int add_one_V(int n) { n++; cout << “1:“ << n << endl; return n; } 5 n int main() { int a,b=5; a = add_one_V(b); cout << “2:“ << a << “ “ << b << endl; a = sub_one_R(b); cout << “3:“ << a << “ “ << b << endl; } 1:6 2:6 5 3:4 4 abab int sub_one_R(int & n) { n--; return n; } n &b

2/17/2016IT Passing Arrays as Parameters int top(int a[]) { int i=0; return a[i]; } 0 aiai int main() { int a[4]={10,200,300,400}; cout << top(a) << endl; cout << third(a) << endl; } a int third(int a[]) { int i=2; return a[i]; } 2 aiai a+2

2/17/2016IT Arrays passed as References void fun(int a[]) { int i=2; a[i] += 5; } 2 aiai int main() { int a[4]={10,200,300,400}; fun(a); cout << a[0] << “ “; cout << a[1] << “ “; cout << a[2] << “ “; cout << a[3] << endl; return 1; } a a[2]

2/17/2016IT Passing the size of an Array int main() { const int size=4; int a[size]={1,2,3,4}; add_one_to_all(a,size); for (int i=0;i<size;i++) cout << a[i] << “ “; return 1; } 4 0 size i a 1 2 void add_one_to_all(int A[], int size) { for (int i=0;i<size;i++) A[i]++; } 0 4 i A size

2/17/2016IT Vector passed as Value ? (right? wrong?) int main() { vector a(4); int i; for (i=0;i<a.size();i++) a[i]=i+1; add_one_to_all(a); for (i=0;i<a.size();i++) cout << a[i] << “ “; } iaia void add_one_to_all(vector v) { for (int i=0;i<v.size();i++) v[i]++; } iviv

2/17/2016IT Vector passed as Reference int main() { vector a(4); for (int i=0;i<a.size();i++) a[i]=i+1; add_one_to_all(a); for (int i=0;i<a.size();i++) cout << a[i] << “ “; } iaia 1 2 void add_one_to_all(vector & v) { for (int i=0;i<a.size();i++) a[i]++; } 0 iviv &a

2/17/2016IT C-string is an array of char char *a = "Dennis"; char b[7] = {'D','e','n', 'n','i','s','\0'}; char *c=b; cout << *a << “ “ << b[0] << endl; cout << *(a+2) << “ “ << b[2] << endl; cout << a << “ “ << b << endl; char *d[2] = {c, "Li"}; cout << d[0] << " " << d[1] << endl;; abcdabcd 4 bytes } D n Dennis Dennis Li } 1 byte ‘D’ ‘e’ ‘n’ ‘i’ ‘s’ 0 ‘L’ ‘i’ d[0] d[1] ‘D’ ‘e’ ‘n’ ‘i’ ‘s’ 0