Download presentation
Presentation is loading. Please wait.
Published bySavana Banford Modified over 9 years ago
1
Review for Final Exam Dilshad M. Shahid @ NYU
2
In this review Arrays Pointers Structures Java - some basic information
3
Arrays Homogeneous - can only hold data of one type. Arrays occupy contiguous (the elements are literally right next to each other) space in physical memory 24 1329 -3 6 10 -19 0 1 2 3 4 5 6 7 Subscript (starts at zero in C) Highest subscript = size - 1 Values in array Array size (or length) = total number of elements
4
Arrays Declaring arrays: int a[6]; Data type that array will hold Name of array Size of array
5
Arrays Other examples: float b[10]; char words[15]; Initializing arrays: int a[6] = {12, 4, -9, 0, 5, 1}; If fewer initializers than total number of elements in array, the rest are initialized to zero Number of initializers must equal or be less than size of array otherwise compilation error
6
Arrays Usually use for loops in array processing. Size of array is fixed so you know when to stop looping through the array. #define is used to specify size of array Examples on web and in homework solutions
7
Arrays Character arrays are a little different. Can use them for strings (i.e. words like “Dilshad” or “hello”) Strings end with the null termination character \0
8
Arrays Arrays are passed to functions by simulated call by reference. So changes to array elements made inside a function occur in the original location in memory Arrays do not have to be passed back to main() for changes to be updated Arrays can be very large so using call by value (working with copies of variables) to pass arrays to functions would be a waste of space and time
9
Arrays Sorting information stored inside arrays has been an area of computer science research where many famous algorithms have originated One algorithm is the Bubble Sort, so called because smaller values “bubble” their way to the top of the array - key section is the swapping Examples on web and in text
10
Arrays A much faster and famous algorithm is the Quick Sort The Quick Sort algorithm won the Turing prize (the equivalent of the Nobel prize for computer science) The code is much harder than that for the Bubble Sort
11
Arrays Multidimensional arrays For a double subscripted array can think of a table layout with rows and columns Declaring a 3 by 4 array called n: int n[3][4]; rowcolumn
12
Arrays When passing double subscripted array to a function, must specify column size (in function prototype also) int func1(int [][4]); /* prototype */ int func1(int n[][4]) { /* code here */ } In function definition, must give name of array
13
Pointers Pointers hold values representing memory locations Pointers are defined by the data type they “point” to examples: int *a, float *b; char *c;
14
Pointers Please refer to slides on pointers and handout on pointers posted on the web. Final thoughts: –pointers are used to simulate call by reference –almost interchangeable with arrays (the name of an array is itself a pointer to the array). See example on web and in text. –please read text on pointer arithmetic, very clear explanation given there
15
Structures Structures are collections of related variables which may be of different data types Structures are heterogeneous Derived data types - structures are made of up objects of other types
16
Structures Example: struct person { char name[30]; int age; char gender; float salary; }; Structure tag members Keyword
17
Structures To declare an instance of a struct: struct person a; Think of the struct as a template or a cookie cutter. The name of the cookie cutter is “person” You would use this “person” cookie cutter to create a cookie called “a” name age gender salary a
18
Structures You can have an array of structs struct person people[5]; name age gender salary 0 1 2 3 4 nam e age people Array name Array subscripts Each one is of type struct person age gender salary nam e
19
Structures You can have a pointer to struct struct person *p; p Memory location: 5040 5040 name age gender salary
20
Structures A structure cannot contain an instance of itself But you can have, as a member of the structure, a pointer that points to that structure For example:
21
Structures struct person2 { char name[30]; int age; char gender; float salary; struct person2 *q; } a, b; Graphically: name age gender salary q struct person2 b struct person2 a A structure containing a member that is a pointer to the same structure type is called a self-referential structure This situation has to be explicitly set up but is possible with this kind of struct
22
Structures Self referential structures are used to create linked data structures like linked lists and binary trees So you would need two things to create dynamic data structures: pointers and structures
23
Structures Initializing structures One way: struct person a = {“Dilshad”, 25, ‘F’, 1000000.0}; If fewer initializers in list than members in the structure, remaining members are automatically initialized to zero (or NULL if the member is a pointer)
24
Structures Accessing members of a structures Two ways (and two operators): –use the structure member operator (.) also called the dot operator –use the structure pointer operator (->) also called the arrow operator
25
Structures struct person a; struct person *p; p = &a;/* setting p to point to address of a */ /* using the dot operator, direct access */ a.name = “Dilshad”; a.age = 25; a.gender = ‘F’; a.salary = 1000000.0; /* using the arrow operator, equivalent to above */ p->name = “Dilshad”; p->age = 25; p->gender = ‘F’; p->salary = 1000000.0;
26
Structures /* Another example using array of structures */ struct person people[5]; int i;/* for loop counter */ /* using the dot operator, direct access */ /* initializing entire array of structures with the following information */ for (i=0; i < 5; i++) { people[i].name = “Dilshad”; people[i].age = 25; people[i].gender = ‘F’; people[i].salary = 1000000.0; }
27
Structures /* Another example using array of structures */ struct person people[5]; struct person *p; int count=0; p = people; /* setting pointer to point to array */ /* I don’t need the & in front of people because the name of an array is itself a pointer to the array */ /* initializing entire array of structures with the following information */ while (count < 5) { p->name = “Dilshad”; p->age = 25; p->gender = ‘F’; p->salary = 1000000.0; count++; /* to keep track of where I am in the array */ p++; /* incrementing pointer to the next structure in array */ }
28
Java One of the hottest programming languages around today - believe the hype Came from C and C++. Syntax is very similar to C Developed by James Gosling, VP at Sun Microsystems, to correct the shortcomings of C and C++
29
Java Java is object-oriented which means it takes the cookie cutter/cookie analogy as its basic philosophy In C, we saw how related data was put into a structure In object-oriented design, data and specific functions used to manipulate the data (and only that data) are put together in a class
30
Java See “Why is Java important?” and all the 5 points on the Java slides on the webpage See “Future of Java” on the Java slides on the webpage
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.