Professional Documents
Culture Documents
Data Structures: Lecture 1: Array
Data Structures: Lecture 1: Array
Lecture 1: Array
The first Data Structure - An Array!
a 3 7 5 9 4 1 . . 2
For Example 0 1 2 . . . . . 99
int a[100];
data_type array_name[array_size];
Here,
data_type : declares the data type of each element.
array_name is any valid C / C++ identifier, defines the common
name that use as a reference of the elements.
array_size defines how many elements the array will hold/store.
The first line declares an integer array id and it immediately assigns the values
1, 2, 3, ..., 7 to id[0], id[1], id[2],..., id[6].
In the second line assigns the values 5.6 to x[0], 5.7 to x[1], and so on.
Similarly the third line assigns the characters ‘a’ to vowel[0], ‘e’ to vowel[1],
and so on. Note again, for characters we must use the single apostrophe (’) to
enclose them. Also, the last character in the array vowel is the NULL character
(‘\0’).
Initializing With a String
char fName[6] =
{'H', 'e', 'n', 'r', 'y', '\0'};
Character arrays (Strings)
Review char A[5] ;
A[0] A
A[1] L
A L I \0 A[2] I
A[3] \0
A[4] Garbage
Partial Array Initialization
12 17 15 11
21 25 27 33 44
0 1 2 3 4 (n-1) S-1
1. Add a new item=71 at the end (i = last+1 or i=n)
21 25 27 33 44 71
0 1 2 3 4 i S-1
array[n] = item;
//Num of item and last subscript need to increase by one
n++;
2. Add a new item=71 at the kth location (i = k = 2)
21 25 27 33 44
0 1 2 3 4 (n-1) S-1
First we need to do right shift of data from last location to kth
location then we will insert new item at kth location.
21 25 27 33 44
0 1 k=2 3 4 5 S-1
array[ k ] = item;
//Num of item and last subscript need to increase by one
n++;
21 25 71 27 33 44
0 1 2 3 4 5 S-1
3. Add a new item = 71 at the beginning (i=0)
21 25 27 33 44
0 1 2 3 4 (n-1) S-1
array[0] = item;
//Num of item and last subscript need to increase by one
n++;
71 22 25 27 33 44
0 1 2 3 4 5 n-1
Delete Operations
int array[10]={2,5,7,3,4};
2 5 7 3 4
1. Delete last item
2 5 7 3
0 1 2 3 4 5 n-1
array[last] = NULL;
2. Delete kth location item (k = 2)
2 5 3 4
0 1 2 3 4 5 n-1
2 5 3 4
0 1 2 3 4 5 n-1
for(i=k; i<last; i++)
array[i] = array[i+1];
No of item and last subscript need to decrease by one
n--;
Delete Operations
int array[10]={2,5,7,3,4};
2 5 7 3 4
84 78
92 97
string name[4];
28
Arrays with Three or More Dimensions
#define n 7
int main()
{
int i, total = 0, y[n] = {6,9,2,4,5,23,12};
for (i=0; i<n; i++)
{
cout<<y[i]<<" "; // display the array contents...
total = total + y[i]; // do the summing up…
}
// display the result...
cout<<"\nSum of 7 numbers in an array is = "<<total<<endl;
return 0;
}
32
Finding the Highest Value in an Array
(LAB work)
int count;
int highest;
highest = numbers[0];
for (count = 1; count < SIZE;
count++)
{
if (numbers[count] > highest)
highest = numbers[count];
}
When this code is finished, the highest variable will contains the
highest value in the numbers array.
Finding the Lowest Value in an Array
(LAB work)
int count;
int lowest;
lowest = numbers[0];
for (count = 1; count < SIZE; count+
+)
{
if (numbers[count] < lowest)
lowest = numbers[count];
}
When this code is finished, the lowest variable will contains the lowest
value in the numbers array.
1-34
Passing One Dimensional Arrays To Function
(Lab work)
A function can receive the address of an array by using:
1. A pointer.
2. A sized array e.g. s[20] or
3. An unsized array e.g. p[ ].
For example:
// pointers, will be explained in another Module
int myfunction(float *x)
// sized array
char yourfunction(float x[5])
// unsized array
void ourfunction(float x[ ])
36
How to determine length of the
string (self practice)
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
int len_str(char str[25]);
int len_str_while(char s[25]);
void main(void)
{
char l[25];
cin >> l;
cout << len_str(l) << endl << len_str_while(l);
}