6. Pointers, Structs, and Arrays. 1. Juli 2011

Size: px
Start display at page:

Download "6. Pointers, Structs, and Arrays. 1. Juli 2011"

Transcription

1 1. Juli 2011 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 50

2 Outline Recapitulation Pointers Dynamic Memory Allocation Structs Arrays Bubble Sort Strings Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 2 of 50

3 Recapitulation What is an enum? How is it encoded? What is a for loop? What is a declaration and what is a definition? What is a namespace for? What is a header file and how do we use it? What is an enum? What happens at the end of a scope? What is the difference between call-by-value and call-by-reference? Why is there a range for all primitive variables in C? Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 3 of 50

4 6.1. Pointers Memory 21: 22; 20 // int a 23; 24; 25; 26; 27; 28: We have talked a lot about the mapping from variables to addresses. However, we have never analysed the real addresses. The memory is enumerated using an integer. Pointers are variables that hold the memory number (address) instead of a value. / / holds a f l o a t i n g p o i n t / / number double a ; i n t a ; / / holds address of a f l o a t i n g / / p o i n t number i n t b ; Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 4 of 50

5 We Have Already Used Pointers Before : C/C++ only supports call-by-value. Call-by-reference is not a language concept. void foo ( i n t & a ) { / / a c t u a l l y, C/C++ does not support t h i s... i n t a = 20; foo ( a ) ; void foo ( i n t a ) { / / address now i s copied ( c a l l by value ) / / work on address of a, not on a d i r e c t l y... i n t a = 20; foo ( / address of a / ) ; Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 5 of 50

6 Pointers And Addresses The * operator declares a pointer. i n t a ; i n t p ; The & operator returns the address of a variable (address operator or reference operator). p = &a ; The * operator makes an operation act on the location an pointers points to instead of the pointer itself (dereferencing operator. a += 10; ( p ) += 10; Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 6 of 50

7 Pointers and the Memory Memory 21: 22; 20 // int a 23; 24; 22 // pointer p 25; 26; 27; 28: int a; a = 20; int *p; p = &a; a++; p++; (*p)++; *p++; // try this at home Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 7 of 50

8 Exercise void foo ( i n t & a ) { a+=1; i n t main ( ) { i n t a = 20; foo ( a ) ; std : : cout << a ;... Rewrite exactly this code without a return statement and without the reference operator. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 8 of 50

9 Solution void foo ( i n t a ) { ( a )+=1; / / i n d i r e c t memory access i n t main ( ) { i n t a = 20; / / d i r e c t memory access foo (&a ) ; std : : cout << a ;... There s three modifications. Analyse the call-by-value for the pointer. Which solution is the better/nicer solution? Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 9 of 50

10 When to Use Pointers void foo ( i n t a ) { ( a )++;... a++; / / That could not have happened with references Pointers are difficult to handle (lots of syntactic overhead). We know how to do it, but do collaborators know? Avoid it whenever possible. Use C++ references instead. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 10 of 50

11 Pointer Syntax The pointer operator binds to the right neighbour. i n t a ; i n t b1 ; i n t b2 ; i n t b3 ; i n t c1, c2 ; i n t c3, c4 ; i n t c5, c6 ; i n t c7, c8 ; i n t c9 ; c7 = c8 ; c7 = c8 ; c7 = c8 ; c7 = c8 ; By the way, often people write int* p=0; to make the pointer point to nothing. However, also int* p = 20; would be fine (and for almost 100 percent is a bug). Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 11 of 50

12 Pointers and Scopes i n t p = &a ; f o r ( i n t i =0; i <2; i ++) { i n t a = 2 i ; p = &a ; std : : cout << a << std : : endl ; std : : cout << p << std : : endl std : : cout << a << std : : endl ; / / does t h i s work? std : : cout << p << std : : endl ; / / does t h i s work? Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 12 of 50

13 Pointers and Scopes i n t p = &a ; f o r ( i n t i =0; i <2; i ++) { i n t a = 2 i ; p = &a ; std : : cout << a << std : : endl ; std : : cout << p << std : : endl std : : cout << a << std : : endl ; / / does t h i s work? std : : cout << p << std : : endl ; / / does t h i s work? With pointers, we can violate the end-of-scope rules, i.e. we can access variables that do not exist anymore. This is the principle of all these buffer overrun malware. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 12 of 50

14 6.2. Dynamic Memory Allocation Switching Off the Lifecycle Management double p ; p = new double ; / / now, p p o i n t s to an e x i s t i n g new v a r i a b l e p = 20; delete p ; / / now v a r i a b l e i s freed, but p s t i l l p o i n t s / / to t h i s v a r i a b l e. p = 30; Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 13 of 50

15 Two Memory Leaks double p ; f o r ( i n t i =0; i <20; i ++) { p = new double ; delete p ; f o r ( i n t i =0; i <20; i ++) { double p = new double ; Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 14 of 50

16 Two Memory Leaks double p ; f o r ( i n t i =0; i <20; i ++) { p = new double ; delete p ; f o r ( i n t i =0; i <20; i ++) { double p = new double ; The upper operation creates 20 doubles on the heap, but it destroys only one double in the end. Consequently, the remaining 19 doubles are lost for the future program execution. Let s sketch the memory layout! The second one destroys the pointer but not the space where the pointer is pointing to. This is why many applications crash after several hours of execution. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 14 of 50

17 6.3. Structs n-tuples / This represents one student i n our o n l i n e system / s t r u c t Student { i n t number ; double averagegrade ; ; Structs allow us to create user-defined data structures (n-tuples). Once declared, we can use them like built-in types. C/C++ takes care of the memory layout. This is particularly important for arrays of structs. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 15 of 50

18 Declaration and Definition / / T e l l s compiler t h a t something / / l i k e a StudentID does e x i s t s t r u c t StudentID ; / This represents one student i n our o n l i n e system / s t r u c t Student { i n t number ; double averagegrade ; StudentID id ; ; For pointers (and references), it is sufficient to tell the compiler that the type does exist (declaration). The declaration is needed for recursive definitions of structs. The declaration is needed for more the composition of structs. We ll first analyse struct composition. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 16 of 50

19 Struct Composition (Attributes) / / T e l l s compiler t h a t something / / l i k e a StudentID does e x i s t s t r u c t StudentID { i n t photonumber ; bool iswinterterm ; ; / This represents one student i n our o n l i n e system / s t r u c t Student { i n t number ; double averagegrade ; StudentID id ; / / i t isn t a pointer anymore ; Take a sheet of paper and sketch the memory layout for an instance of Student if the id is a pointer and if it is not a pointer. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 17 of 50

20 Memory Memory 21: 22; 40 // photono 23; true // winter term 24; 25; 26; 1234 // number 27; 3.4 // average grade 28: 22 // pointer 24; 25; 26; 1234 // number 27; 3.4 // average grade 28; 40 // photono 29; true // winter term 30: 31: Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 18 of 50

21 Structs Referencing Structs s t r u c t I n t e g e r E n t r y { i n t value ; I n t e g e r E n t r y next ; ; Take a sheet of paper and sketch the memory layout for an instance of IntegerEntry referencing another instance of IntegerEntry. What could be the reasoning behind such a data structure? Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 19 of 50

22 Memory 21: 22; 40 // value 23; 26 // next 24; 25; 26; 23 // value 27; 0 // next 28: Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 20 of 50

23 Accessing the Elements of a Struct / / T e l l s compiler t h a t something l i k e a StudentID does e x i s t s t r u c t I n t e g e r E n t r y { i n t value ; I n t e g e r E n t r y next ; ;... IntegerEntry myentry ; myentry. value = 20; myentry. next = 0; IntegerEntry pentry = new IntegerEntry ; pentry >value = 20; pentry >next = 0; delete pentry ; The dot gives access to sub-fields. The -> operator gives access to sub-fields of pointers. Structs can be used with new as well. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 21 of 50

24 The Single Linked List / / T e l l s compiler t h a t something l i k e a StudentID does e x i s t s t r u c t I n t e g e r E n t r y { i n t value ; I n t e g e r E n t r y next ; ; void p r i n t L i s t ( I n t e g e r E n t r y f i r s t E n t r y ) { while ( f i r s t E n t r y! = 0 ) { std : : cout << f i r s t E n t r y >value << ; f i r s t E n t r y = f i r s t E n t r y >next ; void append ( I n t e g e r E n t r y f i r s t E n t r y, i n t value ) { while ( f i r s t E n t r y >next! = 0 ) { f i r s t E n t r y = f i r s t E n t r y >next ; IntegerEntry newentry = new IntegerEntry ; f i r s t E n t r y >next = newentry ; newentry >next = 0; newentry >value = value ; Can you rewrite append() recursively? Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 22 of 50

25 Remove Element Operation Play around with the list and create a list [2, 4, 6, 8]. Write a remove operation that accepts an integer k and removes the kth entry from the list. Invoke it with remove(2). Print the result to the terminal. It should return [2, 4, 8]. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 23 of 50

26 An Excursus on Lists and Complexity the big-o notation f O(n k ) Doesn t grow faster than n k. Faster or slower refers to the leading term of the runtime polynomial. Constants or polynomials of lower order are not considered. Often, one writes = or says is of instead of is contained in. What does this mean for our list? What is the n there? Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 24 of 50

27 An Excursus on Lists and Complexity the big-o notation f O(n k ) Doesn t grow faster than n k. Faster or slower refers to the leading term of the runtime polynomial. Constants or polynomials of lower order are not considered. Often, one writes = or says is of instead of is contained in. What does this mean for our list? What is the n there? Study the list in terms of n list entries. If we remove the first element, this is a fixed number of operations (O(1)). If we append an element, our while loop has to run over all n element before we can append an element (O(n)). If we search for an element, we have to study each element exactly once (O(n)). What could we do to make the append operation run faster? Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 24 of 50

28 6.4. Arrays Application example: At the university, we wanna keep track of four grades a student did throughout his Master s studies. double analysis1 ; double analysis2 ; double linearalgebra ; double stochastics ;... / Takes grades, computes the average, and returns t h i s value. / double computeaverage ( const double& g1, const double& g2,... ) { double r e s u l t = g1+g2+g3+g4 ; r e s u l t /= 4. 0 ; r e t u r n r e s u l t ; Is this impractical, if we wanna store more than four values. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 25 of 50

29 Array Declaration Memory 21: 22; // grade[0] 23; // grade[1] 24; // grade[2] 25; // grade[3] 26; 27; 28: 22 // grade (pointer) double grade [ 4 ] ;... Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 26 of 50

30 Array Access Memory double grade [ 4 ] ;... 21: 22; // grade[0] 23; // grade[1] 24; // grade[2] 25; // grade[3] 26; 27; 28: 22 // grade (pointer) grade [ 0 ] = 1. 3 ; grade [ 2 ] = 3. 3 ; grade [ 3 ] = 1. 0 ; grade [ 1 ] = 4. 0 ; Depending on the context, [] either defines the size of an array (definition) or gives access to individual entries of an array (access). Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 27 of 50

31 Arrays & Pointers double grade [ 4 ] ; grade [ 0 ] = 1. 3 ; grade [ 2 ] = 3. 3 ; grade [ 3 ] = 1. 0 ; grade [ 1 ] = 4. 0 ; double p = grade ; i f ( grade [0]== p )... / / always t r u e i f ( grade [1]== ( p +1))... / / always t r u e An array variable is basically a pointer to the first element of the array. An array element access internally implies pointer arithmetics and dereferencing. Again, we thus have to range checks (size of array) at hand at all. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 28 of 50

32 Grades Revisited We need a range variable / Takes grades, computes the average, and returns t h i s value. / double computeaverage ( double grade, i n t numberofgrades ) { double r e s u l t = 0. 0 ; f o r ( i n t i =0; i<numberofgrades ; i ++ ) {... double scale = numberofgrades ; r e s u l t /= scale ; r e t u r n r e s u l t ;... double grade [ 4 ] ; Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 29 of 50

33 Array Arguments double computeaverage ( double grade [ ], i n t numberofgrades ) {... double computeaverage ( const double grade [ ], i n t numberofgrades ) {... This time, the user is not allowed to modify any entry of grade. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 30 of 50

34 Pitfalls i n t gradesbsc, gradesmsc [ 5 ] ; gradesmsc [ 5 ] = 2. 3 ; gradesmsc [ 3 ] + + ; i n t p0 = gradesmsc ; i n t p1 = &gradesmsc [ 0 ] ; i n t p2 = &gradesmsc [ 1 ] ; gradesmsc++; / / t h a t does not work p2++; / / a r r r g h Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 31 of 50

35 Dynamic Arrays double grades = new double [ 4 5 ] ; delete [ ] grades ; We can create arrays on the heap. Size might be a variable, too. Corresponding delete has to be a delete[]. delete without brackets just deletes the first value. If we omit delete, we will get a memory leak. If we use the array after delete or before new, it points to garbage (remember: grades is only a pointer). Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 32 of 50

36 Array As Return Functions double createthreerandomgrades ( ) { double r e s u l t [ 3 ] ; r e s u l t [ 0 ] = 1. 3 ; r e s u l t [ 1 ] = 2. 7 ; r e s u l t [ 2 ] = 1. 0 ; r e t u r n r e s u l t ; double createthreerandomgrades ( ) { double r e s u l t = new double [ 3 ] ; r e s u l t [ 0 ] = 1. 3 ; r e s u l t [ 1 ] = 2. 7 ; r e s u l t [ 2 ] = 1. 0 ; r e t u r n r e s u l t ; At the end of the scope, the pointer is destroyed always. Arrays on the heap are not destroyed. This is called a factory mechanism. Someone else invoking the function has to delete the array. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 33 of 50

37 Multidimensional Arrays double matrix [ 4 ] [ 4 ] ; f o r ( i n t i =0; i <4; i ++) { f o r ( i n t j =0; j <4; j ++) { matrix [ i ] [ j ] = i == j? 1.0 : 0. 0 ; matrix [ 2 ] [ 1 ] = 1. 0 ; matrix [ 1 2 ] = 1. 0 ; What is the semantics of the for loop? Multidimensional arrays basically are flat arrays. C/C++ uses row-major format (different to FORTRAN). Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 34 of 50

38 Arrays of Structs s t r u c t Person { i n t age ; double weight ; ; Person couple [ 2 ] ; Person p = couple ; p++; C/C++ takes care of the memory padding. It stores the entries in the memory in the following order: couple[0].age, couple[0].weight, couple[1].age, couple[1].weight. The increment sets the pointer to the subsequent age address. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 35 of 50

39 Outlook C/C++ Arrays vs. FORTRAN FORTRAN is claimed to be the language for linear algebra as it is faster. FORTRAN does not provide pointers and dynamic data structures. Consequently, compiler can keep track of who has access where. Consequently, compiler can optimise aggressively (it tries to keep book of all possible values an array could have side-effect!). So, it is all a matter of exclusivity and the const operator. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 36 of 50

40 6.5. Sorting How can we sort an array with n entries? or: Yes We Can (watch a movie) ntqc8 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 37 of 50

41 Bubble Sort void s o r t ( i n t e n t r i e s [ ], const i n t & numberofentries ) { bool sorted = true ; while ( sorted ) { sorted = f a l s e ; f o r ( i n t i =0; i<numberofentries 1; i ++ ) { i f ( e n t r i e s [ i ]> e n t r i e s [ i + 1 ] ) {... sorted = true ; Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 38 of 50

42 Complexity of Bubble Sort Idea of bubble sort: Run over all elements in the list. Compare two subsequent elements whether they are in the correct order. Swap them if necessary. If a swap still had been necessary, run over list again. How expensive is the sorting? Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 39 of 50

43 Complexity of Bubble Sort Idea of bubble sort: Run over all elements in the list. Compare two subsequent elements whether they are in the correct order. Swap them if necessary. If a swap still had been necessary, run over list again. How expensive is the sorting? The number of comparisons is a good metric. So, let n be the number of elements in our list. At most (worst case), we ll need n runs over the list. In the average case, we ll need n/2 runs over the list. In each run, we have to do n 1 comparisons. Overall, the complexity is O(n 2 ). More intelligent (but more complex) sorting algorithms need only O(n log n) comparisons. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 39 of 50

44 6.6. Strings C has no string concept. C++ has a nice string concept. C has chars. And C has arrays. Thus, C has arrays of chars. And C has a mapping of numbers to chars. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 40 of 50

45 Codes The ASCII Code Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 41 of 50

46 Termination Codes for Arrays Memory 21: 22; 24 23; 12 24; 71 25; 71 26; 14 27; 0 28: 29: 30: 22 // s Lets have the following mapping: 0 Reserved (termination) 12 a 14 o 24 H 71 l Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 42 of 50

47 Strings as Arrays char s [ 6 ] ; s [ 0 ] = H ; s [ 1 ] = a ; s [ 2 ] = l ; s [ 3 ] = l ; s [ 4 ] = o ; s [ 5 ] = 0 ; Lets have the following mapping: 0 Reserved (termination) 12 a 14 o 24 H 71 l Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 43 of 50

48 Strings are Arrays char s [ 6 ] ; s [ 0 ] = H ; s [ 1 ] = a ; s [ 2 ] = l ; s [ 3 ] = l ; s [ 4 ] = o ; s [ 5 ] = 0 ; char mystring = Hallo ; We don t want to pass an integer with each string. Thus, C appends a 0 as last character. Thus, the array has one element more than the string has chars. Pascal follows a different variant. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 44 of 50

49 Operations on Strings Memory 21: 22; 24 23; 12 24; 71 25; 71 26; 14 27; 0 28: 29: 30: 22 // s Write a function length that takes a string and counts the number of entries. This number then is returned. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 45 of 50

50 Length Operation i n t length ( const char [ ] s ) { i n t length = 0; while ( s [ length ]==0) { length ++; return length ; char mystring = Hallo ; i n t lengthofmystring = length ( mystring ) ; What is copied here (call-by-value)! What happens, if we write char a n o t h e r S t r i n g = mystring ; can we then call length for both strings? Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 46 of 50

51 Copying Strings char mystring = This i s a t e s t ; char mycopy = mystring ; delete [ ] mystring ; length ( mystring ) What happens if we execute this code? Write an operation that copies a string. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 47 of 50

52 6.7. Trees Remember our single linked list (for double values). Imagine we wanna hold this list sorted all the time. What would an insert operation look like? What is the (average) runtime of insert in terms of elements? How could we reduce this runime to O(logn)? Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 48 of 50

53 A list with three entries s t r u c t L i s t E n t r y { double value ; L i s t E n t r y s m a l l e r E n t r y ; L i s t E n t r y biggerentry ; ; Code the data structure from above, and create a list with three values {0.4, 1.2, 22 (in this order) as follows: Create list for 0.4 (with two null pointers). Create a list element for 1.2 and append it to 0.4. The append function checks whether it is bigger or equal to its current value. It is not, so it is appended to smallerentry. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 49 of 50

54 Trees The example we implemented is a binary tree, as each node has up to two children. The average depth of this tree is O(logn). If we search for an element (as we have to do if we insert), the search consequently also is in O(logn). There s hundreds of variants for trees with different properties. Tree algorithms and recursive code go hand in hand. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 50 of 50

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

6. Pointers, Structs, and Arrays. March 14 & 15, 2011 March 14 & 15, 2011 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 47 Outline Recapitulation Pointers Dynamic Memory Allocation Structs Arrays Bubble Sort Strings Einführung

More information

4. Functions. March 10, 2010

4. Functions. March 10, 2010 March 10, 2010 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 40 Outline Recapitulation Functions Part 1 What is a Procedure? Call-by-value and Call-by-reference Functions

More information

5. Applicative Programming. 1. Juli 2011

5. Applicative Programming. 1. Juli 2011 1. Juli 2011 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 41 Outline Recapitulation Computer architecture extended: Registers and caches Header files Global variables

More information

10. Object-oriented Programming. 7. Juli 2011

10. Object-oriented Programming. 7. Juli 2011 7. Juli 2011 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 47 Outline Object Case Study Brain Teaser Copy Constructor & Operators Object-oriented Programming, i.e.

More information

Object-oriented Programming in C++

Object-oriented Programming in C++ Object-oriented Programming in C++ Working with C and C++ Wolfgang Eckhardt, Tobias Neckel March 23, 2015 Working with C and C++, March 23, 2015 1 Challenges of This Course Heterogeneity of the audience

More information

Object-oriented Programming in C++

Object-oriented Programming in C++ Object-oriented Programming in C++ Working with C and C++ Wolfgang Eckhardt, Tobias Neckel June 30, 2014 Working with C and C++, June 30, 2014 1 Challenges of This Course Heterogeneity of the audience

More information

8. Object-oriented Programming. June 15, 2010

8. Object-oriented Programming. June 15, 2010 June 15, 2010 Introduction to C/C++, Tobias Weinzierl page 1 of 41 Outline Recapitulation Copy Constructor & Operators Object-oriented Programming Dynamic and Static Polymorphism The Keyword Static The

More information

Pointers and Dynamic Memory Allocation

Pointers and Dynamic Memory Allocation Pointers and Dynamic Memory Allocation ALGORITHMS & DATA STRUCTURES 9 TH SEPTEMBER 2014 Last week Introduction This is not a course about programming: It s is about puzzling. well.. Donald Knuth Science

More information

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics PIC 10A Pointers, Arrays, and Dynamic Memory Allocation Ernest Ryu UCLA Mathematics Pointers A variable is stored somewhere in memory. The address-of operator & returns the memory address of the variable.

More information

Exercise 1.1 Hello world

Exercise 1.1 Hello world Exercise 1.1 Hello world The goal of this exercise is to verify that computer and compiler setup are functioning correctly. To verify that your setup runs fine, compile and run the hello world example

More information

Object-Oriented Programming for Scientific Computing

Object-Oriented Programming for Scientific Computing Object-Oriented Programming for Scientific Computing Dynamic Memory Management Ole Klein Interdisciplinary Center for Scientific Computing Heidelberg University ole.klein@iwr.uni-heidelberg.de 2. Mai 2017

More information

CS201 Some Important Definitions

CS201 Some Important Definitions CS201 Some Important Definitions For Viva Preparation 1. What is a program? A program is a precise sequence of steps to solve a particular problem. 2. What is a class? We write a C++ program using data

More information

a data type is Types

a data type is Types Pointers Class 2 a data type is Types Types a data type is a set of values a set of operations defined on those values in C++ (and most languages) there are two flavors of types primitive or fundamental

More information

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26 COSC 2P95 Procedural Abstraction Week 3 Brock University Brock University (Week 3) Procedural Abstraction 1 / 26 Procedural Abstraction We ve already discussed how to arrange complex sets of actions (e.g.

More information

11. Generic Programming and Design Patterns. 8. Juli 2011

11. Generic Programming and Design Patterns. 8. Juli 2011 8. Juli 2011 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 26 Outline Recapitulation Template Programming An Overview over the STL Design Patterns (skipped) Evaluation/feedback

More information

Principles of Programming Pointers, Dynamic Memory Allocation, Character Arrays, and Buffer Overruns

Principles of Programming Pointers, Dynamic Memory Allocation, Character Arrays, and Buffer Overruns Pointers, Dynamic Memory Allocation, Character Arrays, and Buffer Overruns What is an array? Pointers Memory issues The name of the array is actually a memory address. You can prove this by trying to print

More information

Pointers, Dynamic Data, and Reference Types

Pointers, Dynamic Data, and Reference Types Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation The new operator The delete operator Dynamic Memory Allocation for Arrays 1 C++ Data Types simple

More information

Review of Important Topics in CS1600. Functions Arrays C-strings

Review of Important Topics in CS1600. Functions Arrays C-strings Review of Important Topics in CS1600 Functions Arrays C-strings Array Basics Arrays An array is used to process a collection of data of the same type Examples: A list of names A list of temperatures Why

More information

Consider the above code. This code compiles and runs, but has an error. Can you tell what the error is?

Consider the above code. This code compiles and runs, but has an error. Can you tell what the error is? Discussion 1H Notes (Week 8, May 20) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs31 Dynamic Allocation of Memory Recall that when you create an array, you must know

More information

Introduction to Computer Science Midterm 3 Fall, Points

Introduction to Computer Science Midterm 3 Fall, Points Introduction to Computer Science Fall, 2001 100 Points Notes 1. Tear off this sheet and use it to keep your answers covered at all times. 2. Turn the exam over and write your name next to the staple. Do

More information

CS 11 C track: lecture 5

CS 11 C track: lecture 5 CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap Pointers (from last week) Address: location where data stored

More information

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book. Pointers and Arrays CS 201 This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book. Pointers Powerful but difficult to master Used to simulate pass-by-reference

More information

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings 19/10/2017 CE221 Part 2 1 Variables and References 1 In Java a variable of primitive type is associated with a memory location

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31. Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172 The lab will be on pointers In compensation, no class on Friday, Jan. 31. 1 Consider the bubble function one more

More information

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2 Discussion 1E Jie(Jay) Wang Week 10 Dec.2 Outline Dynamic memory allocation Class Final Review Dynamic Allocation of Memory Recall int len = 100; double arr[len]; // error! What if I need to compute the

More information

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic Review from Letctures 3 & 4 C++ class syntax, designing classes, classes vs. structs; Passing comparison functions to

More information

EE 355 Lab 4 - Party Like A Char Star

EE 355 Lab 4 - Party Like A Char Star 1 Introduction In this lab you will implement a "hangman" game where the user is shown blanks representing letter of a word and then tries to guess and fill in the letters with a limited number of guesses

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

Memory and C++ Pointers

Memory and C++ Pointers Memory and C++ Pointers C++ objects and memory C++ primitive types and memory Note: primitive types = int, long, float, double, char, January 2010 Greg Mori 2 // Java code // in function, f int arr[];

More information

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures The main body and cout Agenda 1 Fundamental data types Declarations and definitions Control structures References, pass-by-value vs pass-by-references The main body and cout 2 C++ IS AN OO EXTENSION OF

More information

Pointers II. Class 31

Pointers II. Class 31 Pointers II Class 31 Compile Time all of the variables we have seen so far have been declared at compile time they are written into the program code you can see by looking at the program how many variables

More information

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012 MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012 Instructor: K. S. Booth Time: 70 minutes (one hour ten minutes)

More information

CS2141 Software Development using C/C++ C++ Basics

CS2141 Software Development using C/C++ C++ Basics CS2141 Software Development using C/C++ C++ Basics Integers Basic Types Can be short, long, or just plain int C++ does not define the size of them other than short

More information

Variables, Memory and Pointers

Variables, Memory and Pointers Variables, Memory and Pointers A variable is a named piece of memory The name stands in for the memory address int num; Variables, Memory and Pointers When a value is assigned to a variable, it is stored

More information

Fast Introduction to Object Oriented Programming and C++

Fast Introduction to Object Oriented Programming and C++ Fast Introduction to Object Oriented Programming and C++ Daniel G. Aliaga Note: a compilation of slides from Jacques de Wet, Ohio State University, Chad Willwerth, and Daniel Aliaga. Outline Programming

More information

Object Reference and Memory Allocation. Questions:

Object Reference and Memory Allocation. Questions: Object Reference and Memory Allocation Questions: 1 1. What is the difference between the following declarations? const T* p; T* const p = new T(..constructor args..); 2 2. Is the following C++ syntax

More information

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff and Arrays Comp Sci 1570 Introduction to C++ Outline and 1 2 Multi-dimensional and 3 4 5 Outline and 1 2 Multi-dimensional and 3 4 5 Array declaration and An array is a series of elements of the same type

More information

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive) Final Exam Exercises Chapters 1-7 + 11 Write C++ code to: l Determine if a number is odd or even CS 2308 Fall 2016 Jill Seaman l Determine if a number/character is in a range - 1 to 10 (inclusive) - between

More information

Memory and Pointers written by Cathy Saxton

Memory and Pointers written by Cathy Saxton Memory and Pointers written by Cathy Saxton Basic Memory Layout When a program is running, there are three main chunks of memory that it is using: A program code area where the program itself is loaded.

More information

CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic

CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic Announcements: Test 1 Information Test 1 will be held Monday, February 10th, 2014 from 6-7:50pm, Lab sections 1-5 and

More information

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites: C Programming Code: MBD101 Duration: 10 Hours Prerequisites: You are a computer science Professional/ graduate student You can execute Linux/UNIX commands You know how to use a text-editing tool You should

More information

Arrays array array length fixed array fixed length array fixed size array Array elements and subscripting

Arrays array array length fixed array fixed length array fixed size array Array elements and subscripting Arrays Fortunately, structs are not the only aggregate data type in C++. An array is an aggregate data type that lets us access many variables of the same type through a single identifier. Consider the

More information

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types Chapter 6 Topics WEEK E FOUR Data Types Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types Associative Arrays Record Types Union Types Pointer and Reference

More information

Run Time Environment

Run Time Environment CS 403 Compiler Construction Lecture 12 Run Time Environment and Management [Based on Chapter 7 of Aho2] 1 Run Time Environment From Lecture 1 to 11, we have seen many jobs that are done by a compiler.

More information

11. Arrays. For example, an array containing 5 integer values of type int called foo could be represented as:

11. Arrays. For example, an array containing 5 integer values of type int called foo could be represented as: 11. Arrays An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier. That means that, for example,

More information

CA31-1K DIS. Pointers. TA: You Lu

CA31-1K DIS. Pointers. TA: You Lu CA31-1K DIS Pointers TA: You Lu Pointers Recall that while we think of variables by their names like: int numbers; Computer likes to think of variables by their memory address: 0012FED4 A pointer is a

More information

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion Review from Lectures 5 & 6 Arrays and pointers, Pointer arithmetic and dereferencing, Types of memory ( automatic, static,

More information

C++ for Java Programmers

C++ for Java Programmers Basics all Finished! Everything we have covered so far: Lecture 5 Operators Variables Arrays Null Terminated Strings Structs Functions 1 2 45 mins of pure fun Introduction Today: Pointers Pointers Even

More information

Quiz 0 Review Session. October 13th, 2014

Quiz 0 Review Session. October 13th, 2014 Quiz 0 Review Session October 13th, 2014 Topics (non-exhaustive) Binary. ASCII. Algorithms. Pseudocode. Source code. Compiler. Object code. Scratch. Statements. Boolean expressions. Conditions. Loops.

More information

CISC220 Lab 2: Due Wed, Sep 26 at Midnight (110 pts)

CISC220 Lab 2: Due Wed, Sep 26 at Midnight (110 pts) CISC220 Lab 2: Due Wed, Sep 26 at Midnight (110 pts) For this lab you may work with a partner, or you may choose to work alone. If you choose to work with a partner, you are still responsible for the lab

More information

Arrays in C++ Instructor: Andy Abreu

Arrays in C++ Instructor: Andy Abreu Arrays in C++ Instructor: Andy Abreu Reason behind the idea When we are programming, often we have to process a large amount of information. We can do so by creating a lot of variables to keep track of

More information

2. Variables, Identifiers, and Expressions. March 8, 2011

2. Variables, Identifiers, and Expressions. March 8, 2011 March 8, 2011 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 34 Outline A Movie Structure of Simple C Codes Comments & Documentation Variables & Identifiers Built-in

More information

Pointers in C/C++ 1 Memory Addresses 2

Pointers in C/C++ 1 Memory Addresses 2 Pointers in C/C++ Contents 1 Memory Addresses 2 2 Pointers and Indirection 3 2.1 The & and * Operators.............................................. 4 2.2 A Comment on Types - Muy Importante!...................................

More information

Lab#5 Due Wednesday, February 25, at the start of class. Purpose: To develop familiarity with C++ pointer variables

Lab#5 Due Wednesday, February 25, at the start of class. Purpose: To develop familiarity with C++ pointer variables Lab#5 Due Wednesday, February 25, at the start of class Purpose: To develop familiarity with C++ pointer variables Introduction: In this lab, you will learn by experimentation the answers to some questions

More information

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am Exam 1 on July 18, 2005 10:00-11:40am 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. This is the

More information

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/ CS61C Machine Structures Lecture 4 C Pointers and Arrays 1/25/2006 John Wawrzynek (www.cs.berkeley.edu/~johnw) www-inst.eecs.berkeley.edu/~cs61c/ CS 61C L04 C Pointers (1) Common C Error There is a difference

More information

8. The C++ language, 1. Programming and Algorithms II Degree in Bioinformatics Fall 2017

8. The C++ language, 1. Programming and Algorithms II Degree in Bioinformatics Fall 2017 8. The C++ language, 1 Programming and Algorithms II Degree in Bioinformatics Fall 2017 Hello world #include using namespace std; int main() { } cout

More information

CS 231 Data Structures and Algorithms, Fall 2016

CS 231 Data Structures and Algorithms, Fall 2016 CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard

More information

Lecture Notes on Memory Layout

Lecture Notes on Memory Layout Lecture Notes on Memory Layout 15-122: Principles of Imperative Computation Frank Pfenning André Platzer Lecture 11 1 Introduction In order to understand how programs work, we can consider the functions,

More information

CS 103 Unit 11. Linked Lists. Mark Redekopp

CS 103 Unit 11. Linked Lists. Mark Redekopp 1 CS 103 Unit 11 Linked Lists Mark Redekopp 2 NULL Pointer Just like there was a null character in ASCII = '\0' whose ue was 0 There is a NULL pointer whose ue is 0 NULL is "keyword" you can use in C/C++

More information

For example, let s say we define an array of char of size six:

For example, let s say we define an array of char of size six: Arrays in C++ An array is a consecutive group of memory locations that all have the same name and the same type. To refer to a particular location, we specify the name and then the positive index into

More information

CSCI-1200 Data Structures Fall 2012 Lecture 5 Pointers, Arrays, Pointer Arithmetic

CSCI-1200 Data Structures Fall 2012 Lecture 5 Pointers, Arrays, Pointer Arithmetic CSCI-1200 Data Structures Fall 2012 Lecture 5 Pointers, Arrays, Pointer Arithmetic Announcements: Test 1 Information Test 1 will be held Tuesday, September 18th, 2012 from 2-3:50pm in West Hall Auditorium.

More information

CPSC 3740 Programming Languages University of Lethbridge. Data Types

CPSC 3740 Programming Languages University of Lethbridge. Data Types Data Types A data type defines a collection of data values and a set of predefined operations on those values Some languages allow user to define additional types Useful for error detection through type

More information

Object Oriented Software Design II

Object Oriented Software Design II Object Oriented Software Design II Introduction to C++ Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 20, 2012 G. Lipari (Scuola Superiore Sant Anna) C++ Intro February

More information

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions? Lecture 14 No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions? Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 1 Outline Static

More information

Understand Execution of a Program

Understand Execution of a Program Understand Execution of a Program Prof. Zhang September 17, 2014 1 Program in Memory When you execute a program, the program (i.e., executable code) is loaded into the memory (i.e., main memory, RAM) in

More information

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University CS558 Programming Languages Winter 2018 Lecture 4a Andrew Tolmach Portland State University 1994-2018 Pragmatics of Large Values Real machines are very efficient at handling word-size chunks of data (e.g.

More information

22c:111 Programming Language Concepts. Fall Types I

22c:111 Programming Language Concepts. Fall Types I 22c:111 Programming Language Concepts Fall 2008 Types I Copyright 2007-08, The McGraw-Hill Company and Cesare Tinelli. These notes were originally developed by Allen Tucker, Robert Noonan and modified

More information

Vector and Free Store (Vectors and Arrays)

Vector and Free Store (Vectors and Arrays) DM560 Introduction to Programming in C++ Vector and Free Store (Vectors and Arrays) Marco Chiarandini Department of Mathematics & Computer Science University of Southern Denmark [Based on slides by Bjarne

More information

CSCI-1200 Data Structures Fall 2018 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

CSCI-1200 Data Structures Fall 2018 Lecture 5 Pointers, Arrays, & Pointer Arithmetic CSCI-1200 Data Structures Fall 2018 Lecture 5 Pointers, Arrays, & Pointer Arithmetic Announcements: Test 1 Information Test 1 will be held Thursday, Sept 20th, 2018 from 6-7:50pm Students will be randomly

More information

pointers + memory double x; string a; int x; main overhead int y; main overhead

pointers + memory double x; string a; int x; main overhead int y; main overhead pointers + memory computer have memory to store data. every program gets a piece of it to use as we create and use more variables, more space is allocated to a program memory int x; double x; string a;

More information

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency Introduction Fundamentals Declaring arrays Indexing arrays Initializing arrays Arrays and functions Multidimensional arrays Sorting and algorithm efficiency An array is a sequence of values of the same

More information

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples. Outline Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples. 1 Arrays I Array One type of data structures. Consecutive group of memory locations

More information

QUIZ. What is wrong with this code that uses default arguments?

QUIZ. What is wrong with this code that uses default arguments? QUIZ What is wrong with this code that uses default arguments? Solution The value of the default argument should be placed in either declaration or definition, not both! QUIZ What is wrong with this code

More information

Programming in C/C Lecture 2

Programming in C/C Lecture 2 Programming in C/C++ 2005-2006 Lecture 2 http://few.vu.nl/~nsilvis/c++/2006 Natalia Silvis-Cividjian e-mail: nsilvis@few.vu.nl vrije Universiteit amsterdam News Check announcements on the C/C++ website

More information

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011 More C++ David Chisnall March 17, 2011 Exceptions A more fashionable goto Provides a second way of sending an error condition up the stack until it can be handled Lets intervening stack frames ignore errors

More information

Pointers and References

Pointers and References Steven Zeil October 2, 2013 Contents 1 References 2 2 Pointers 8 21 Working with Pointers 8 211 Memory and C++ Programs 11 212 Allocating Data 15 22 Pointers Can Be Dangerous 17 3 The Secret World of Pointers

More information

CSCI-1200 Data Structures Spring 2017 Lecture 5 Pointers, Arrays, Pointer Arithmetic

CSCI-1200 Data Structures Spring 2017 Lecture 5 Pointers, Arrays, Pointer Arithmetic CSCI-1200 Data Structures Spring 2017 Lecture 5 Pointers, Arrays, Pointer Arithmetic Announcements Submitty iclicker registration is still open. Even if you already registered on the iclicker website,

More information

CS 103 Lab 6 - Party Like A Char Star

CS 103 Lab 6 - Party Like A Char Star 1 Introduction In this lab you will implement a "hangman" game where the user is shown blanks representing letter of a word and then tries to guess and fill in the letters with a limited number of guesses.

More information

377 Student Guide to C++

377 Student Guide to C++ 377 Student Guide to C++ c Mark Corner January 21, 2004 1 Introduction In this course you will be using the C++ language to complete several programming assignments. Up to this point we have only provided

More information

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE? 1. Describe History of C++? The C++ programming language has a history going back to 1979, when Bjarne Stroustrup was doing work for his Ph.D. thesis. One of the languages Stroustrup had the opportunity

More information

KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming

KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 1 ARRAYS ~ VECTORS KOM3191 Object-Oriented Computer Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 2 What is an array? Arrays

More information

CSE 333 Midterm Exam 7/25/16. Name UW ID#

CSE 333 Midterm Exam 7/25/16. Name UW ID# Name UW ID# There are 7 questions worth a total of 100 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. The exam is closed book, closed notes,

More information

CS 103 Unit 11. Linked Lists. Mark Redekopp

CS 103 Unit 11. Linked Lists. Mark Redekopp 1 CS 103 Unit 11 Linked Lists Mark Redekopp 2 NULL Pointer Just like there was a null character in ASCII = '\0' whose ue was 0 There is a NULL pointer whose ue is 0 NULL is "keyword" you can use in C/C++

More information

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341) CSE 413 Languages & Implementation Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341) 1 Goals Representing programs as data Racket structs as a better way to represent

More information

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING 2011 C ++ Basics Review part 2 Auto pointer, templates, STL algorithms AUTO POINTER (AUTO_PTR) //Example showing a bad situation with naked pointers void MyFunction()

More information

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each): FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each): 1. If a function has default arguments, they can be located anywhere

More information

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak! //Example showing a bad situation with naked pointers CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING 2011 C ++ Basics Review part 2 Auto pointer, templates, STL algorithms void MyFunction() MyClass* ptr( new

More information

Chapter 6. Structured Data Types. Topics. Structured Data Types. Vectors and Arrays. Vectors. Vectors: subscripts

Chapter 6. Structured Data Types. Topics. Structured Data Types. Vectors and Arrays. Vectors. Vectors: subscripts Topics Chapter 6 Structured Data Types Vectors Arrays Slices Associative Arrays Records Unions Lists Sets 2 Structured Data Types Virtually all languages have included some mechanisms for creating complex

More information

Scientific Computing

Scientific Computing Scientific Computing Martin Lotz School of Mathematics The University of Manchester Lecture 1, September 22, 2014 Outline Course Overview Programming Basics The C++ Programming Language Outline Course

More information

Patterns: Working with Arrays

Patterns: Working with Arrays Steven Zeil October 14, 2013 Outline 1 Static & Dynamic Allocation Static Allocation Dynamic Allocation 2 Partially Filled Arrays Adding Elements Searching for Elements Removing Elements 3 Arrays and Templates

More information

Looping and Counting. Lecture 3 Hartmut Kaiser hkaiser/fall_2012/csc1254.html

Looping and Counting. Lecture 3 Hartmut Kaiser  hkaiser/fall_2012/csc1254.html Looping and Counting Lecture 3 Hartmut Kaiser hkaiser@cct.lsu.edu http://www.cct.lsu.edu/ hkaiser/fall_2012/csc1254.html Abstract First we ll discuss types and type safety. Then we will modify the program

More information

C++ Lecture 5 Arrays. CSci 588: Data Structures, Algorithms and Software Design.

C++ Lecture 5 Arrays. CSci 588: Data Structures, Algorithms and Software Design. C++ Lecture 5 Arrays CSci 588: Data Structures, Algorithms and Software Design http://www.cplusplus.com/doc/tutorial/arrays/ All material not from online sources copyright Travis Desell, 2013 Overview

More information

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors Arrays Returning arrays Pointers Dynamic arrays Smart pointers Vectors To declare an array specify the type, its name, and its size in []s int arr1[10]; //or int arr2[] = {1,2,3,4,5,6,7,8}; arr2 has 8

More information

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011 CS 61C: Great Ideas in Computer Architecture (Machine Structures) Introduction to C (Part II) Instructors: Randy H. Katz David A. Patterson http://inst.eecs.berkeley.edu/~cs61c/sp11 Spring 2011 -- Lecture

More information

Computer Components. Software{ User Programs. Operating System. Hardware

Computer Components. Software{ User Programs. Operating System. Hardware Computer Components Software{ User Programs Operating System Hardware What are Programs? Programs provide instructions for computers Similar to giving directions to a person who is trying to get from point

More information

Looping and Counting. Lecture 3. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

Looping and Counting. Lecture 3. Hartmut Kaiser  hkaiser/fall_2011/csc1254.html Hartmut Kaiser hkaiser@cct.lsu.edu http://www.cct.lsu.edu/ hkaiser/fall_2011/csc1254.html 2 Abstract First we ll discuss types and type safety. Then we will modify the program we developed last time (Framing

More information

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1 BEng (Hons) Electronic Engineering Cohort: BEE/10B/FT Resit Examinations for 2016-2017 / Semester 1 MODULE: Programming for Engineers MODULE CODE: PROG1114 Duration: 3 Hours Instructions to Candidates:

More information

CSCI 171 Chapter Outlines

CSCI 171 Chapter Outlines Contents CSCI 171 Chapter 1 Overview... 2 CSCI 171 Chapter 2 Programming Components... 3 CSCI 171 Chapter 3 (Sections 1 4) Selection Structures... 5 CSCI 171 Chapter 3 (Sections 5 & 6) Iteration Structures

More information