You are on page 1of 16

Arrays

1-Dimensional Array

Character

A character constant is an int value represented as a character in single quotes. E.g. 2 represents the int value of 2 i.e. 50 Also 65 - 90 for A - Z a z , 0 9, many special characters such as +, %, /, $, (space), etc.

Strings

Is a series of characters treated as a single unit. String may include letters, digits & various special character. Strings are always written in double quotation marks. e.g. House # 1879, Street # 182, Sector F-10/2, Islamabad, FirstName LastName

A string in C is an array of characters ending in null character (\0), e.g. Pakistan stored in memory as
0 1 2 3 4 5 6 7 8

\0

String can be stored in an array using cin (from iostream.h library) and gets (from stdio.h library) cin >> reads till the first white space (space, enter, tab) only.

Character Arrays to Manipulate Strings

E.g.
char string1[] = first; Actually six elements (5 + null character \0)

Input from Keyboard


char str2[20]; cin >> str2; // till first white space char. cout << str2; // print till \0 (not \0)

Test Example
# include <iostream.h> # include <stdio.h> void main(){ char str1[20]; cin >> str1; // input I am Pakistani cout << string 1: << str1 << endl; // output: string 1: I char str2[] = Love Pakistan; cout << string 2: << str2 << endl; // output: string 2: Love Pakistan char str3[20]; gets(str3); // input I am Pakistani cout << string 3: << str3 << endl; // output: string 3: I am Pakistani }

Passing Arrays to Functions

Only pass the name of array without [ ] and also the size of array should be passed as another argument. Arrays are always passed by reference (by default) Individual elements of array are always passed by value Example code is on next slide.

# include <iostream.h> ModifyArray (int a[ ], int size); ModifyElement (int e); void main(){ int b[5] = {0, 1, 2, 3, 4}; cout << Original values of array are: << endl; for (int i=0; i<5; i++) cout << b[i] << \t; cout << endl; cout << Before Modification b[3]: << b[3] << endl; ModifyElement (b[3]); cout << After Modification b[3]: << b[3] << endl; ModifyArray (b, 5); cout << Modified values of array are: << endl; for (int i=0; i<5; i++) cout << b[i] << \t; cout << endl;

ModifyArray (int a[ ], int size) { for (int i=0; i<5; i++) a[i] += 2; } ModifyElement (int e) { e += 2; }

Searching & Sorting

Searching: refers to the operation of finding the location of an item in data or printing some message that item doesnt exist in data.

Linear Search Algorithm Binary Search Algorithm

Sorting: refers to putting elements of a array in a certain order (ascending / descending).

Linear Search

Linear search compares each element of the array with the search key (item to be found). Works fine for small data set. Discuss Algorithm

# include <iostream.h> void main()


{

int A[10]; for (int i=0; i<10; i++) cin >> A[i]; int item, found =0; cin >> item; for (int i=0; i<10; i++) if(A[i] == item) { cout << i << endl; found = 1; } if(found == 0) cout << Data item not Found << endl;
}

2-Dimensional Array

2-D array represents table of values, containing information arranged as rows & columns. To identify a particular element, specify two subscripts [r, c] 2-D Array of 3 rows and 4 columns is called 3-by-4 array

Array Declaration & Initialization


int a[2][2]; //represent on board int b[2][3] = { {2, 3, 4}, {6, 7, 8} }; //represent on board int b[2][3] = {1, 2, 3, 4, 5}; //represent on board int b[2][3] = { {1, 2}, {4} }; //represent on board

Discuss

Initialize complete array with 0 Calculate sum of array. Sum of each column in the array Sum of each row in the array Sum of all even nos. in the array Print maximum number in each row. Print minimum number in each column.

You might also like