Professional Documents
Culture Documents
}
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>
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 ;
Memory is returned
Dynamic objects can exist
automatically when object
beyond the function in
goes out of scope
which they were allocated
new
delete
{ int* ptr;
int a[200]; ptr = new int[200];
} delete [] ptr;
Syntax
ptr = new SomeType;
Example
int* p = new int;
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
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 ){
newList[size] = value;
size++;
list = newList;
return;
}
void deleteFirst( int * & list, int & size ){
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
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;
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;
}