Академический Документы
Профессиональный Документы
Культура Документы
Lecture Notes I
by
Ender Ozcan, Şebnem Baydere
Solving commercial, engineering and research problems using computers require many data
items. Each individual data item is an atomic piece of information contining a value about
some aspect of the problem.
A data item has a type, a set of values from which it can take a value from. For example, the
data item “age” has the type “non-negative integer”, and the data item “salary” has the type
“real number”. Some types are just reused types, such as, the data item “student id” is just a
“non-negative integer”, but note that we do not usually do arithmetic on students ids. There
are other types as well that are derived from the real world. Some of these types arouse due to
the related constraints and some of them are user defined types for structuring data. For
example, possible value of a month is restricted to one of twelve: January, February,...,
December. Real world problems, being complex problems, require processing and storage of
data in a structured manner for the sake of easeness.
Before starting to work at a company, you fill out an employee form. There are some data
items that you have to provide, such as, your name, last name, date of birth and so on. These
forms are received by the human resources department and kept in a file, a structure to hold a
set of records. In order to reach records in files fast, keys might be assigned to each record.
For example, 2382912 can be assigned to record of the employee Mr. Ay Sies.
There are two forms of structures in this example;
• record, representing a form, holding many individual, but linked items of data
• an array of items of the same type, that is the file.
There are two types of data:
• Empolyee information is a real world data.
• Structural information, it is about how the real data will be stored and accesed, e.g.
keys.
int nextEntry;
Synonyms
1
CSE211 Lecture Notes – 2004/2005-II
salary totalSalaries, nextSalary;
totalSalaries = totalSalaries + nextSalary;
Enumerated Types
enum month_t {January, February, March, April, May, June, July, August,
September, October, November, December}
enum month_t monthOfSeminars;
monthOfSeminars=January;
Structure (Record)
struct struct-name {
Struct-items // list of declarations
};
Remember that the array has two major benefits: we can index, therefore can reach each
element of the array directly, we can pass just the name off the array to a function.
One of the difference between arrays and structures is that we cannot simply loop over
structure item as we do in arrays. Because the type of the items may not be the same.
Structures may be copied and assign to, pass to a function and returned by a function.
The variable names in the declarations of a structure are called members. Member access
operator is the dot operator (.). If the variable is a pointer, then (->) is used
struct_object.member_name
struct student_r {
int ID;
char name[50];
char lname[50];
};
typedef struct student_r Student;
Student s1;
s1.ID = 9802070203;
s1.name[0] = ‘A’;
s1.name[1] = ‘L;
s1.name[2] = ‘İ’;
s1.name[3] = ‘\0’;
2.ARRAYS
2
CSE211 Lecture Notes – 2004/2005-II
#define MaxSize 5;
typedef int myIntArray_t[MaxSize];
myIntArray_t ids;
Let's clarify the difference between arrays and structures by an abstract example given below.
We start with the following definitions:
The Array is the basic mechanism for storing a collection of identically typed objects.
The Structure stores a collection of objects that need not be of the same type.
Abstract example: Consider the layout of an apartment building. Each floor might have a one-
bedroom unit, a two-bedroom unit, a three-bedroom unit and a utility room for the floor.
In this section we will see arrays and their relationship with pointers.
Declaration:
int a[10];
//defines an array of size 10, that is a block of 10 consecutive objects named a[0], a[1],….a[9].
a:
a[i] refers to the i-th element in the array. Compiler allocates a consecutive space for 10
integers.
In C, to allocate memory calloc, malloc functions are used, to deallocate memory, free
is used.
int *Arr2 = malloc( sizeOf( int ) * Size );
free(Arr2);
In C++, new [ ] and delete [ ]
int Arr1[ Size ]; // size is a compile time constant
int *Arr2 = new int [ Size ];
3
CSE211 Lecture Notes – 2004/2005-II
delete [ ] Arr2;
3. POINTERS
Sophisticated C (C++) programmers makes heavy use of pointers to access variables (objects)
in the program. Because:
• Sometimes it is the only way to express a computation
• They lead to more compact and efficient code.
Pointer: is a variable (object) that can be used to access the address of another variable
(object). It provides indirect access rather than direct access to an object.
Pointers and arrays are closely related. We will see how shortly.
If the pointers are used carelessly, it is easy to create pointers, which point to an unexpected
location. If used with a discipline, they simplify and clarify the code.
4
CSE211 Lecture Notes – 2004/2005-II
Let's see the use of pointers in real life situations:
• Somebody asks you for directions. If you do not know the answer you may give an
indirect answer. "Go to the shop X and ask for directions."
• Someone asks you for a phone number. Rather than giving an immediate answer you may
give an indirect reply "let me look it up in the phone book"
• When a Prof. says, "do problem 1.2 in the textbook", the actual homework assignment is
being stated indirectly.
• Looking up a topic in the index of a book. The index tells you where a full description
can be found.
• A street address is a pointer, (it tells you where someone resides) A forwarding address is
a pointer to pointer.
In C/C++, a pointer is a variable stored in a group of cells that can hold an address that is a
location in memory where other data are stored. Since an address is expected to be an integer
a pointer can usually be represented internally unsigned int or unsigned long depending on the
machine architecture.
What makes a pointer more than just a plain integer is that we can access the data that is
pointed at. This is called dereferencing.
• Address-of operator & : To have a pointer point at an object we need to know the target
objects memory address. & (unary operator) is used for this purpose.
p = &c; assigns the address of c to the pointer variable p (p is said to ‘point to c’)
The unary operator & only applies to objects in memory: variables, structures and array
elements. It cannot apply to expressions, constants or register variables.
5
CSE211 Lecture Notes – 2004/2005-II
Declaration: int x = 5;
int y = 7;
int *ptr; (the value represented by ptr is a memory address,
this declaration doesnot initialize ptr to any particular value)
Using pointers before assigning an address to it produces bad results and crash your program.
ptr x y
say *ptr = 10; dereferencing works for writing new values to the object as well
1000 x=10
1004 y=5
> 10
ptr x
1200 1000
5 7
6
CSE211 Lecture Notes – 2004/2005-II
ptr = &x;
*ptr = x; semantically incorrect. No compiler error. Because the statement says that the
integer to which ptr is pointing should get the value of x. For instance, if ptr is &y then y is
assigned the value of x. The assignment is legal but it does not make ptr point at x. Moreover,
since ptr is uninitialized dereferencing is likely to cause run-time error.
Pointer Arithmetic
Every pointer points to a specific data type. If ptr points to an integer x, then *ptr can occur in
any context where x could.
x = 3;
ptr = &x;
*ptr = *ptr + 10; increments *ptr by 10. (x becomes 13)
y = *ptr + 1; takes the value ptr pointing at , add 1 to it and assign the result to y.
(y becomes 14)
*ptr++ (increments the pointer value first, and then access the value, after this
operation ptr points to the next memory location)
Finally, since pointers are variables they can be used without dereferencing.
For example:
if both ptr1 and ptr2 are pointers to the same data type such as integer then
ptr1 = ptr2 sets ptr1 to point to the same location as ptr2, while
*ptr1 = *ptr2 assigns the value ptr1 points to the value ptr2 points to.
Question: give an example.
7
CSE211 Lecture Notes – 2004/2005-II
ptr1 x ptr2 y
after ptr1=ptr2 5
from initial state
ptr1 x
ptr2 y
ptr1 x
ptr2 y
In C, there is a strong relationship between pointers and arrays. Therefore they should be
discussed simultaneously. Any operation that can be achieved by array subscripting can also
be done with pointers. The pointer version is faster but harder to understand for the beginners.
In this section we will discuss the array implementation in C/C++ and see the relationship
between arrays and pointers. When a new array is allocated, the compiler multiplies the size
in bytes of the type of the array by the array size.
usually the actual number of bytes used for int, float, double declarations is machine
dependent.
After the allocation the size is irrelevant because the name of the array represents a pointer to
the beginning of allocated memory for that array.
8
CSE211 Lecture Notes – 2004/2005-II
The compiler allocates for consecutive blocks for the array elemts and then allocates storage
for i.
The values stored in a is the same as the value &a[0]. This equivalence is always quaranteed
and this tells that the array name (a in this example) is actually a pointer.
int *pa;
if pa points to a particular element of an array, then by definition pa+1 points to the next
element.
Since the array name is a pointer to the beginning of allocated memory then
pa = a also valid. (initializes the pointer pa to point to array a --- same as pa = &a[0])
Also, a reference to a[i] can also be written as *(a+i). In evaluating a[i], C/C++ converts it to
*(a+i) immediately; the two forms are identical.
A pointer s a variable, so pa = a and pa++ are legal. But an array name is not a variable;
constructions like a=pa and a++ are illegal.
void Swap(int &first, int &second) void Swap(int *first, int *second)
{ int temp= first; { int temp= *first;
first = second; *first = *second;
second = temp; *second = temp;
9
CSE211 Lecture Notes – 2004/2005-II
} }
Swap(x,y) Swap(&x,&y)
So...create new data types and declare exactly what the program can do to manipulate a
variable of that type. This provides a form of data integrity. And provides flexibility for future
modification of the data's form (i.e. for efficiency, bug-fixing, or enhancement).
A data type is a set of values and a set of operations defined on those values. An abstract
data type is a data type where the specification of the values, and the operations’ effects on
the values, is seperated from the representation of the values and the implementation of the
operations.
A complex number consists of a real part and imaginary part. We should be able to add,
subtract, multiply and divide complex numbers.
10
CSE211 Lecture Notes – 2004/2005-II
C Implementation
If one asks you to define an array, most probably you will say a consecutive set of memory
locations. This clearly reveals a common point of confusion; the distinction between a data
structure and its representation. It is true that arrays are implemented by consecutive memory
but intuitively, an array is a set of pairs; index and value. For each index which is defined
there is a value associated with that index. In mathematical terms we call this a
correspondance or a mapping. An array is declared by giving it a name and by telling the
compiler what type the elements are. If we are defining an array a size must also be provided.
The size can be omitted if it is initialized. The compiler count the number of initializers and
take that as the size.
Examples for ordered lists are the days of the week, values in a card desk, floors of a building.
If we consider an ordered list (seqeunce) more abstractly, we say that it is either empty or it
can be written as <a1, a2,a3……an> where the ai are atoms from some set S. There are a
variety of operations that are performed on these lists. These operations include:
In the study of data structures we are interested in ways of representing ordered lists so that
these operations can be carried out efficiently. Perhaps the most common way to represent an
ordered list is by an array where we associate the list element a i with the array index i. This
may be called sequential mapping because using the conventional array representation we are
storing ai and a i+1 into consecutive locations i and i+1 of the array. This gives us the ability to
retrieve or modify the values of random elements in the list in a constant amount of time,
essentially because a computer memory has random access to any word. We can access the
list element values in either direction by changing the subscript values in a controlled way.
In the following lectures we will see the problems with array implementations.
11