Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Programming and Data Structure
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Lecture 20 Arrays and Strings
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.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Pointers in C Rohit Khokher
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
Chapter 10.
CS 192 Lecture 12 Winter 2003 December 31, January 1, 2004 Dr. Shafay Shamail.
Thursday, January 11, 2007 Harrisberger's Fourth Law of the Lab: Experience is directly proportional to the amount of equipment ruined Harrisberger's Fourth.
1 11/27/06CS150 Introduction to Computer Science 1 Searching Arrays.
Friday, January 05, 2007 A few weeks of developing and testing can save a whole afternoon in the library. -Anonymous.
C-Strings A C-string (also called a character string) is a sequence of contiguous characters in memory terminated by the NUL character '\0'. C-strings.
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.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
CSSE221: Software Dev. Honors Day 28 Announcements Announcements Simulation grades coming back Simulation grades coming back All C Projects due Friday.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C Arrays and Pointers In Java, pointers are easy to deal with –In fact, there is little that can go wrong in Java since pointer access is done for you.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
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.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
Chapter 6 Pointers C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.
Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
Exam Format  90 Total Points  60 Points Writing Programs  25 Points Tracing Code/Algorithms and determining results  5 Points Short Answer  Similar.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Review Binary Numbers Bit : 0 or 1 Byte: 8 bites 256 different values 2 8 KB : 1024 bytes 2 10 bytes MB : 1024 * 1024 bytes 2 10 * 2 10 (2 20 ) bytes GB.
 Building blocks of a C++ program  Each function has a name, which is used to call the function; functions call each other  You will write your own.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
POINTERS.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
DCT1063 Programming 2 CHAPTER 1 POINTERS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
String operations. About strings To handle strings more easily, we need to include a library> #include To see what the library allows us to do, look here:
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
2/23/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 9 (C-3)
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.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Objectives You should be able to describe: One-Dimensional Arrays
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
C++ LANGUAGE MULTIPLE CHOICE QUESTION SET-3
Chapter 6 Arrays in C++ 2nd Semester King Saud University
Student Book An Introduction
Introduction to Programming
Lecture 6 C++ Programming
CS 2308 Exam I Review.
7 Arrays.
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Presentation transcript:

Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal

Write C++ code that prints out all of the powers of 2 from 2 to 128 i.e., Multiple cout statements are not acceptable, you must use some looping mechanism. SELF TEST

What is the output of the following program segment? int i, j, N=5; for(i=0; i<N; i++){ for(j=N-1; j>0; j--){ if(j>i) continue; cout<<j<<" "; } cout<<endl; } SELF TEST

int g=100; int main(){ int a=30; int b=40; cout<<a<<" "<<b<<" "<<g<<endl; function1(a,b); cout<<a<<" "<<b<<" "<<g<<endl; a=function2(a,b); cout<<a<<" "<<b<<" "<<g<<endl; b= a + function3(a,b); cout<<a<<" "<<b<<" "<<g<<endl; return 0;} int function1(int a, int b){ a=30; b=40; g=a-b; return a+b; } int function2(int a, int b){ g=b; a=3; b=4; return 2*b*a;} int function3(int a, int b){ g=50-a; a=b-a; b=3-a; return a+b; }

int g=100; int main(){ int a=30; int b=40; cout<<a<<" "<<b<<" "<<g<<endl; function1(a,b); cout<<a<<" "<<b<<" "<<g<<endl; a=function2(a,b); cout<<a<<" "<<b<<" "<<g<<endl; b= a + function3(a,b); cout<<a<<" "<<b<<" "<<g<<endl; return 0;} int function1(int a, int b){ a=30; b=40; g=a-b; return a+b; } int function2(int a, int b){ g=b; a=3; b=4; return 2*b*a;} int function3(int a, int b){ g=50-a; a=b-a; b=3-a; return a+b; }

int g=100; int main(){ int a=30; int b=40; cout<<a<<" "<<b<<" "<<g<<endl; function1(a,b); cout<<a<<" "<<b<<" "<<g<<endl; a=function2(a,b); cout<<a<<" "<<b<<" "<<g<<endl; b= a + function3(a,b); cout<<a<<" "<<b<<" "<<g<<endl; return 0;} int function1(int a, int b){ a=30; b=40; g=a-b; return a+b; } int function2(int a, int b){ g=b; a=3; b=4; return 2*b*a;} int function3(int a, int b){ g=50-a; a=b-a; b=3-a; return a+b; }

int g=100; int main(){ int a=30; int b=40; cout<<a<<" "<<b<<" "<<g<<endl; function1(a,b); cout<<a<<" "<<b<<" "<<g<<endl; a=function2(a,b); cout<<a<<" "<<b<<" "<<g<<endl; b= a + function3(a,b); cout<<a<<" "<<b<<" "<<g<<endl; return 0;} int function1(int a, int b){ a=30; b=40; g=a-b; return a+b; } int function2(int a, int b){ g=b; a=3; b=4; return 2*b*a;} int function3(int a, int b){ g=50-a; a=b-a; b=3-a; return a+b; }

int g=100; int main(){ int a=30; int b=40; cout<<a<<" "<<b<<" "<<g<<endl; function1(a,b); cout<<a<<" "<<b<<" "<<g<<endl; a=function2(a,b); cout<<a<<" "<<b<<" "<<g<<endl; b= a + function3(a,b); cout<<a<<" "<<b<<" "<<g<<endl; return 0;} int function1(int a, int b){ a=30; b=40; g=a-b; return a+b; } int function2(int a, int b){ g=b; a=3; b=4; return 2*b*a;} int function3(int a, int b){ g=50-a; a=b-a; b=3-a; return a+b; }

void f1(); int main() { char str[]="this is str in main()"; cout << str << '\n'; f1(); cout << str << '\n'; return 0; } void f1() { char str[80]; cout << "Enter something: "; cin >> str; cout << str << '\n'; } SELF TEST: FUNCTIONS : Local variables

int main() { char s1[80]="main string"; int choice; cout << "(1) add numbers or "; cout << "(2) concatenate strings?: "; cin >> choice; if(choice == 1) { int a, b; /* activate two integer vars */ cout << "Enter two numbers: "; cin >> a >> b; cout << "Sum is " << a+b << '\n'; char s1[80]="if block string"; cout << "s1= " << s1 << '\n'; } SELF TEST: Local to a block

else { char s1[80], s2[80]; /* activate two strings */ cout << "Enter two strings: "; cin >> s1; cin >> s2; strcat(s1, s2); cout << "Concatenation is " << s1 << '\n'; } int a=34; cout << "a= " << a << '\n'; cout << "s1= " << s1 << '\n'; return 0; } SELF TEST: Local to a block

int main() { int i, j; i = 10; j = 100; if(j > 0) { int i; // this i is separate from outer i i = j / 2; cout << "inner i: " << i << '\n'; } cout << "outer i: " << i << '\n'; return 0; } SELF TEST: Local to a block

void drill(); int count; // count and num_right are global int num_right; int main() { cout << "How many practice problems: "; cin >> count; num_right = 0; do { drill(); count--; } while(count); cout << "You got " << num_right << " right.\n"; return 0; } SELF TEST: Global Variables

void drill() { int count; /* This count is local and unrelated to the global one.*/ int a, b, ans; // Generate two numbers between 0 and 99. a = rand() % 100; b = rand() % 100; // The user gets three tries to get it right. for(count=0; count<3; count++) { cout << "What is " << a << " + " << b << "? "; cin >> ans; if(ans==a+b) { cout << "Right\n"; num_right++; return; } } cout << "You've used up all your tries. The answer is " << a+b << '\n'; } SELF TEST: Global Variables

Applications of sorting An important key to algorithm design is to use sorting as a basic building block, because once a set of items is sorted, many other problems become easy. Employee records, telephone books, tax records, student records, frequency distribution, CS192 grades…

Applications of sorting Sorting is fundamental to most other algorithmic problems, for example binary search. Speeding up searching is perhaps the most important application of sorting.

Searching § Sequential Search 3, 6, 7, 10, 14, 15, 19, 23, 26, 28, 31, 34, 35, 39, 43, 46, 47, 49, 51, 52, 57, 58, 60, 61, 65, 67, 70, 72, 73, 78, 85, 87, 88, 90, 93, 94, 97, 98, 102, 104, 105, 111, 120, …1000

Searching § Binary Search 3, 6, 7, 10, 14, 15, 19, 23, 26, 28, 31, 34, 35, 39, 43, 46, 47, 49, 51, 52, 57, 58, 60, 61, 65, 67, 70, 72, 73, 78, 85, 87, 88, 90, 93, 94, 97, 98, 102, 104, 105, 111, 120, …1000 Break problem down into a smaller problem

Strings char myString[80]="i am a string"; int i=0; while(myString[i]){ char Upper=myString[i]-(97-65); cout<<Upper; i++; }

Strings I AM A STRING

char words[3]; strcpy (words, “Now, here is a long string.”); What is wrong here?

char words[3]; strcpy (words, “Now, here is a long string.”); Will continue to fill whatever memory follows last indexed variable of words, even though this memory may be used for something else. What is wrong here?

char name[9]={'p', 'a', 'k', 'i', 's', 't', 'a', 'n'}; cout<<name<<endl; What is wrong here?

Arrays int a[10] = {1,2,3,4,0,3,4,6,7,8}; int x=3; y=1; cout<< a[x+2*y] <<“\n”; cout<<“Answer= “<<a[a[a[4]]]<<“\n”; Output?

Arrays int a[10] = {1,2,3,4,0,3,4,6,7,8}; int x=3; y=1; cout<< a[x+2*y] <<“\n”; cout<<“Answer= “<<a[a[a[4]]]<<“\n”; Output: 3 Answer= 2

The name of the array is the address of the first element. It is a constant. int a[5]={13, 5, 6, 34, 6}; cout<<a[1]<<endl; cout<<a[3]<<endl; cout<<a[4]<<endl; cout<<a<<endl;

The name of the array is the address of the first element. It is a constant x0012FEC4

char str[]="fruit"; cout<<str[1]<<endl; cout<<str[3]<<endl; cout<<str[4]<<endl; cout<<str<<endl;

r i t fruit

What is wrong with following? char str1[80]=“i am a string”; char str2[80]=“i am a string”; if (str1==str2) cout<<“equal”; else cout<<“not equal”;

What is wrong with following? char str1[80]=“i am a string”; char str2[80]=“i am a string”; if (str1==str2) cout<<“equal”; else cout<<“not equal”; // Use strcmp(str1, str2)

What is wrong with following? char str1[80]="cs192"; char str2[80]; str2=str1;

What is wrong with following? char str1[80]="cs192"; char str2[80]; str2=str1; //Error Use strcpy(str2, str1);

int twoD[4][2]={ 1, 10, 2, 13, 3, 34, 4, 15 }; Picture of array twoD[1][1] is twoD[2][0] is

int twoD[4][2]={ 1, 10, 2, 13, 3, 34, 4, 15 }; Picture of array twoD[1][1] is 13 twoD[2][0] is 3

Arrays of strings char strings[6][80]={ "one", "two", {'t','h','r','e','e',‘\0’}, "four", "five", "" }; Picture of array cout<<strings[1][1]<<endl; cout<<strings[2][0]<<endl; cout<<strings[1]<<endl; cout<<strings[2]<<endl;

Arrays of strings char strings[6][80]={ "one", "two", {'t','h','r','e','e', '\0'}, "four", "five", "" }; Picture of array cout<<strings[1][1]<<endl; // w cout<<strings[2][0]<<endl; // t cout<<strings[1]<<endl; //two cout<<strings[2]<<endl; //three

Arrays of strings char strings[6][80]={ "one", "two", {'t','h','r','e','e’,‘\0’}, "four", "five", "" }; int i=0; while(strings[i][0]){ cout<<strings[i]<<endl; i++; }

Most of C++’s power is derived from pointers. They allow different sections of code to share information easily. Pointers enable complex data structures like linked lists. Pointers

Assumptions: characters are one byte in length integers are four bytes long floats are four bytes long doubles are eight bytes long Pointers

A pointer is a variable that holds a memory address. Pointers

int x=5; int* xptr; 5 x of type int0x0012F578 0x0012F690xptr of type int*

int x=5; int* xptr; xptr=&x; //points to x 5 0x0012F578 x of type int0x0012F578 0x0012F690xptr of type int*

int x=5; int* xptr; xptr=&x; //points to x 5 x of type int0x0012F578 xptr of type int*

int x=5; int* xptr; xptr=&x; //points to x 5 x of type intxptr of type int*

int x=5; int* xptr; xptr=&x; //points to x The most common error is forgetting to initialize the pointer 5 x of type intxptr of type int*

There are two special operators that are used with pointers: * and & The & is a unary operator that returns the memory address of its operand. int balance = 350; int *balptr; balptr = &balance; This address is the location of the variable balance in memory, it has nothing to do with the value of balance. Pointer Operators

The second operator * is the complement of &. It is a unary operator that returns the value of variable located at address specified by its operand. int balance = 350; int *balptr; balptr = &balance; int value; value = *balptr; //what does value contain? Pointer Operators

& address of operator * value of pointee (de-referencing operator) Pointer Operators

When a pointer is first allocated it does not point to anything. Trying to de-reference an un-initialized pointer is a serious runtime error. If you are lucky the dereference will crash or halt immediately. If you are unlucky it will corrupt a random area of memory – so that things go wrong after some indefinite time. Pointers