You are on page 1of 10

SJES 2271/ SJEM 2231

3/26/2014

What is an array?
 A list of variables of same type that are
referred to by the same identifier name.

SJES 2271 : Scientific Computing 1


SJEM 2231 : Structured Programming
LECTURE NOTES (WEEK 6)
BY
DR NOOR FADIYA MOHD NOOR

One-dimensional Array
 An array with 1 list of related variables i.e.
number of stud, list name of patients, etc.
 Total array size in bytes =
(no. of bytes in type)(no. of elements)
 Example: int sample[10]
- has 10 elements
- has index from 0 to 9.
 Character array is the most common onedimensional array

DR NOOR FADIYA MOHD NOOR, UM

 Array variables are differentiated by


subscript; size written inside brackets [ ].
 Can be defined as any data types in C++ i.e.
integer array, double floating-point array,
character array except string array.
 The format for 1-dimensional array:
datatype indentifiername[size]

Example
#include <iostream>
#include <cstring>
using namespace std;
void f1();
int main()
{
f1(); f1();
return 0;
}
void f1()
{
char s[80]="this is a test\n"; cout << s;
strcpy(s, "CHANGED\n"); cout << s;
}

SJES 2271/ SJEM 2231

The previous program displays the


following output:

3/26/2014

Initializing Arrays
 Assigning first values to arrays.

this is a test
CHANGED
this is a test
CHANGED

Initialization In Array
Declaration
 char city[5]=Oslo;
 int age[5]={12, 8, 2, 23, 16};
 char Letter[8]={A, B, C, D, E, F, G, H};
 double sales[ ]={3100.12, 41497.98, 17143.35};
 double sales[3]={3100.12, 41497.98,17143.35};

DR NOOR FADIYA MOHD NOOR, UM

 C++ initializes global arrays to null zeros.


 Two ways to initializes variable arrays:

a) At declaration time
b) In the program

Initialization In Program
 An array cannot be assigned to another array
directly using assignment operator (=) except
in a program loop.
 If sales[5] is to be initialized with profits[5] i.e.

for (i=0; i<5; i++)


{
sales[i]=profits[i];
}

SJES 2271/ SJEM 2231

3/26/2014

Example
#include <iostream>
using namespace std;
int main()
{ int sample[10];
int t;
for(t=0; t<10; ++t)
{ sample[t]=t;}
for(t=0; t<10; ++t)
{ cout << sample[t] << ' '; }
return 0;
}

2-dimensional Arrays
 Called as table or matrix which have two
dimensions.
 Array of arrays with two subscripts

i.e. int matrix[3][5], double coordinates[4][7]


 The format:
datatype identifiername[size_1][size_2]
 Total size of array in byte=
(no. of bytes in type)(row)(column)

DR NOOR FADIYA MOHD NOOR, UM

Array Referencing
 Array can be referred using regular subscript
notation or by modifying the array address.
 Other ways to refer the array i.e. amt[3]

a) (amt+3)[0]
b) (3+amt)[0]
c) (amt+0)[3]
d) (amt+1)[2]
e) (-2+amt)[5]

Initialization of
2-dimensional Array
Examples:
a) int num[1][3]={8,2,5};
b) int num[2][3]={{8,2,5},{3,6,7}};
OR int num[2][3]={8,2,5,3,6,7};
c) int num[3][3]={{8,2,5},{3,6,7},{1,1,4}};
OR int num[3][3]={8,2,5,3,6,7,1,1,4};
d) int num[3][4]={0};

SJES 2271/ SJEM 2231

3/26/2014

Using For Loop To Define


2-dimensional Array

2-dimensional
Array

Example: int matrix[5][10]

C++ mapped 2-dimensional


arrays to single
dimension memory stored
in row order.

for (int row=0; row<5, row++)


{ for (int col=0; col<10; col++)
{
int matrix[5][10];
cout<< matrix[row][col]<<endl;
}
}

Example:
int table[4][4]

Multidimensional Arrays

Multidimensional Array

 Called as tables or matrices which have more


than two dimensions.
 Arrays of arrays with more than two
subscripts.
 The format:
datatype name[size_1][size_2][size_n]
 Example:
a) int list[3][5][4]
b) double points[2][2][6][3][1]

 It is optional to state the maximum subscript


on the first dimension when receiving arrays.

DR NOOR FADIYA MOHD NOOR, UM

 Example:

- int score[9][2][5]) OR int score[ ][2][5])


- double points[2][2][6][3][1] OR
double points[ ][2][6][3][1]
 Total size of array in byte=
(no. of bytes in type)(size_1)(size_2)(size_n)

SJES 2271/ SJEM 2231

3/26/2014

Pointers
 A variable that points to a memory address
value of other variable.
 The general format:

datatype *identifiername
 Identifiername is the name of the pointer
variable.

The previous program displays the


following output:

Pointers are fun to use.

DR NOOR FADIYA MOHD NOOR, UM

Example
#include <iostream>
using namespace std;
int main()
{
char *s;
s = "Pointers are fun to use.\n";
cout << s;
return 0;
}

Example
#include <iostream>
using namespace std;
int main()
{ int balance; int *balptr; int value;
balance = 3200;
balptr = &balance;
value = *balptr;
cout << "balance is: " << value << '\n';
return 0;
}

SJES 2271/ SJEM 2231

The previous program displays the


following output:

balance is: 3200

The previous program displays the


following output:
hello tom
Hello tom
HEllo tom

3/26/2014

Example
#include <iostream>
#include <cctype>
using namespace std;
int main()
{ char str[20] = "hello tom"; char *p; int i;
p = str; cout<<p<<endl;
for(i=0; p[i]; i++)
{ p[i] = toupper(p[i]); cout << p<<endl;
}
return 0;
}

Character Array
 An array of character which used to holds
string in memory.
 Example: without character pointer

HELlo tom
HELLo tom
HELLO tom
HELLO tom
HELLO Tom
HELLO TOm
HELLO TOM
Press any key to continue . . .

DR NOOR FADIYA MOHD NOOR, UM

SJES 2271/ SJEM 2231

3/26/2014

Character Array

Character Array
 The previous format uses too much memory
than what is needed.
 Character pointers preceded with asterisk (*)
fix this problem.

Character Array

Character Array
 The previous strings can be printed such as:

cout<<*names;
cout<<*(names+1);
cout<<*(names+2);
cout<<*(names+3);
cout<<*(names+4);

DR NOOR FADIYA MOHD NOOR, UM

\\prints George
\\prints Michelle
\\prints Joe
\\prints Marcus
\\prints Stephanie

SJES 2271/ SJEM 2231

3/26/2014

Sorting Array
 Exchanging values of elements back and
forth.
 Mainly used to swap values or arrange array
in certain order.
 Three methods to sort array:

a) Bubble sort
b) Quick sort
c) Shell sort

Bubble Sort
 Values in an array are compared to each
other, a pair at a time and swapped if they are
not in back-to-back order.
 The lowest value floats to the top as bubbles
float in water.
 Easy procedure but the slowest among other
methods.

Bubble Sort
Example: Sorting the array[5]={ 3, 2, 5, 1, 4}.

Bubble Sort

DR NOOR FADIYA MOHD NOOR, UM

SJES 2271/ SJEM 2231

3/26/2014

Quick Sort
 A divide and conquer algorithm.
 The steps are:

1. Choose a pivot from the list


2. Rearrange the smaller number to the left
side and the greater number to the right
side of the pivot.
3. Reapply Step 1 & 2 for smaller list of
numbers

Quick Sort
{ 1, 2, 5, 7, 7, 14, 3, 26, 12} is 7<8.5<3?
{ 1, 2, 5, 7, 3, 14, 7, 26, 12} is 14<8.5<7?
{ 1, 2, 5, 7, 3, 7, 14, 26, 12}
{ 1, 2, 5, 7, 3, 7} & {14, 26, 12}
Second Passage:
{ 1, 2, 5, 7, 3, 7}
{14, 26, 12}

Quick Sort
Example: { 1, 12, 5, 26, 7, 14, 3, 7, 2}
First Passage:
{ 1, 12, 5, 26, 7, 14, 3, 7, 2} is 1<8.5<2 ?
{ 1, 12, 5, 26, 7, 14, 3, 7, 2} is 12<8.5<2 ?
{ 1, 2, 5, 26, 7, 14, 3, 7, 12} is 5<8.5<7 ?
{ 1, 2, 5, 26, 7, 14, 3, 7, 12} is 26<8.5<7?

Quick Sort
{ 1, 2, 5, 7, 3, 7} (1)
{ 1, 2, 5, 7, 3, 7} is 1<4.17<7?
{ 1, 2, 5, 7, 3, 7} is 2<4.17<3?
{ 1, 2, 5, 7, 3, 7} is 5<4.17<3?
{ 1, 2, 3, 7, 5, 7}
{1, 2, 3} & {7, 5, 7}

(1)
(2)

DR NOOR FADIYA MOHD NOOR, UM

SJES 2271/ SJEM 2231

3/26/2014

Quick Sort
{14, 26, 12} (2)
{14, 26, 12} is 14<17.33<12?
{12, 26, 14} is 26<17.33<14?
{12, 14, 26}
{12, 14} & {26}

Quick Sort
Third Passage:
{1, 2, 3}
{7, 5, 7}
{12, 14}
{26}

(3)
(4)
(5)
(6)

Quick Sort
{1, 2, 3}
(3) &
{7, 5, 7}
(4)
{1, 2, 3} is 1<2<3? & {7, 5, 7} is 7<6<7?
{ 1, 2, 3} & {7, 5, 7} is 7<6<5?
{1, 2, 3} & {5, 7, 7}
(3)+(4): {1, 2, 3, 5, 7, 7}
(7)

Quick Sort
{12, 14}

(5)
&
{26}
{12, 14} is 12<13<14?
{12, 14} & {26}
(5)+(6): {12, 14, 26}

(6)

(8)

(7)+(8): {1, 2, 3, 5, 7 ,7, 12, 14, 26}

DR NOOR FADIYA MOHD NOOR, UM

10

You might also like