cs31 lecture 10

Cpp example2 cppstringscpp 10 multi dimensional

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: ions on C++ strings Many operators available: ==, <=, >=, >, < Functions available to manipulate characters one by one toupper tolower isupper islower isalpha isdigit 9 Examples example1-cppstrings.cpp example2-cppstrings.cpp 10 Multi-dimensional static arrays in C++ Syntax to declare an array: <type> <name>[<size1>][<size2>]… Example: int array1[10][20]; char mystring[256][2][42]; Syntax to pass an array allocated as above as argument to a function: <type> <name>[<size1>][<size2>] Example: void foo(int array[10][20], char array2[256][2][42]); 11 More on passing arrays as arguments Example 1: a 1-dimensional array of int void foo1(int array, int n); Example 2: a 2-dimensional array of int, with 10 columns void foo2(int array[10], int nrows); Example 3: a 2-dimensional array of int, with 5 rows and 10 columns void foo3(int array[5][10]); 12 Some observations on arrays Arrays are always “pass-by-reference” actually, pass-by-value of the pointer to the start of the array Be careful: an array has the same liveness as a variable E.g., if declared within a function, the array is destroyed when the function exits Arrays are standard variables We can use references, have global arrays, put them in namespaces, etc. The size of an array is set at its declaration It will not automatically increase when trying to “add” elements 13 Some observations on arrays You cannot mix different types for the elements (but techniques exist to overcome this: structs/unions) You cannot recover the size of the array from the variable associated to it: only the declaration contains its size The size of an array can be determined at run-time int main() { int a; std::cin >> a; 14 Example example3-multidarray.cpp 15 A second data structure: Queue Definition: a queue is a linear data structure, implementing the First-in-First-Out (FIFO) principle Typical operations: Enqueue Dequeue Queues are used in numerous algorithms, and are also ubiquitous in computers Printing documents (“printer queue”) … 16 The QueueLib in C++ 17 Review 18 Loops Critical control-flow structure: iterative/repetitive process Almost all programs contain loops Typical uses: Traverse input data Do a repetitive treatment Sort an array … 19 Different kinds of loops For loop (algo, C++) Do loop (algo, C++) While loop (algo, C++) Repeat until loop (algo) 20 For loop in algorithms Syntax: for <loop increment rule> [step <step>] do <instructions> done The initializa...
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