Presentation is loading. Please wait.

Presentation is loading. Please wait.

COSC 220 Computer Science II

Similar presentations


Presentation on theme: "COSC 220 Computer Science II"— Presentation transcript:

1 COSC 220 Computer Science II
Lecturer: Dr. Joseph Anderson Office: 140 Devilbiss Hall Office Hours: Monday 9-11am, Wednesday 2-4pm, Friday 10-11am

2 Course information Website Canvas
Syllabus Schedule Canvas Duplicates of materials found on website Updated less periodically

3 Course overview Study more advanced problem-solving techniques with programming in C++ Become familiar with low-level development tools Command-line interface GCC will be used for compiling code How linking source and header files actually happens Linux environment Will begin using in the first lab

4 How to succeed Come to class Come to labs
Review text before/after each lecture Contains many small pieces of information that may not be covered in class or on exams, but can be useful later Experiment! If I try and write a program to do X, what will happen? Do it and find out! What if… Do it! What happened? Why? (Computer) Science

5 If you get stuck Stop. Don’t try and multitask
Change tasks, go eat, exercise, etc. Try again, back up to an earlier point Ask for help Having time to do this means start early enough to find problems!

6 CLI Development In addition to advanced C++ we will be using very low-level development tools Command line compilation and linking Pure text-based editing Linux filesystem and tools See webpage for helpful links Code editors Linux references

7 Review: arrays An array is a special data type that holds multiple values, of some other data type The data it holds can also be an array type! What does this mean? An array of arrays, or multidimensional array!

8 Array review Array declaration: int my_array[10];
const int my_size = 10; char my_word[my_size]; int my_array[] = {1, 2, 3, 4}; int my_array[5] = {1, 2, 3, 4}; // what is different? Array size must be constant, integer, bigger than zero Or, as above, implicit from an array literal

9 Array review What happens when an array is declared?
The compiler looks at 1) the type and 2) the size Allocates a block of memory of (type size) * (array size) bytes E.g. the ‘char’ type (usually) takes 1 byte of space, so an array of 10 char’s would take 10 bytes of memory E.g. an array of 10 doubles, which are 8 bytes each, would take 80 bytes of memory Consider the declaration: double prices[6];

10 Array REview Accessing an array Use the [] operator E.g.
int my_array[10]; my_array[3] = 5; int a = my_array[3]; Calling my_array[n] tells the compiler to access n*size bytes beyond the head of the array

11 Array review Caveats In C++, array indexing starts at 0
An n element array has indexes 0 through n-1 C++ provides no access verification The following is legal: int arr[3] = {1,2,3}; int b = arr[3]; // looks ok, but really not The compiler will access the head of the array plus 12 bytes But the array is only 12 bytes long!

12 Array review Making sure your array access is safe
Use variable to store size Use the size variable in loop iteration const int NUM_WEEKS = 52; int weeks[NUM_WEEKS]; // ... Code to populate the weeks array for(i=0; i < NUM_WEEKS; i++){ printf(“Checking week %d”, weeks[i]); }

13 Off-by-one Errors Consider: Does it compile? Does it work?
const int NUM_WEEKS = 52; int weeks[NUM_WEEKS]; // ... Code to populate the weeks array for(i=0; i <= NUM_WEEKS; i++){ printf(“Checking week %d”, weeks[i]); } Does it compile? Does it work?

14 Advanced for-loop on Array
C++11 introduces an easier and safer way to iterate: range-based for loops int days[5] = {2,4,6,8,10}; for( int val : days ){ printf(“%d”, val); } Will print: 2 4 6 8 10

15 Ranged-based for loops
Useful when you don’t need to know the index of the element Can be used with reference variables to modify array elements We will see more later, when working with pointers Still have to know the length of the array in advance Fortunately, there are techniques one can use to change the maximum length of an array during execution of a program We will see how to do this with pointers

16 Multidimensional arrays
The general form of an array definition is dataType name[]; What if we wanted to track two different indexes (e.g. a matrix of data): C++ Allows us to declare an array which takes two (or more) index parameters int myMatrix[2][5] = {{1,2,3,4,5}, {6,7,8,9,10}}; We can think of this as an “array of arrays” First index is the “row” and second is the “column” Can read the definition as “two arrays, each with five elements” Referencing myMatrix[1][3] would return 9, as we referenced the fourth element of the second array.

17 Variable addresses Recall that every variable in C++ is really just a reference to a memory location, along with type information about the data contained there Sometimes we may want to access the actual address where the data is stored char letter; int day; double price; For the above, we know that there are segments of memory 1 byte, 2 bytes, and 8 bytes long, respectively But we don’t know where! In C++ (not Java) we can find out!

18 pointers We have a simple operator for retrieving the location in memory The address operator: & It is a unary operator (takes only one argument, similar to ++ and --) The type of a pointer is (Type*) where “Type” is the type that it points to int a = 10; int* b = &a; // The “type” of data returned by &a is “int*” printf(“%d”,sizeof(a)); // prints “4” on most systems printf(“%d”,sizeof(b)); // prints “8” on a 64-bit system

19 Pointer dereference If you have a pointer, ptr1, how to access the variable it points to? Use the * (dereference) operator int a = 10; int *ptr = &a; int b = *ptr; // b == 10 printf(“%d”,*ptr); printf(“%d”,b);

20 Arrays as Function Parameters
Recall that we can use an array as a parameter to a function: void printArray(int my_arr[], int size){ for( int i=0; i < size; i++){ printf(“%d\n”,my_arr[i]); } What is actually passed is a pointer to the start of the array

21 Pointers as parameters
Recall: reference variables int my_age; void askForAge(int &age){ std::cout << “What is your age?\n” << std::endl; std::cin >> age; } Calling askForAge(my_age) would store the response in the variable without any extra work (you would still want to validate)

22 Null pointers When you declare a pointer, you should always initialize it If you don’t know what the first value should be, set it to 0, or the null pointer int *ptr = nullptr; // Use 0 or NULL if not in C++11 standard If by accident, you accessed the memory referenced by an uninitialized pointer, you could end up modifying memory you don’t mean to Because when the pointer is declared, it’s contents aren’t changed, so it could contain data that coincidentally is the address of a part of memory being used for something else!

23 Pointer comparison Pointers refer to addresses
As such, you can compare with < > == != >= <= ptr1 < ptr2 Compares the position in memory *ptr1 < *ptr2 Compares the values pointed to, if they can be compared

24 Pointer arithmetic As we saw before, the [i] operation really just says “the beginning of the array plus i more elements” This is just calculated as “my_arr + i*sizeof(int)” But C++ is smart enough so that you don’t need sizeof above: *(my_arr + i) Gives same as my_arr[i]


Download ppt "COSC 220 Computer Science II"

Similar presentations


Ads by Google