EXAM 2 REVIEW-Fall2011

EXAM 2 REVIEW-Fall2011 - EXAM 2 REVIEW WHAT WE ...

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: EXAM 2 REVIEW WHAT WE DID THIS MONTH: 1. ARRAYS 2. FILE PROCESSING 3. POINTERS 4. STRINGS ARRAYS Probably the most important data structure we have seen up to now. An array is a collection of variables of the same type. It can be of any of the types that we have already seen, integer, real, character, logical and so on. It is declared as follows: float x[100]; // declares an array with 100 real elements int a={1, 3, 6, 0} // declares an integer array (how many elements are there?) char b[5]; // declares an array with 5 characters (remember that the last position is ALWAYS reserved for the terminating character, i.e. this array can hold up to 4 real characters plus one memory space for the ‘/0’.) Using the same logic we can declare n- dimensional arrays. A very simple example is a 2- dimensional array as follows float grades[100][5] in order to store the grades of 100 students in 5 projects. The first element of the array is stored at position 0 and so an array that has n elements goes up to element number n- 1. So int array={2, 3, 9} has 2 as its value in the 1st place (array[0]), 3 in the second place (array[1]) and 9 in the last place (array[2]). Be extremely careful with the bounds of an array because if you go out of bounds there will be no error to warn you and you will be free to mess up with memory spaces that belong to other programs. CALLING ARRAYS AS ARGUMENTS Arrays are passed by reference by default (why is that? Remember pointers..). This implies that any function has full access to the array at any point.. Also, any array is passed as an argument just by name (i.e., no after the array name). Why is that? Example: void fun1(int a); int main(){ int array={1,2,3} // declare an integer array with elements 1, 2 and 3. fun1(array); // what are the values of the array at this moment? …. } void fun1(int a){ a[0]=5; a[1]=19; a[2]=3; } FILE PROCESSING File input and output is very important when it comes to computer programming. We want to be able to store data (and read data) to (from) files because information that is written there is not immediately lost. In a normal program execution, we print important information on screen. When we terminate the program, the memory is released and our data are lost forever. Exactly like variables, files want to be declared. We use ifstream and ofstream for declaring files for input and output accordingly. Examples: ifstream file1; // create a file1 to read from ofstream file2; // create a file2 to write to We are not done yet. When we have simply declared the files, we don’t yet have access to real- life files on our computer, except if we associate them somehow. This happens with the method open(). Its syntax is as follows. file1.open(“test.txt”); //associate file1 with real- life file test.txt. If you are trying to read from a file that does not exist, you are going to encounter an error. What will happen when you are trying to write to a file that does not exist? Another useful method is eof(). It is the method that lets us know if we have reached the end of file while accessing a file that we are reading from. As soon as we have reached the end of file, we know that there are no more information to be read so we can quit the inputting (from the file) process. POINTERS Another problem that we ran into while using arrays was the fact that we need to assign an initial estimate of how many elements we need the array to have, even if we end up using much, much less (recall the student database project). In this part of the class, we saw pointers which represent places in memory of specific size (corresponding to the type at every time). The declaration of pointers is like: int * p; // declare a pointer p that points to an integer value char * q; // declare a pointer q that points to a character value Also, it is useful to remember the following: &x = the address of variable x *y = the value of the address y So, when we store a variable x then we can find the position where it is stored as follows: int x; cout << &x; Another useful property of pointers is that there is a correspondence with arrays: ARRAY NAMES ARE POINTERS! and *(array_name+i)=array_name[i] Note that this is the way that we can return more than one value with a function! (How?) Also very important: the new and the delete operators. With the new we can declare a new array of 10 elements, then delete it and change it into a new array with a different number of elements. (How?) STRINGS Strings are character arrays made easy. The problem with character arrays is that they are not as flexible when performing easy tasks like searching for a specific character or appending other characters at the end. All of these are managed a lot easier with strings. The most notable of the methods that have to do with strings are the following: .substr() - - - Returns a substring of the original one .find() - - - Returns the position of a character that you are looking for. If nothing is there, it returns string::npos .length() - - - Returns the length of the string .append(“…”) - - - Our string is now the previous plus the argument (the same can happen with a string s as follows: s+=”…”;) What you should know how to do: 1. find how many words are inside a string. 2. find specific information (i.e. when wanting just the color of a car and you know that it is within parentheses, be able to retrieve just the color). 3. find how many times a specific character appears in a string. 4. transform arithmetic strings into their numerical counterparts. ...
View Full Document

This note was uploaded on 11/16/2011 for the course COP 2271 taught by Professor Staff during the Fall '11 term at University of Florida.

Ask a homework question - tutors are online