2 BytesC++ course_2014_c4_ arrays

48
Kinan keshkeh IT Engineering-Damascus University 3 rd year Summer course- 2014 2 bytes team

Transcript of 2 BytesC++ course_2014_c4_ arrays

Page 1: 2 BytesC++ course_2014_c4_ arrays

Kinan keshkeh

IT Engineering-Damascus University

3rd year

Summer course- 2014

2 bytes team

Page 2: 2 BytesC++ course_2014_c4_ arrays

• How are you today ?

• Feel good with Functions ?

• Any Ques ?

Page 3: 2 BytesC++ course_2014_c4_ arrays

• Do you want to become a

real programmer ?!!

Page 4: 2 BytesC++ course_2014_c4_ arrays

Smashing keyboards !!

Page 5: 2 BytesC++ course_2014_c4_ arrays

Arrays !

Page 6: 2 BytesC++ course_2014_c4_ arrays

Arrays

• Type_Name Array_Name [Declared_Size] ;

Page 7: 2 BytesC++ course_2014_c4_ arrays

Arrays

• Type_Name Array_Name [Declared_Size] ;

The size of Array

Page 8: 2 BytesC++ course_2014_c4_ arrays

Arrays

• Type_Name Array_Name [Declared_Size] ;

The size of Array

But the index : 0 size -1

Page 9: 2 BytesC++ course_2014_c4_ arrays

Arrays

• Type_Name Array_Name [Declared_Size] ;

• int score[5];

• int next, score[5], max ;

Page 10: 2 BytesC++ course_2014_c4_ arrays

Arrays #include <iostream> using namespace std; int main( ) { const int NUMBER_OF_STUDENTS=5; int i, score[NUMBER_OF_STUDENTS],max; cout << "Enter "<< NUMBER_OF_STUDENTS<<" scores:\n"; cin >> score[0]; max = score[0]; for (i = 1; i < NUMBER_OF_STUDENTS ; i++){ cin >> score[i]; If (score[i] > max) max = score[i]; } //max is the largest of the values score[0],..., score[i]. cout << "The highest score is " << max << endl << "The scores and their\n" << "differences from the highest are:\n"; for (i = 0; i < NUMBER_OF_STUDENTS; i++) cout << score[i] << " off by " << (max - score[i]) << endl; return 0; }

Enter 5 scores: 5 9 2 10 6 The highest score is 10 The scores and their differences from the highest are: 5 off by 5 9 off by 1 2 off by 8 10 off by 0 6 off by 4

Page 11: 2 BytesC++ course_2014_c4_ arrays

• Array in Memory

• If you pass the domain 0 size -1 , you will not get any error message !

• In print especially : you will have random values .

• In cin (inputting values) : you will change other variables values.

Arrays in memory

Arrays

Page 12: 2 BytesC++ course_2014_c4_ arrays

Arrays • Array in Memory

• If you pass the domain 0 size -1 , you will not get any error message !

Page 13: 2 BytesC++ course_2014_c4_ arrays

• Initializing Arrays

• Initialized with random values in memory .

• int score[5]={ 1, 20 ,100 };

• int score[5];

• int score[5]={ 1, 20 ,100 , 5 , 3 };

• score[3] == 0 ; score[4] == 0 ;

• int score[] ={ 1, 20 ,100 , 5 , 3 };

Initializing Arrays

Arrays

Page 14: 2 BytesC++ course_2014_c4_ arrays

Arrays

• Initializing Arrays

• Initialized with random values in memory .

• int score[5]={ 1, 20 ,100 };

• int score[5];

• int score[5]={ 1, 20 ,100 , 5 , 3 };

• score[3] == 0 ; score[4] == 0 ;

• int score[] ={ 1, 20 ,100 , 5 , 3 };

Page 15: 2 BytesC++ course_2014_c4_ arrays

Arrays

• Initializing Arrays

• Initialized with random values in memory .

• int score[5]={ 1, 20 ,100 };

• int score[5];

• int score[5]={ 1, 20 ,100 , 5 , 3 };

• score[3] == 0 ; score[4] == 0;

• int score[] ={ 1, 20 ,100 , 5 , 3 };

• int score[] ={ 1, 20 };

Page 16: 2 BytesC++ course_2014_c4_ arrays

Arrays

• Initializing Arrays

• Initialized with random values in memory .

• int score[5]={ 1, 20 ,100 };

• int score[5];

• int score[5]={ 1, 20 ,100 , 5 , 3 };

• score[3] == 0 ; score[4] == 0 ;

• int score[] ={ 1, 20 ,100 , 5 , 3 };

• int score[] ={ 1, 20 };

Page 17: 2 BytesC++ course_2014_c4_ arrays

• Array in Functions

• It’s like calling variables by reference.

Array in Functions

Arrays

Page 18: 2 BytesC++ course_2014_c4_ arrays

Arrays #include <iostream> using namespace std; void fillUp(int a[], int size); void asSort(int a[], int size);

void print(int a[], int size); int main(){ const int numberOfScores = 5; int score[numberOfScores]; fillUp(score,numberOfScores); asSort(score,numberOfScores); cout <<"the ascendent array is: "; print(score,numberOfScores); return 0; } void fillUp(int a[], int size){ cout << "Enter " << size << " numbers:\n"; for (int i = 0; i < size; i++) cin >> a[i]; }

void asSort(int a[], int size){ for (int i = 0; i < size-1; i++) for (int j = i+1; j < size; j++) if (a[i]>a[j]){ int temp=a[i]; a[i]=a[j]; a[j]=temp; } } void print(int a[], int size){ for (int i = 0; i < size-1; i++) cout<<a[i]<<", "; cout<<a[size-1]<<endl; }

Page 19: 2 BytesC++ course_2014_c4_ arrays

Arrays #include <iostream> using namespace std; void fillUp(int a[], int size); void asSort(int a[], int size);

void print(int a[], int size); int main(){ const int numberOfScores = 5; int score[numberOfScores]; fillUp(score,numberOfScores); asSort(score,numberOfScores); cout <<"the ascendent array is: "; print(score,numberOfScores); return 0; } void fillUp(int a[], int size){ cout << "Enter " << size << " numbers:\n"; for (int i = 0; i < size; i++) cin >> a[i]; }

void asSort(int a[], int size){ for (int i = 0; i < size-1; i++) for (int j = i+1; j < size; j++) if (a[i]>a[j]){ int temp=a[i]; a[i]=a[j]; a[j]=temp; } } void print(int a[], int size){ for (int i = 0; i < size-1; i++) cout<<a[i]<<", "; cout<<a[size-1]<<endl; }

Like calling by reference

Page 20: 2 BytesC++ course_2014_c4_ arrays

Arrays #include <iostream> using namespace std; void fillUp(int a[], int size); void asSort(int a[], int size);

void print(int a[], int size); int main(){ const int numberOfScores = 5; int score[numberOfScores]; fillUp(score,numberOfScores); asSort(score,numberOfScores); cout <<"the ascendent array is: "; print(score,numberOfScores); return 0; } void fillUp(int a[], int size){ cout << "Enter " << size << " numbers:\n"; for (int i = 0; i < size; i++) cin >> a[i]; }

void asSort(int a[], int size){ for (int i = 0; i < size-1; i++) for (int j = i+1; j < size; j++) if (a[i]>a[j]){ int temp=a[i]; a[i]=a[j]; a[j]=temp; } } void print(int a[], int size){ for (int i = 0; i < size-1; i++) cout<<a[i]<<", "; cout<<a[size-1]<<endl; }

So , The array values ,are stored in array after calling !

Page 21: 2 BytesC++ course_2014_c4_ arrays

Arrays #include <iostream> using namespace std; void fillUp(int a[], int size); void asSort(int a[], int size);

void print(int a[], int size); int main(){ const int numberOfScores = 5; int score[numberOfScores]; fillUp(score,numberOfScores); asSort(score,numberOfScores); cout <<"the ascendent array is: "; print(score,numberOfScores); return 0; } void fillUp(int a[], int size){ cout << "Enter " << size << " numbers:\n"; for (int i = 0; i < size; i++) cin >> a[i]; }

void asSort(int a[], int size){ for (int i = 0; i < size-1; i++) for (int j = i+1; j < size; j++) if (a[i]>a[j]){ int temp=a[i]; a[i]=a[j]; a[j]=temp; } } void print(int a[], int size){ for (int i = 0; i < size-1; i++) cout<<a[i]<<", "; cout<<a[size-1]<<endl; }

The function knows the array position in memory and its type.

Page 22: 2 BytesC++ course_2014_c4_ arrays

Arrays #include <iostream> using namespace std; void fillUp(int a[], int size); void asSort(int a[], int size);

void print(int a[], int size); int main(){ const int numberOfScores = 5; int score[numberOfScores]; fillUp(score,numberOfScores); asSort(score,numberOfScores); cout <<"the ascendent array is: "; print(score,numberOfScores); return 0; } void fillUp(int a[], int size){ cout << "Enter " << size << " numbers:\n"; for (int i = 0; i < size; i++) cin >> a[i]; }

void asSort(int a[], int size){ for (int i = 0; i < size-1; i++) for (int j = i+1; j < size; j++) if (a[i]>a[j]){ int temp=a[i]; a[i]=a[j]; a[j]=temp; } } void print(int a[], int size){ for (int i = 0; i < size-1; i++) cout<<a[i]<<", "; cout<<a[size-1]<<endl; }

So , you don’t have to write its size !!

Page 23: 2 BytesC++ course_2014_c4_ arrays

Arrays #include <iostream> using namespace std; void fillUp(int a[], int size); void asSort(int a[], int size);

void print(int a[], int size); int main(){ const int numberOfScores = 5; int score[numberOfScores]; fillUp(score,numberOfScores); asSort(score,numberOfScores); cout <<"the ascendent array is: "; print(score,numberOfScores); return 0; } void fillUp(int a[], int size){ cout << "Enter " << size << " numbers:\n"; for (int i = 0; i < size; i++) cin >> a[i]; }

void asSort(int a[], int size){ for (int i = 0; i < size-1; i++) for (int j = i+1; j < size; j++) if (a[i]>a[j]){ int temp=a[i]; a[i]=a[j]; a[j]=temp; } } void print(int a[], int size){ for (int i = 0; i < size-1; i++) cout<<a[i]<<", "; cout<<a[size-1]<<endl; }

Put in calling the array name !

Page 24: 2 BytesC++ course_2014_c4_ arrays

Arrays #include <iostream> using namespace std; void fillUp(int a[], int size); void asSort(int a[], int size);

void print(int a[], int size); int main(){ const int numberOfScores = 5; int score[numberOfScores]; fillUp(score,numberOfScores); asSort(score,numberOfScores); cout <<"the ascendent array is: "; print(score,numberOfScores); return 0; } void fillUp(int a[], int size){ cout << "Enter " << size << " numbers:\n"; for (int i = 0; i < size; i++) cin >> a[i]; }

void asSort(int a[], int size){ for (int i = 0; i < size-1; i++) for (int j = i+1; j < size; j++) if (a[i]>a[j]){ int temp=a[i]; a[i]=a[j]; a[j]=temp; } } void print(int a[], int size){ for (int i = 0; i < size-1; i++) cout<<a[i]<<", "; cout<<a[size-1]<<endl; }

Enter 5 numbers: 1 4 2 3 1 the ascendent sorted array is: 1, 1, 2, 3, 4

Page 25: 2 BytesC++ course_2014_c4_ arrays

Arrays

• Array in Functions

• It’s like calling variables by reference.

• Constant Array Parameter:

• void print(const int a[], int size)

Page 26: 2 BytesC++ course_2014_c4_ arrays

Arrays

• Array in Functions

• It’s like calling variables by reference.

• Constant Array Parameter:

• void print(const int a[], int size)

to prevent changing in its values !

Page 27: 2 BytesC++ course_2014_c4_ arrays

Arrays #include <iostream> using namespace std; void fillUp(int a[], int size); void asSort(int a[], int size);

void print(int a[], int size); int main(){ const int numberOfScores = 5; int score[numberOfScores]; fillUp(score,numberOfScores); asSort(score,numberOfScores); cout <<"the ascendent array is: "; print(score,numberOfScores); return 0; } void fillUp(int a[], int size){ cout << "Enter " << size << " numbers:\n"; for (int i = 0; i < size; i++) cin >> a[i]; }

void asSort(int a[], int size){ for (int i = 0; i < size-1; i++) for (int j = i+1; j < size; j++) if (a[i]>a[j]){ int temp=a[i]; a[i]=a[j]; a[j]=temp; } } void print(int a[], int size){ for (int i = 0; i < size-1; i++) cout<<a[i]<<", "; cout<<a[size-1]<<endl; }

Page 28: 2 BytesC++ course_2014_c4_ arrays

Arrays #include <iostream> using namespace std; void fillUp(int a[], int size); void asSort(int a[], int size);

void print(int a[], int size); int main(){ const int numberOfScores = 5; int score[numberOfScores]; fillUp(score,numberOfScores); asSort(score,numberOfScores); cout <<"the ascendent array is: "; print(score,numberOfScores); return 0; } void fillUp(int a[], int size){ cout << "Enter " << size << " numbers:\n"; for (int i = 0; i < size; i++) cin >> a[i]; }

void asSort(int a[], int size){ for (int i = 0; i < size-1; i++) for (int j = i+1; j < size; j++) if (a[i]>a[j]){ int temp=a[i]; a[i]=a[j]; a[j]=temp; } }

void print( const int a[], int size){

for (int i = 0; i < size-1; i++) cout<<a[i]<<", "; cout<<a[size-1]<<endl; }

Page 29: 2 BytesC++ course_2014_c4_ arrays

Arrays #include <iostream> using namespace std; void fillUp(int a[], int size); void asSort(int a[], int size);

void print(int a[], int size); int main(){ const int numberOfScores = 5; int score[numberOfScores]; fillUp(score,numberOfScores); asSort(score,numberOfScores); cout <<"the ascendent array is: "; print(score,numberOfScores); return 0; } void fillUp(int a[], int size){ cout << "Enter " << size << " numbers:\n"; for (int i = 0; i < size; i++) cin >> a[i]; }

void asSort(int a[], int size){ for (int i = 0; i < size-1; i++) for (int j = i+1; j < size; j++) if (a[i]>a[j]){ int temp=a[i]; a[i]=a[j]; a[j]=temp; } }

void print( const int a[], int size){

for (int i = 0; i < size-1; i++) cout<<a[i]<<", "; cout<<a[size-1]<<endl; }

Page 30: 2 BytesC++ course_2014_c4_ arrays

Arrays

• Array in Functions

• It’s like calling variables by reference.

• Constant Array Parameter:

• void print(const int a[], int size)

• Functions can’t returns Arrays !!

• Int a[] add(int a [] , int b[] , int size )

Page 31: 2 BytesC++ course_2014_c4_ arrays

Arrays

• Array in Functions

• It’s like calling variables by reference.

• Constant Array Parameter:

• void print(const int a[], int size)

False !!

• Functions can’t returns Arrays !!

• Int a[] add(int a [] , int b[] , int size )

Page 32: 2 BytesC++ course_2014_c4_ arrays

Arrays

• Array in Functions

• It’s like calling variables by reference.

• Constant Array Parameter:

• void print(const int a[], int size)

In pointers … True

• Functions can’t returns Arrays !!

• Int * add(int a [] , int b[] , int size )

Page 33: 2 BytesC++ course_2014_c4_ arrays

Arrays

• Like in Pascal , you will reserve a Maximum size (cause you are unable to know how many numbers for.ex the user want to input .

• So , determine max size , then save the real size in variable .

• void fillUp(int b[], int size, int maxSize)

Page 34: 2 BytesC++ course_2014_c4_ arrays

• Multidimensional Arrays

• Type Array_Name[Size_Dim_1][Size_Dim_2]...[Size_Dim_Last];

Multidimensional Arrays

Arrays

Page 35: 2 BytesC++ course_2014_c4_ arrays

Arrays

• Multidimensional Arrays

• One dim array : const int size=6 ; int arr[size];

• Two dim array : const int rows=3 ; const int columns=5; int arr[rows][columns];

• Dimension:

Page 36: 2 BytesC++ course_2014_c4_ arrays

Arrays

• Multidimensional Arrays

• One dim array : const int size=6 ; int arr[size];

• Two dim array : const int rows=3 ; const int columns=5; int arr[rows][columns];

• Initialize :

• Dimension:

• One dim array : int arr[size]= {20 , 10 , 5 , 6 };

• Two dim array : int arr[rows][columns]={ {20 , 10 , 5 , 6 } , {4, 7, 1, 2} , {50, 10 , 4, 6 } };

Page 37: 2 BytesC++ course_2014_c4_ arrays

Arrays

• Multidimensional Arrays

• One dim array : const int size=6 ; int arr[size];

• Two dim array : const int rows=3 ; const int columns=5; int arr[rows][columns];

• Initialize :

• Dimension:

• One dim array : int arr[]= {20 , 10 , 5 , 6 };

• Two dim array : int arr[rows][columns]={ {20 , 10 , 5 , 6 } , {4, 7, 1, 2} , {50, 10 , 4, 6 } };

Like this one dim It could be []

Page 38: 2 BytesC++ course_2014_c4_ arrays

Arrays

• Multidimensional Arrays

• One dim array : const int size=6 ; int arr[size];

• Two dim array : const int rows=3 ; const int columns=5; int arr[rows][columns];

• Initialize :

• Dimention:

• One dim array : int arr[]= {20 , 10 , 5 , 6 };

• Two dim array : int arr[][columns]={ {20 , 10 , 5 , 6 } , {4, 7, 1, 2} , {50, 10 , 4, 6 } };

This in two dim it could be []

Page 39: 2 BytesC++ course_2014_c4_ arrays

Arrays

• Multidimensional Arrays

• In functions: • One dim array : void getPage(char p[] , int sizeDimension1);

• Two dim array : void getPage(char p[][100] , int sizeDimension1);

Page 40: 2 BytesC++ course_2014_c4_ arrays

Arrays

• Multidimensional Arrays

• In functions: • One dim array : void getPage(char p[] , int sizeDimension1);

• Two dim array : void getPage(char p[][100] , int sizeDimension1);

function must knows the other dimensions !

Page 41: 2 BytesC++ course_2014_c4_ arrays

Arrays

• Multidimensional Arrays

• Inputting by user: • One dim array : int arr[size]; for ( int i=0 ; i<size ; i++ ) cin>>arr[i];

• Two dim array : int arr[rows][columns]; for ( int i=0 ; i<rows ; i++ ) for ( int j=0 ; i<columns ; j++ ) cin>>arr[i][j]

Page 42: 2 BytesC++ course_2014_c4_ arrays

Arrays

• Multidimensional Arrays

• One dim array : const int size=6 ; int arr[size];

• Two dim array : const int rows=3 ; const int columns=5; int arr[rows][columns];

• Dimention:

• Three dim array: double _3dPicture[10][20][30];

Page 43: 2 BytesC++ course_2014_c4_ arrays

Homework

Page 44: 2 BytesC++ course_2014_c4_ arrays

Homework

Page 45: 2 BytesC++ course_2014_c4_ arrays

Homework

• Write a program that calls a function calculates the Average of students marks and print it .

• And another function that finds the max mark of them .

Page 46: 2 BytesC++ course_2014_c4_ arrays

That’s for today

That’s for today guys !!

Page 47: 2 BytesC++ course_2014_c4_ arrays

That’s for today

Go and eat !

Page 48: 2 BytesC++ course_2014_c4_ arrays

2 bytes team

Group : group link

Mobile phone- Kinan : 0994385748

Facebook account : kinan’s account

2 bytes team