Review for the final - NYU Computer Science Department

Download Report

Transcript Review for the final - NYU Computer Science Department

Review for Final Exam
Dilshad M. Shahid
@ NYU
In this review
•
•
•
•
Arrays
Pointers
Structures
Java - some basic information
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
Values in
array
Array size (or length) = total number of elements
24
13
2
0
1
2
9
3
Subscript (starts at zero in C)
-3
6
10
-19
4
5
6
7
Highest subscript = size - 1
Arrays
• Declaring arrays:
int a[6];
Size of array
Data type
that array
will hold
Name of array
Arrays
• Other examples:
float b[10];
char words[15];
• Initializing arrays:
int a[6] = {12, 4, -9, 0, 5, 1};
Number of initializers must equal
or be less than size of array
otherwise compilation error
• If fewer initializers than total number of
elements in array, the rest are initialized to
zero
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
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
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
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
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
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];
row
column
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
Pointers
• Pointers hold values representing
memory locations
• Pointers are defined by the data type they
“point” to
• examples: int *a, float *b;
char *c;
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
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
Structures
• Example:
• struct person {
char name[30];
int age;
members
Keyword
char gender;
Structure tag
float salary;
};
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”
a
name
age
gender
salary
Structures
• You can have an array of structs
struct person people[5];
Each one is of type struct person
people
age
nam
e
age
nam
e
age
nam
e
age
nam
e
age
gender
salary
gender
salary
gender
salary
gender
salary
gender
salary
1
2
name
Array
name
0
Array subscripts
3
4
Structures
• You can have a pointer to struct
• struct person *p;
name
age
5040
p
gender
salary
Memory location: 5040
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:
Structures
struct person2 {
char name[30];
int age;
char gender;
float salary;
struct person2 *q;
} a, b;
A structure containing a
Graphically:
name
struct person2
a
This situation has to
be explicitly set up but
is possible with this
kind of struct
age
member that is a
pointer to the same
structure type is called
a self-referential
structure
gender
salary
q
struct person2
b
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
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)
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
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;
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;
}
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 */
}
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++
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
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