C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void.

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

A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
1 Pointers and Strings Section 5.4, , Lecture 12.
Programming in C Chapter 10 Structures and Unions
SEE C GO Provisional Title. Syntax Types int, float, double, char, void Identifiers foo Operators + - * / ^ Delimiters ; {} () “” ‘’ Keywords return,
Part 1 Landscape Hannu Laine. Pointer parameters //prototype of swap void swap(int *a, int *b); //application void main(void) { in number1 = 1, number2.
1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
String in C++. String Series of characters enclosed in double quotes.“Philadelphia University” String can be array of characters ends with null character.
Pointers in C Rohit Khokher
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Kernighan/Ritchie: Kelley/Pohl:
Informática II Prof. Dr. Gustavo Patiño MJ
Pointers and Memory Allocation -L. Grewe. Objectives Why and What are Pointers Create Pointers in C++ Memory Allocation Defined Memory Allocation/Deallocation.
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
Pointers and Output Parameters. Pointers A pointer contains the address of another memory cell –i.e., it “points to” another variable cost:1024.
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
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.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
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.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers.
February 11, 2005 More Pointers Dynamic Memory Allocation.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
Dynamic memory allocation and Pointers Lecture 4.
Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS];
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 3 – August 28, 2001.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
Weeks 5-6 Pointers and Arrays Basic pointer type Pointers and Arrays Address arithmetic Pointer Arrays User-defined data types Structures Unions Pointers.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
Functions & Pointers in C Jordan Erenrich
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
System Programming Practical Session 7 C++ Memory Handling.
What we will cover A crash course in the basics of C “Teach yourself C in 21 days”
Strings Jarret Raim C Strings Same as arrays of characters. Use pointers. Require static declarations (compile time). No bounds checking. No easy.
Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output.
Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
P OINTERS A pointer is an address All data is stored in memory in some location that is indexed with an address Can refer to variables by name or by memory.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
POINTERS AND MEMORY ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Pointers What is the data type of pointer variables?
Pointers and Dynamic Arrays
Introduction to Programming
Dynamic Memory Allocation
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Dynamic Memory A whole heap of fun….
C++ Pointers and Strings
Dynamic Memory.
C Programming Lecture-8 Pointers and Memory Management
The Stack.
C++ Pointers and Strings
EECE.2160 ECE Application Programming
Presentation transcript:

C++ crash course Class 10 practice problems

Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void swap(int &a, int b) { int tmp = a; a = b; b = tmp; }

Strings Write a function which, given a char array and two ints, will return a string * which holds the section between the two ints. Return the null pointer if its not possible. string *substr(char arr[], int start, int stop);

Dynamic Memory Allocation The below is bad practice. Why is that? int *p = new int(5); int j = 7; p = &j;

Pointers This wont compile. Why? double a = 10; int *aptr = &a;

Pointers What is this, and what is its use?

Data Types How is initializing a vector of ints different from initializing a single int?

Data Types Write the following in decimal x21

Files and Unix Whats the difference between > and < on the Unix command line?

Whats the output? int size = 3; int arr[12] = {7, 2, 4}; char name[12] = Jeff Jones; int *iptr; char *cptr; iptr = arr; cout << iptr[1] << and << *iptr << endl; cptr = &name[5]; cout << cptr << endl; cout << *(cptr + 3) << and << *(cptr-4) << endl; cptr ++; cout << name[3] << and << cptr[3] << endl;

Operators What do all of the following do? *itr++; *++itr; (*itr)++;

What does this output? void funct(int a); int main() { int a = 5, *b = &a, c[] = {10, 21, 12}; int *d, *e, f; cout << *b << << *c + 1 << endl; e = b; b = new int; d = new int; *d = 7; *b = 9; *e = 11; cout << *e << *d << a; funct(a); e = b; free(b); cout << *e << f << a; return 0; }

Scope Whats the scope operator (::) doing in these instances? std::cout int Animal_type::avg_age() const What can you do to avoid using the scope operator repeatedly?

Overloading Operators Whats the function prototype to overload the output operator? – return type – function name – parameter list

const What is the purpose of the const keyword? const gets a little tricky with pointers. Give an example.

Loops Write a function to sum even numbers from int a to int b, and return the result.

Reserved Words What are reserved words? Give 3 examples.

Functions Whats the difference between int a, int &a, and int *a in a function parameter list?

? What are these called? What do they do? #include #define #ifndef #endif

Operators What is precedence? What is associativity? Is assignment left or right associative? Is arithmetic left or right associative?

Sorting Sort these using insertion sort, and show all the steps

Sorting Write an insertion sort on a vector of int *.

Classes Whats a constructor? How do you identify one? Write a default constructor for this class: class Mystery { int a; bool b; string s; };

Classes What is the difference between public, private and friend?

Data Types Whats the difference between a literal and a constant?