c++ chapter iii
TRANSCRIPT
Lecturer: Hong Mom 1
Lecturer: Hong Mom
Chapter III
Objec8ve
Lecturer: Hong Mom 2
A<er completely this chapter, you will be able to: • Declare , ini8alize, and use arrays • Insert value to elements of array • Display value of elements of array • Sort array
Arrays
Defini8on
An array is a group of variables of the same data type. Why using array?
Ex: We can store 10 values of the type int in an array without having to declare 10 different variables.
Lecturer: Hong Mom 3
Array Representa8on How the array represented?
Ex: an array to contain 5 integer values of type int called name could be represented like this: Array declara8on
data_type name[size];
Lecturer: Hong Mom 4
1 0 2 3 4 name
int
Array Assignment Ex: int arr[5]; //defines array of 5 elements as integer called arr Assigning values to array
int arr[3]= {4,8,9}; Note: The elements between the braces{} must not be larger than the size.
char leaer[4] = { ‘a’, ’z’, ‘j’, ‘k’ }; int a[2]; a[0] = 5; a[1] = 11;
Lecturer: Hong Mom 5
Array-‐Insert value by cin
• Insert value by cin int a[10]; for(int i= 0;i<10;i++){ cin >> a[i]; cout << endl; }
Lecturer: Hong Mom 6
// testArray.cpp // Ini8alizing an array with a declara8on #include <iostream.h> #include <iomanip.h> int main(){
int n[10] = { 20, 32, 27, 98, 13, 40, 37, 23, 70, 73}; cout << “Element” << setw(13) << “Value” << endl; //output contents of arrays n in tabular format for ( int i= 0 ; i<10; i++) cout << setw(7) << I <<setw(13) << n[i] << endl; return 0;
}
Array-‐Example
Lecturer: Hong Mom 7
Array-‐Example
Output:
Lecturer: Hong Mom 8
Element Value
0 20
1 32
2 27
3 98
4 13
5 40
6 37
7 23
8 70
9 73
Input and Output of Array Example
#include <iostream> using namespace std; int main() {
const int NUMELS = 5; int i, grade[NUMELS]; for (i = 0; i < NUMELS; i++) // Enter the grades { cout << "Enter a grade: "; cin >> grade[i]; } cout << endl; for (i = 0; i < NUMELS; i++) // Print the grades cout << "grade [" << i << "] is " << grade[i] << endl; return 0;
}
Lecturer: Hong Mom 9
Input and Output of Array Example
Output Enter a grade: 85 Enter a grade: 90 Enter a grade: 78 Enter a grade: 75 Enter a grade: 92
grade[0] is 85 grade[1] is 90 grade[2] is 78 grade[3] is 75 grade[4] is 92
Lecturer: Hong Mom 10
Array of char
• Strings -‐ arrays of characters -‐ all strings end with null (‘\0’) Example:
char str[ ] = “hello”; // null character implicitly added char str[ ] = {‘h’, ‘e’ , ‘l’ , ‘l’, ‘o’, ‘\0’ };
-‐ Subscrip8ng str[0] = ‘h’ str[2] = ‘l’
Lecturer: Hong Mom 11
Array of char-‐ Example
Lecturer: Hong Mom 12
#include <iostream.h> void main(){
char str1[20]; char str2[ ] = “string literal”; //read string from user cout << “Enter the string “hello there”:“; cin >> str1; //reads “hello” [space terminates input] //out strings cout << “str1 is : “ << str1; cout << “str2 is : “ << str2; cout << “\nstr1 with spaces between characters is : \n”; //output characters un8l null character is reached for ( int i = 0; str1[i] != ‘\0’; i++ ) cout << str1[i] <<‘ ‘; cin >> str1; //reads “there” cout << “\nstr1 is: “ << str1 << endl;
} }
Array of char-‐ Example
Enter the string “hello there”: hello there str1 is : hello str2 is : string literal str1 with spaces between characters is: h e l l o str1 is: there
Lecturer: Hong Mom 13
A two-‐dimensional array
• Defini8on A two-‐dimensional array: consists of both rows and columns of elements Example: two-‐dimensional array of integer
Lecturer: Hong Mom 14
8 16 9 52
3 15 27 6
14 25 2 10
[0] [1] [2] [3]
[0] 8 16 9 52
[1] 3 15 27 6
[2] 14 25 2 10
int a[3][4]; //3 rows and 4 columns
A two-‐dimensional array
Lecturer: Hong Mom 15
a[1][3]
Row posi8on
Column posi8on
A two-‐dimension array
• Ini8alizing a two-‐ dimensional array A two-‐dimensional array, like a one-‐dimensional array, is ini8alized by enclosing by ini8al values in braces. Example: int val[4][3] = {{ 4, 9, 5}, {2, 11,3}, {21, 9, 32}, {10, 1, 5} };
Lecturer: Hong Mom 16
Sor8ng array
int arr[10]={4,8,2,7,11,3,20,68,45,25}; for(int i=0;i<9;i++) for(int j = i+1;j<10;j++) if(a[i]>a[j]){ int temp = a[i]; a[i] = a[j]; a[j] = temp; }
Lecturer: Hong Mom 17
Sor8ng array
Before sor8ng 4 8 2 7 11 3 20 68 45 25 A<er sor8ng 2 3 4 7 8 11 20 25 45 68
Lecturer: Hong Mom 18