10. 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 theirn" << "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
11. •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
12. Arrays
•Array in Memory
•If you pass the domain 0 size -1 , you will not get any error message !
13. •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
14. 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 };
15. 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 };
16. 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 };
17. •Array in Functions
•It’s like calling variables by reference.
Array in Functions
Arrays
18. 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; }
19. 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
20. 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 !
21. 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.
22. 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 !!
23. 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 !
24. 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
25. Arrays
•Array in Functions
•It’s like calling variables by reference.
•Constant Array Parameter:
•void print(const int a[], int size)
26. 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 !
27. 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; }
28. 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;
}
29. 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; }
30. 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 )
31. 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 )
32. 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 )
33. 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)
35. 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:
36. 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 } };
37. 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 []
38. 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 []
39. Arrays
•Multidimensional Arrays
•In functions:
•One dim array : void getPage(char p[] , int sizeDimension1);
•Two dim array : void getPage(char p[][100] , int sizeDimension1);
40. 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 !
41. 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]
42. 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];
45. 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 .