You are on page 1of 50

An array in a variable with multiple values

It allows you to Stack the same variable with


different value.
They allow you to use a single variable and
attach different values to it.
myarray[0]
int myarray[5];
myarray[1]
myarray[2]
myarray[3]
myarray[4]
Assigning values ?
int myarray[ 5 ];
myarray[0] 0
for ( int i = 0; i < 5; i++ )
myarray[1] 0
myarray[ i ] = 0;
myarray[2] 0
myarray[3] 0
myarray[4] 0
int main()
{
int myarray[ 5 ] = { 2, 7, 4, 8, 5 };
return 0;
}
myarray[0] 2
myarray[1] 7
myarray[2] 4
myarray[3] 8
myarray[4] 5
int main()
{
int myarray[5]; myarray[0] 5
myarray[0] = 5;
myarray[1] 8
myarray[1] = 8;
myarray[2] = 2; myarray[2] 2
myarray[3] = 1; myarray[3] 1
myarray[4] = 9;
Return 0; myarray[4] 9

}
int main()
{
int myarray[ 10 ] = { 2, 7, 4, 8, 5, 4, 9, 7, 6, 3 };
for ( int i = 0; i < 5; i++ ) 2
cout<<myarray[ i ]<<endl; 7
return 0; 4
} 8
5
4
9
7
6
3
for ( int i = 0; i < 10; i++ )
cout<<myarray[ i ] ;

int main()
{
int myarray[ 5 ];
0
for ( int i = 0; i < 5; i++ ) 0
myarray[ i ] = 0; 0
for ( int j = 0; j < 5; j++ ) 0
cout << myarray[ j ] << endl; 0

return 0;
}
Create an array that consists of 5 names
#include <iostream>
#include<string>

using namespace std;

int main()
{
string name[5]; Great tutorial :
int i;
http://www.youtube.com/wa
for(i=0;i<5;i++) tch?v=XEzM77ICfBU
{
cout<<"Enter name : ";
cin>>name[i];
}
return 0;
}
Example of the table 5 4 5 5
3 rows and 4 columns 3 7 4 7
How to declare 2D arrays ?1 6 8 4

#include <iostream>
using namespace std;

int main()
{
int table[3][4];
return 0;
}
5 4 5 5
3 7 4 7
#include <iostream> 1 6 8 4
using namespace std;

int main()
{
int table[3][4] = {{5,4,5,5},{3,7,4,7},{1,6,8,4}};
return 0;
}
Use nested loop (outer loop and inner loop)
Outer loop for rows
Inner loop for columns
5 4 5 5
3 7 4 7
for (i= 0 ; i < 3 ; i++) //outer loop 1 6 8 4
{
for (j = 0; j < 4 ; j++) //inner loop
{
cout << table[i][j];
}
}
We can also use nested loop to enter values
in the 2 D arrays

int table[3][4];
Great tutorial:
for (i= 0 ; i < 3 ; i++) //outer loop
http://www.youtube.co
{ m/watch?v=RwDttcH
for (j = 0; j < 4 ; j++) //inner loop RYHA&feature=relate
{ d
cin>>table[i][j];
}
}
A pointer is the memory address of a variable
Pointers tell where to find a variable
Pointers "point" to a variable by telling where the
variable is located
How to declare a pointer ?
int *p ;
(a pointer, p points to the address where the int type value is stored)
How to assign a value?
p = &v ;
(a pointer, p points to the address of v. What is the value of v ?)
value
name
Memory management
address
system:
When we declare a variable,
Example:
normally 3 spaces are allocated:
int v;
1) The address of that variable
automatically given/generated by value
the computer name v
2) The name of the variable - address F001111E
depends on the given name by us
3) The value of the variable depends
v = 10;
on the value that we want to give.
MUST BE THE SAME TYPE WITH OUR
value 10
DECLARATION.
name v
address F001111E
value
int *p ;
name p
address F005555E

value 10 cout<< v ;
Meanwhile.
int v; name v
v= 10 address F001111E

value F001111E
p = &v; name p
address F005555E

cout<< p ;
cout<<*p ;
#include<iostream.h>
using namespace std;
int main() 10
{
int v; v
int *p; F005555E
v = 10;
cout<<v<<endl;
cout<<&v<<endl;
p = &v;
cout<<p<<endl; What is the output ?
cout<<*p<<endl;
*p = 8;
cout<<v<<endl;
cout<<p<<endl;
cout<<*p<<endl;
return 0;
}
Declaring a pointer:
Eg:
int *p ;
double *p ;
float *p ;

Assigning a value: We can also assign a value like this:


Eg:
p = &v ; int *p ;
p = new int;
*p = 45;
#include<iostream.h>
using namespace std;
p1 p2
int main()
{ F005555E F007777E
int *p1, *p2;
p1 = new int;
p2 = p1;
*p1 = 42;
cout<<*p1<<" "<<*p2<<endl;
*p2 = 53;
cout<<*p1<<" "<<*p2<<endl;
p2 = new int;
*p2 = 45;
cout<<p1<<*p1<<p2<<*p2<<endl;
*p1 = *p2;
cout<<p1<<*p1<<p2<<*p2<<endl;
p1 = p2;
cout<<p1<<*p1<<p2<<*p2<<endl;
return 0;
}
Static Memory Allocation
Memory is allocated at compilation time
Dynamic Memory
Memory is allocated at running time
Static object Dynamic object
(variables as declared in function calls)
Memory is acquired by
Memory is acquired
program with an allocation
automatically
request
new operation

Memory is returned
Dynamic objects can exist
automatically when object
beyond the function in
goes out of scope
which they were allocated

Object memory is returned


by a deallocation request
delete operation
Memory Allocation

new
delete
{ int* ptr;
int a[200]; ptr = new int[200];

} delete [] ptr;
Syntax
ptr = new SomeType;

where ptr is a pointer of type SomeType

Example
int* p = new int;

Uninitialized int variable

p
Syntax
delete p;
storage pointed to by p is returned to free store and p is now
undefined

Example
int* p = new int;
*p = 10;
delete p;

p 10
Syntax
P = new SomeType[Expression];
Where
P is a pointer of type SomeType
Expression is the number of objects to be
constructed -- we are making an array

Because of the flexible pointer syntax, P


can be considered to be an array
Example
Dynamic Memory Allocation
Request for unnamed memory from the Operating System

int *p, n=10; new


p = new int; p

new
p = new int[100];
p
new
p = new int[n]; p
Memory Allocation Example
Want an array of unknown size
#include <iostream>
using namespace std;
void main()
{
int n;
cout << How many students? ;
cin >> n;
int *grades = new int[n];
for(int i=0; i < n; i++){
int mark;
cout << Input Grade for Student << (i+1) << ? :;
cin >> mark;
grades[i] = mark;
}
...
printMean( grades, n ); // call a function with dynamic array
...
}
Freeing (or deleting) Memory
cout << "Enter list size: ";
int n;
cin >> n;
int *A = new int[n];
if(n<=0){
cout << "bad size" << endl;
return 0;
}
initialize(A, n, 0); // initialize the array A with value 0
print(A, n);
A = addElement(A,n,5); //add an element of value 5 at the end of A
print(A, n);
A = deleteFirst(A,n); // delete the first element from A
print(A, n);
selectionSort(A, n); // sort the array (not shown)
print(A, n);
delete [] A;
void initialize(int list[], int size, int value){
for(int i=0; i<size; i++)
list[i] = value;

}
void print(int list[], int size) {
cout << "[ ";
for(int i=0; i<size; i++)
cout << list[i] << " ";
cout << "]" << endl;
}
// for adding a new element to end of array
int* addElement(int list[], int& size, int value){
int* newList = new int [size+1]; // make new array
if(newList==0){
cout << "Memory allocation error for addElement!" << endl;
exit(-1);
}
for(int i=0; i<size; i++)
newList[i] = list[i];
if(size) delete [] list;
newList[size] = value;
size++;
return newList;
}
// for deleting the first element of the array
int* deleteFirst(int list[], int& size){
if(size <= 1){
if( size) delete list;
size = 0;
return NULL;
}
int* newList = new int [size-1]; // make new array
if(newList==0){
cout << "Memory allocation error for deleteFirst!" << endl;
exit(-1);
}
for(int i=0; i<size-1; i++) // copy and delete old array
newList[i] = list[i+1];
delete [] list;
size--;
return newList;
}
void addElement( int * & list, int & size, const int value ){

int * newList = new int [size + 1];

if( newList == NULL )


{
cout << "Memory allocation error!" << endl;
exit(-1);
}
for( int i = 0; i < size; i++ )
newList[i] = list[ i ];

if(size) delete [] list;

newList[size] = value;
size++;
list = newList;
return;
}
void deleteFirst( int * & list, int & size ){

if( size <= 1 ){


if( size )
delete list;
list = NULL;
size = 0;
return;
}

delete list; // delete the first element


list++;
size--;
return;
}
int main()
{
int * A = NULL;
int size = 0;
int i;

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


addElement( A, size, i );

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


cout << A[i] << " ";
cout << endl;

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


deleteFirst( A, size );

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


cout << A[i] << " ";
cout << endl; 0123456789
return 0; 456789
}
Dangling Pointer Problem
int *A = new int[5];
for(int i=0; i<5; i++)
A[i] = i;
int *B = A;
A
0 1 2 3 4
B

delete [] A;
Locations do not belong to program
B[0] = 1; // illegal!
A

?
B
Memory Leak Problem
int *A = new int [5];
for(int i=0; i<5; i++)
A[i] = i;
A 0 1 22 3 4

These locations cannot be


A = new int [5]; accessed by program

A 0 1 2 3 4


A Dynamic 2D Array
A dynamic array is table
an array of pointers 32 18 12 24
to save space when table[0]
not all rows of the table[1] 13 11 16 12 42 19 14
array are full. table[2]
22
table[3]
int **table; table[4] 13 13 14
table[5]
11 18
table = new int*[6];

table[0] = new int[4];
table[1] = new int[7];
table[2] = new int[1];
table[3] = new int[3];
table[4] = new int[2];
table[5] = NULL;
int **table;

table = new int*[6];

table[0]= new int[3];


table[1]= new int[1];
table[2]= new int[5];
table[3]= new int[10];
table[4]= new int[2];
table[5]= new int[6];

table[0][0] = 1; table[0][1] = 2; table[0][2] = 3;


table[1][0] = 4;
table[2][0] = 5; table[2][1] = 6; table[2][2] = 7; table[2][3]
= 8; table[2][4] = 9;

table[4][0] = 10; table[4][1] = 11;


cout << table[2][5] << endl;
Memory leak is a serious bug!
Each row must be deleted individually
Be careful to delete each row before deleting
the table pointer.
for(int i=0; i<6; i++)
delete [ ] table[i];
delete [ ] table;
Simple data types
Eg:
integer
Float
double
char
Have more than one data types = struct
Eg:
struct Student
{
The name of the data type:
char id[4];
char name[20]; Student
int age; This struct combines 3
};
simple data types (char [ ],
char [ ] and int = 3
members)
struct Student
{
char id[4];
What is the structs name ?
char name[20]; How many elements/members
int age; inside Student data type ?
}; How to declare objects of the
Student?

Student student1 ;
Student student2 ;
Lets compare:

int age ;
Student student1 ;
Memory management
Simple data type age
int age ; F005555E
Struct data type
struct Student
{
char id[4];
char name[20];
id name age
int age;
}; student1
Student student1 ; F008888E
If simple data type:
int age ;
19
age = 19 ; age
F005555E
Struct object?
struct Student
{
char id[4];
char name[20];
int age;
}; D201010 Ida 19
Student student1 ; 222
id name age
strcpy(student1.id, D201010222);
strcpy(student1.name,Ida);
student1.age = 19; student1
F008888E
#include<iostream.h>
using namespace std;
struct Student {
char id[14];
char name[20];
int age; Great tutorial:
};
http://www.youtube.com/watch?v=0
int main() k23xe3c4es&feature=mfu_in_order
{ &list=UL
int age;
Student student1;
age = 19;
strcpy(student1.id,"D201010100");
strcpy(student1.name,"ida");
student1.age = 19;
cout<<age<<endl;
cout<<student1.id<<student1.name<<student1.age<<endl;
return 0;
}

You might also like