CSS 332 PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES LECTURE 3. 141010.

Slides:



Advertisements
Similar presentations
Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
Advertisements

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
CS 1620 File I/O. So far this semester all input has been from keyboard all output has been to computer screen these are just two examples of where to.
Computer Science 1620 Loops.
Wednesday, 12/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Wednesday, 12/11/02  QUESTIONS??  Today: CLOSING CEREMONIES!  HW #5 – Back Monday (12/16)
Chapter 5: Loops and Files.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
CS-2303 System Programming Concepts
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Basic Elements of C++ Chapter 2.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Java and C++, The Difference An introduction Unit - 00.
High-Level Programming Languages: C++
CIS Computer Programming Logic
1 Chapter 9 Scope, Lifetime, and More on Functions.
February 11, 2005 More Pointers Dynamic Memory Allocation.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Functions Why we use functions C library functions Creating our own functions.
C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2 Department of Computer Engineering 1.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
CSS 332 PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES LECTURE
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 5 An Array Class Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
File Input and Output in C++. Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
1 COMS 261 Computer Science I Title: String Class Date: October 3, 2005 Lecture Number: 14.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Object-Oriented Programming in C++
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CSS 332 PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES LECTURE
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation.
Week 2. Functions: int max3(int num1, int num2, int num3) {int result; result = max(max(num1,num2),num3); return result; } //max3.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE C++ INTERLUDE 1.3. C++ BOOK.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CH 2, APPENDIX E.
CS 11 C++ track: lecture 1 Administrivia Need a CS cluster account sysadmin/account_request.cgi Need to know UNIX (Linux)
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
CSS 332 PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES LECTURE
Loops and Files. 5.1 The Increment and Decrement Operators.
CS31: Introduction to Computer Science I Discussion 1A 5/14/2010 Sungwon Yang
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO , APP D, C++ BOOK.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO C++ INTERLUDE 2, CHAPT 4, 6.
CSS342: Introduction1 Professor: Munehiro Fukuda.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Module 5: I/O and Strings #1 2000/01Scientific Computing in OOCourse code 3C59 Module 5: I/O and STRINGS In this module we will cover The C++ input and.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Computer Programming in C++ 黃鐘揚 教授 Prof. Chung-Yang (Ric) Huang Department of Electrical Engineering National Taiwan University 2007/06/26.
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter Topics The Basics of a C++ Program Data Types
User-Written Functions
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Standard Input/Output Stream
CHAPTER 4 File Processing.
COMS 261 Computer Science I
Scope of Identifier The Scope of an identifier (or named constant) means the region of program where it is legal to use that.
Presentation transcript:

CSS 332 PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES LECTURE

Today’s Agenda Collect topics / questions Homework questions. Present / code snippets Arrays, Pointers, Dynamic memory Constructor Invocation Timing FileIO Recursion Assignment and Copy Constructor New :: returning an array More Pointers In-class assignment:

Homework questions.

Reverse an array (pointers) Write a function which reverses an array which only uses pointers, not the arr[] syntax

void ReverseArray(int *array, int length) { for (int i = 0; i < length / 2; i++) { int temp = *(array + i); *(array + i) = *(array + length i); *(array + length i) = temp; } void PrintArray(int *array, int length) { int *ind = array; for (int i = 0; i < length; i++) { cout << "array[" << i << "] = " << *ind << endl; ind++; }

Dynamic Memory Array Update your array program to take in input and dynamically allocate an array of this size

cout << "Input Size of Array: "; cin >> ArraySize; dynArr = new int[ArraySize]; for (int i = 0; i < ArraySize; i++) { dynArr[i] = i; } PrintArray(dynArr, ArraySize); ReverseArray(dynArr, ArraySize); cout << endl << "Reversed: " << endl; PrintArray(dynArr, ArraySize); ReverseArray(dynArr, ArraySize); cout << endl << "Reversed again: " << endl; PrintArray2(dynArr, ArraySize); delete(dynArr);

Time of Invocation Automatic Local Each time block is executed Static Local Once –first time it is hit Global In order of declaration in translation unit Typically before main() is entered Destroyed in reverse order of construction Dynamic (tbd) malloc/free new/delete

Invocation example Create a class Employee: name, number. Create a class Cubicle: width, length, Employee Optional: Office an array of Cubicles In constructors put a cout<< describing what is being created.

Computer Scientist of the week Sir Timothy Berners-Lee HTTP Protocol Unlike Al Gore, he can claim to have invented the world wide web Director of WWW Consortium Knighted in 2004

File IO In C:In C++:In Java: FILE *fp;ifstream inFile(“name.dat”);import java.io.*; orFileInputStream infile = ifstream inFile;new FileInputStream( “name.dat” ); if ((fp = fopen( “name.dat”, “r” ))inFile.open(“name.dat”);ObjectInputStream input = != NULL {if ( inFile ) { // true of falsenew ObjectInputStream( infile ); fscanf( fp, “%d”, &i );inFile >> i;try { i = input.readInt( ); fclose(fp);inFile.close( );} catch ( EOFException e ) { }}input.close( ); } Note: for output, use ofstream.

File IO example Read in form the console the name of a file. The file contains strings (names) which is your team’s roster. Read in your roster; close the file. User inputs to console a name to look for on the team. Confirm or deny.

cout << "Input file" << endl; cin >> fName; inFile.open(fName); while (getline(inFile, name)) { roster.push_back(name); } inFile.close(); cout << "Input player you are looking for: "; cin >> name; bool onTeam = false; for (int i = 0; i < roster.size(); i++) { if (roster[i] == name) { onTeam = true; break; } if (onTeam) { cout << name << " is on the team." << endl; } else { cout << name << " is not on the team." << endl; }

Recursive Solutions A recursive solution calls itself Each recursive call solves an identical, smaller problem Test for base case enables recursive calls to stop Eventually one of smaller calls will be base case DATA STRUCTURES AND PROBLEM SOLVING WITH C++: WALLS AND MIRRORS, CARRANO AND HENRY, © 2013

Recursion example 1 Write a recursive function which sums up all numbers from 1 to n. Write the same function iteratively. What is the difference in execution?

Babies Recursion 2: Multiplying Mice Month 1 Month 2 Month 4 Month 5 Month 3 Month 6 Month 7 Recursive relation: #babies in Month 7= #mice in Month 5 #adults in Month 7 = #mice in Month 6

Fibonacci Sequence mice(n) = mice(n-1) + mice(n-2) Base cases: mice(1) = 1; mice(2) = 1; Recursive: mice(n) = mice(n-1) + mice(n-2)

Today’s In-class Assignment. Continue last weeks Implement a team roster class The members of a team (Players) have a first name, last name, and number Use an Array as a data structure Read in players from a file Tony Blair 14 Spiro Agnew 7 Clint Dempsey 6 ….. overload << so that the roster is printed out in alphabetical order

break

Pointers 1.Pointer variablesint *p, *q; 2.Int allocationint x; 3.Address-of operatorp = &x; 4.Memory cell to which P points*p = 6; 5.Pointer operationsq = p; ??? pqx ?? pqx ?6 pqx 6 pqx

Pointer C++ Examples We’ll use a pointer.cpp file Compile with command line Bring into VS environment Play with pointers Why does this work: char* InitialInput = "Adam";

Double v. float

Char, Char*, fstream, array Write a program which reads a string names from a file into an array.

int defencesiveAttack() { int * pointer; int info[1]; pointer = &info[0]; // working out how much user will hit srand((unsigned int)time(0)); info[0] = (strenght+exp)*rand()%5+1; // working out the experience pionts for the hit info[1] = (info[0]/2)-exp; return pointer; } I am just starting off in c++ and I have function which I want to return two pieces of information from. So I make an array in the function and return it, but you cannot do that in c++ right. So instead I use a pointer to refer to the array in memory and then increment it to get the values right. I have googled this topic and still cannot get it to work. Here is my function, well method, its inside a class.