Download presentation
Presentation is loading. Please wait.
Published byRandolph Mitchell Modified over 9 years ago
1
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers we create are visible in the block they are created, and in any sub-blocks What blocks are we talking about The global block – the entire program Function blocks – the code following a function definition, contained in {} Condition blocks – the code inside the {} of an if or else Loop blocks – the code inside the {} of a for or while loop Generic blocks – any code contained within {}
2
Scope... int main(){ int x = 3; fun();... } void fun(){ cout << x; //This will cause problems }
3
Scope … int x = 0; //This is global void fun(); int main(){ int x = 1;//this is local to main cout << x << endl; fun(); } void fun(){ cout <<x << endl;//what do we see here? }
4
Design We can generally break big problems down into smaller ones Sometimes the smaller ones can be broken down again Often we break down to the point where the small problems are handled by a single function This allows us to design a program to solve larger problems through the use of simple to write functions
5
Design Example “Write a program to read a text file containing names and output a new file with the names sorted and formatted” What are the small problems (look for the verbs)? - read- output- sort- format
6
Design So we’ll probably want functions that can void readFile(string names[]); void sortNames(string names[]); void formatNames(string names[]); void outputFile(string names[]);
7
Design Our main then simple can call the names in the order described by the problem int main(){ string names[100]; readFile(names); formatNames(names); sortNames(names); outputFile(names); }
8
Arrays Arrays allow us to make a collection of variables, each identified with the same label Each element is of the same type We refer to a specific element using an index Index are from 0 to NUM-1, where NUM is the number of elements in the array int x[5]; // an array of 5 int X[3] = 2; //sets the 4 th element to 2
9
Arrays We can create multi-dimensional arrays double y[3][7]; // a 3x7 matrix of doubles again, elements are indexed in the same manner y[2][5] = 1.5; //The element at 2,5, or row 2, col 5
10
Arrays Working with Most commonly problems deal with moving through the array - output every 2 nd element - add all elements - find the maximum element
11
Arrays Arrays are special in that they are always passed by reference into functions (they just are) This is true for 2-d arrays, but in this case we must always indicate the size of the 2 nd dimension( the columns) int fun(int a[], fload b[][5]) Care must always be taken to keep track of the size of the arrays
12
For Loops The other main form of looping (and often used when dealing with arrays and indexing) are for loops For loops allow us to declare code that is executed before the loop, a condition, and code that is executed at the end of each loop for (initialization code; condition; end of loop code) { code block; }
13
For Loops The most common form of for loops utilizes a counting variable, a condition to stop the loop, and a count update for(int i = 0; i < 5; i++) { cout << i << ‘,’; //We get 0,1,2,3,4, }
14
Strings String is a class (kind of like a type) which not only holds data, but also provides built in functions We declare objects (kinda like variables) which holds data strings, and allows us to call the built in functions to operate on the data Strings hold characters string s = “I’m a string”;
15
Strings We can do all kinds of fun things with Strings string s = “I’m a string”; cout << s; // output them s.length(); // find out how many characters int p = s.find(‘a’); //find the index of a character s.erase(p,2); // remove characters cout <<s; // What do we output now “I’m string”
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.