2 BytesC++ course_2014_c4_ arrays
Transcript of 2 BytesC++ course_2014_c4_ arrays
Kinan keshkeh
IT Engineering-Damascus University
3rd year
Summer course- 2014
2 bytes team
• How are you today ?
• Feel good with Functions ?
• Any Ques ?
• Do you want to become a
real programmer ?!!
Smashing keyboards !!
Arrays !
Arrays
• Type_Name Array_Name [Declared_Size] ;
Arrays
• Type_Name Array_Name [Declared_Size] ;
The size of Array
Arrays
• Type_Name Array_Name [Declared_Size] ;
The size of Array
But the index : 0 size -1
Arrays
• Type_Name Array_Name [Declared_Size] ;
• int score[5];
• int next, score[5], max ;
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
• 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
Arrays • Array in Memory
• If you pass the domain 0 size -1 , you will not get any error message !
• 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
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 };
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 };
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 };
• Array in Functions
• It’s like calling variables by reference.
Array in Functions
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; }
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
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 !
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.
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 !!
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 !
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
Arrays
• Array in Functions
• It’s like calling variables by reference.
• Constant Array Parameter:
• void print(const int a[], int size)
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 !
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; }
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; }
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; }
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 )
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 )
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 )
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)
• Multidimensional Arrays
• Type Array_Name[Size_Dim_1][Size_Dim_2]...[Size_Dim_Last];
Multidimensional Arrays
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:
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 } };
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 []
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 []
Arrays
• Multidimensional Arrays
• In functions: • One dim array : void getPage(char p[] , int sizeDimension1);
• Two dim array : void getPage(char p[][100] , int sizeDimension1);
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 !
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]
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];
Homework
Homework
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 .
That’s for today
That’s for today guys !!
That’s for today
Go and eat !
2 bytes team
Group : group link
Mobile phone- Kinan : 0994385748
Facebook account : kinan’s account
2 bytes team