Lecture 1
Lecture 1
Data Types
Simple (basic)
char, int, float, double
Modifiers
signed, unsigned
Qualifiers
static, const, void
Structured (derived)
Arrays, structures, unions, classes
Advanced (composite)
List, queues, stacks, trees, graphs
Data Structures
Goal: organize data Criteria: facilitate efficient
storage of data retrieval of data manipulation of data
Process:
select and design appropriate data types
Tradeoff
simplicity of data organization simplicity/elegance of algorithms Simple (unsophisticated) data strucuture may require much work for processing data More complex data organization may yield nicer algorithms for the basic operations
e.g.
double score[100];
Better to use a named constant to specify the array capacity: const int CAPACITY = 100; double score[CAPACITY];
Subscript operator
[] is an actual operator and not simply a notation/punctuation as in some other languages. Its two operands are an array variable and an integer index (or subscript) and is written array_name[i] Here i is an integer expression with 0 <= i <= CAPACITY 1. [] returns a reference to (alias of) the element in location i in array_name; array_name[i]is a variable, called an indexed (or subscripted) variable, whose type is the specified element_type of the array. An array reference can be used on the left side of an assignment, in input statements, etc. to store a value in a specified location in the array. // Zero out all the elements of score for (int i = 0; i < CAPACITY; i++) score[i] = 0.0; // Read values into the first numScores elements of score for (int i = 0; i < numScores; i++) cin >> score[i]; // Display the values stored in the first numScores elements for (int i = 0; i < numScores; i++) cout << score[i] << endl;
How well does this implement the general definition of an array? Nicely! As an ADT In C++
ordered indices numbered 0, 1, 2, . . ., CAPACITY - 1 fixed size CAPACITY specifies the capacity of the array same type elements element_type is the type of elements direct access Subscript operator []
Array Initialization
In C++, arrays can be initialized when they are declared. Numeric arrays:
element_type num_array[CAPACITY]={list_of_initial_values}; double rate[5] = {0.11, 0.13, 0.16, 0.18, 0.21};
4 0.21
Note 1: If fewer values supplied than array's capacity, remaining elements assigned 0. double rate[5] = {0.11, 0.13, 0.16};
3 0
4 0
Note 2: It is an error if more values are supplied than the declared size of the array. How this error is handled, however, will vary from one compiler to another. Note 3: If no values supplied, array elements are undefined (i.e., garbage values).
Character arrays
may be initialized in the same manner as numeric arrays. char vowel[5] = {'A', 'E', 'I', 'O', 'U'};
0 vowel A 1 E 2 I 3 4 O U
Note 1: If fewer values are supplied than the declared size of the array, the zeroes used to fill uninitialized elements are interpreted as the null character '\0' whose ASCII code is 0. const int NAME_LENGTH = 10; char collegeName[NAME_LENGTH]={'C', 'a', 'l', 'v', 'i', 'n'};
0 collegeName C 1 a 2 l 3 v 4 i 5 6 7 8 9 n \0 \0 \0 \0
How well does this implement the general definition of an array? Nicely! As an ADT In C++
ordered indices numbered 0, 1, 2, . . ., CAPACITY - 1 fixed size CAPACITY specifies the capacity of the array same type elements element_type is the type of elements direct access Subscript operator []
Note: This explains the brackets in constant declarations such as const char IN_FILE[] = "employee.dat";
Addresses
When an array is declared, the address of the first byte (or word) in the block of memory associated with the array is called the base address of the array. Each array reference is then translated into an offset from this base address. For example, if each element of array score will be stored in 8 bytes and the base address of score is 0x1396. A statement such as cout << score[3] << endl; requires that array reference score[3] be translated into a memory address: score[3] 0x1396 + 3 * sizeof(double) = 0x1396 + 3 * 8 = 0x13ae The contents of the memory word with this address 0x13ae can then be retrieved and displayed. An address translation like this is carried out each time an array element is accessed.
The value of array_name is actually the base address of array_name array_name + index is the address of array_name[index]. An array reference is equivalent to array_name[index] *(array_name + index)
* is the dereferencing operator; *ref returns the contents of the memory location with address ref.
For example, the following statements are equivalent: cout << score[3] << endl; cout << *(score + 3) << endl;
Test 2 Test 3 93.5 89.0 68.0 84.5 78.5 70.0 : : : : 99.5 100.0
Initialization
List the initial values in braces, row by row; May use internal braces for each row to improve readability.
double rates[2][3] = {{0.50, 0.55, 0.53}, // first row
Arrays as Parameters
Passing an array to a function actually passes the base address of the array. Thus the formal parameter has the same address as the actual argument. Hence, modifying the parameter will modify the corresponding array argument. Array capacity is not available to function unless passed as a separate parameter. The following function prototypes are all equivalent. void Print(int A[100], int theSize); void Print(int A[], int theSize); void Print(int *A, int theSize);
For example, typedef double TwoDimArray[30][4]; TwoDimArray scoresTable; void Print(TwoDimArray table, int rows, int cols)
. . .