CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Computer Programming w/ Eng. Applications
Introduction to Programming Lecture 39. Copy Constructor.
Strings Input/Output scanf and printf sscanf and sprintf gets and puts.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
Kernighan/Ritchie: Kelley/Pohl:
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
CIS 101: Computer Programming and Problem Solving Lecture11 Usman Roshan Department of Computer Science NJIT.
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
CIS 101: Computer Programming and Problem Solving Lecture 6 Usman Roshan Department of Computer Science NJIT.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
1 String Library and Stream I/O Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.
CIS 101: Computer Programming and Problem Solving Lecture 2 Usman Roshan Department of Computer Science NJIT.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
1 Array, Pointer and Reference ( I ) Ying Wu Electrical Engineering and Computer Science Northwestern University EECS 230 Lectures.
CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Lecture No.01 Data Structures Dr. Sohail Aslam
CS 450 MPX P ROJECT A Quick C Review. P OINTERS AND ADDRESSES IN C Check Manual I2 from Dr. Mooney’s website for a complete review of C topics you will.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
LAB#1 : Arrays & Functions. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays Arrays.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
1 CSC241: Object Oriented Programming Lecture No 22.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 3 – August 28, 2001.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Array Lists Array Lists Dale.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
ENEE150 – 0102 ANDREW GOFFIN More With Pointers. Importance of Pointers Dynamic Memory (relevant with malloc) Passing By Reference Pointer Arithmetic.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
Week 6 - Friday.  What did we talk about last time?  Pointers  Passing values by reference.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Lecture2.
5. Arrays, Pointers and Strings
Object Oriented Programming COP3330 / CGS5409
More About Data Types & Functions
CSC215 Homework Homework 06 Due date: Oct 30, 2016.
Topics discussed in this section:
C Programming Lecture-8 Pointers and Memory Management
CS250 Introduction to Computer Science II
Characters and Strings
Presentation transcript:

CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT

Dynamic arrays int *x; x = new int[3]; x[0] = 2; x[2] = 4; x[3] = 6; Memory x 50 2 x[0] 46 x[1] x[2]

Dynamic arrays

Memory defined using new must be cleared up using delete. Otherwise your program may use up ALL the memory. int *x = new int[10];. delete x;

Two dimensional arrays 2D arrays are defined as [ ][ ]; For example, int A[10][20] allocates space for a 2-D array of 200 integers. Any cell of the array can be accessed using A[i][j] where is between 0 and 9 (0 and 9 inclusive) and j is between 0 and 19 (0 and 19 inclusive).

Strings Strings are arrays of characters. A character x is defined using char x; We can set x to say ‘A’ using x=‘A’; Strings are just arrays of characters. So if we want to create a string of length 10 called mystring we would define it as char mystring[10]; If we want to initialize mystring to say “Hello” we would define mystring as char mystring[10] = { ‘H”, ‘e’, ‘l’, ‘l’, ‘o’ };

Creating strings

Output

Dynamic strings char *x; x = new char[5]; x[0] = ‘H’; x[1] = ‘e’; x[2] = ‘l’; x[3] = ‘l’; x[4] = ‘o’; Memory x 50 H x[0] el x[1] l o x[2] x[3] x[4]

Dynamic strings

Output Garbage characters

Dynamic strings We need a null character to specify the end of the string. The length of the string is increased by one for the null character.

Output

Dynamic arrays You cannot print an entire integer array in the same manner. This code will just output the value of the pointer x, which is just a memory location.

Output

Object Oriented Programming You can imagine integers, characters, and arrays as objects. They contain data on which various operations can be performed. We can define new objects which contain more data and new operations on them. Integers, characters, floats (real numbers), and pointers are fundamental data types (or fundamental objects if you like). These can be used to create new objects. These new objects are created using a special type of variable called class. A class can contain several fundamental data types and even other pre-defined classes.

Classes Let’s say we want to define a rectangle object. This is the same defining a new type of variable called rectangle. Previous types we have seen so far are int, char, float, and their pointers. int length; int width; int area(); float diagonal(); This is our rectangle class. it contains the length and width, and also functions to compute the area and diagonal. Rectangle

Classes Variables and functions in classes fall into two categories: private and public. Public variables and functions can be accessed by any function in your program. Private variables can only be accessed by other functions of the class. Let’s look at an example to understand this better.

Defining classes class { public: ; … private: … };

Defining the rectangle class Defining length and width variables Defining function to compute area and diagonal of rectangle. There are no parameters because the width and length are defined in the class.

Defining the rectangle class Setting length and width of rectangle Computing area and diagonal of rectangle. Since area and diagonal is previously defined in rectangle class, we don’t have to redefine it.

Output Now what is we changed the public to private?

Rectangle class Now length and width are private? This means they cannot only Be accessed by functions defined In the class. Compilation produces two errors.

Rectangle class Says it cannot access private variables. If we want to keep these private we can define new functions to set the value of length and width.

Rectangle class We have defined two new functions to set the length and width of the rectangles. We use them to set length and width.

Lab problems 1.Update the rectangle class: 1.Write a class function which adds one rectangle into another. 2.Set length and width in one function instead of two. 3.Update rectangle to 3 dimensions---add new variable for height and modify rectangle and diagonal functions 2.Write a copy array function 3.Create the following classes: 1.Circle 2.Sphere 3.String 4.3-D vector class 4.Problems from midterm