Professional Documents
Culture Documents
3/26/2014
What is an array?
A list of variables of same type that are
referred to by the same identifier name.
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
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;
}
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};
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.
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
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};
3/26/2014
2-dimensional
Array
Example:
int table[4][4]
Multidimensional Arrays
Multidimensional Array
Example:
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.
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;
}
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 . . .
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);
\\prints George
\\prints Michelle
\\prints Joe
\\prints Marcus
\\prints Stephanie
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
3/26/2014
Quick Sort
A divide and conquer algorithm.
The steps are:
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)
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)
10