cs31 lecture 8

X you will be asked c quizzes 25 points understand

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: m (50 points) • Esp. find mistakes in a program, find good test cases, etc. Write a real-life algorithm (25 points) x To prepare: Know your C++! Do the C++ quizzes on CourseWeb Know basics about strings (nothing more than in the projects) 28 Arrays x Arrays are a collection of data elements organized in a Arrays specific order specific Roughly: a set of variables of the same type that can be accessed Roughly: in an homogeneous way in x Vectors (see lect. 5) are a special case of array x Arrays are the core data structure to program useful things Matrices, images Data structures (stack, queue, graph, tree, etc.) can be Data programmed using arrays programmed 29 One-dimensional arrays in C++ x Syntax to declare an array: <type> <name>[<size>] Example: iint array1[10]; nt char mystring[256]; char x Syntax to pass a 1-d array allocated as above as argument Syntax to a function: to <type> <name>[[<size>]] Example: void foo(int array[10], char array2); 30 Some observations on arrays x Arrays are always pass-by-reference x Be careful: an array has the same liveness as a variable E.g., if declared within a function, the array is destroyed when the E.g., function exits function x Arrays are standard variables We can use references, have global arrays, put them in We namespaces, etc. namespaces, x The size of an array is set at its declaration It will not automatically increase when trying to “add” elements The program will crash if trying to access an element not in the The 31 Some observations on arrays x You cannot mix different types for the elements (but You techniques exist to overcome this: structs/unions) techniques x You cannot recover the size of the array from the variable You associated to it: only the declaration contains its size associated x The size of an array can be determined at run-time int main() { int a; std::cin >> a; int int array[a]; int } 32 A first data structure: Stack x Definition: a stack is a linear data structure, implementing Definition: the Last-in-First-Out principle the x Typical operations: Typical Push Pop x Stacks are used in numerous algorithms, and are Stacks ubiquitous in computers ubiquitous Handle function calls / memory Recursive algorithms can always be implemented with a stack 33 The StackLib in C++ 34 An algorithm using stacks function outputInBinary(Integer n) Stack s = new Stack new while n > 0 do while do Integer bit = n modulo 2 modulo s.push(bit) s.push(bit) if s is full then if is then return error return end if end n = floor(n / 2) floor(n end while end while s is not empty do while is do output(s.pop()) output(s.pop()) end while end end function 35...
View Full Document

This note was uploaded on 04/03/2014 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

Ask a homework question - tutors are online