Data type List Definition The data type List




![Data Initialization EX 1: array L [5] of type integer L {2, 4, 7, Data Initialization EX 1: array L [5] of type integer L {2, 4, 7,](https://slidetodoc.com/presentation_image_h2/77d793ae235a867b7dbcc1e510b015e0/image-5.jpg)
![Processing order array word [6] of type character word {‘A’, ’t’, ’e’, ’n’, ’d’} Processing order array word [6] of type character word {‘A’, ’t’, ’e’, ’n’, ’d’}](https://slidetodoc.com/presentation_image_h2/77d793ae235a867b7dbcc1e510b015e0/image-6.jpg)

![Example: Write. Array Algorithm Print-characters Begin array Word [6] of type character //Data definition: Example: Write. Array Algorithm Print-characters Begin array Word [6] of type character //Data definition:](https://slidetodoc.com/presentation_image_h2/77d793ae235a867b7dbcc1e510b015e0/image-8.jpg)


![One-Dimensional Arrays • Declaration of one-dimension array Syntax: atype aname [ size ] ; One-Dimensional Arrays • Declaration of one-dimension array Syntax: atype aname [ size ] ;](https://slidetodoc.com/presentation_image_h2/77d793ae235a867b7dbcc1e510b015e0/image-11.jpg)
![One-Dimensional Arrays Examples • EX 1: int x [ 3 ]; - This tells One-Dimensional Arrays Examples • EX 1: int x [ 3 ]; - This tells](https://slidetodoc.com/presentation_image_h2/77d793ae235a867b7dbcc1e510b015e0/image-12.jpg)
![Accessing One-Dimensional Array int x [3] ; Array x in memory: 24 20 10 Accessing One-Dimensional Array int x [3] ; Array x in memory: 24 20 10](https://slidetodoc.com/presentation_image_h2/77d793ae235a867b7dbcc1e510b015e0/image-13.jpg)










![Algorithm Search Begin Array L [10] of integer Integer Cur. Elem, Search. Val Boolean Algorithm Search Begin Array L [10] of integer Integer Cur. Elem, Search. Val Boolean](https://slidetodoc.com/presentation_image_h2/77d793ae235a867b7dbcc1e510b015e0/image-24.jpg)

![The C++ Program #include <iostream. h> void main ( ) { int L [10] The C++ Program #include <iostream. h> void main ( ) { int L [10]](https://slidetodoc.com/presentation_image_h2/77d793ae235a867b7dbcc1e510b015e0/image-26.jpg)

- Slides: 27
Data type List Definition: The data type List (or set or array) allows: 1. To group together a limited number of elements. These elements have the same type (integer, real, character, string, ). 2. To fix the possible operations on these elements (input, output, sum, search, min, max…) 1
List general processing model • The elements of a list, being of the same type, undergo, generally, the same processing. • We start by processing the first element, the second, the third, and so on, up to the last. • The general model of this processing is as it follows: 2
Algorithm General-array-Processing Begin / / Data definition: declarations … / / Data Initialization … Obtain the first element to process; While (the last element to process is not yet processed) do / / There is at least one element to process Process the element / / Current element processing Obtain the next element to process / / next element obtaining 3 End
Data definition(declarations) The definition of the elements to be processed differs from a problem to other. These elements may be elementary (integer, character, …) or complex (word, sequence, sentence, paragraph, . . ). array_name [array size] of type_name • EX: array L [5] of type integer array word [10] of type character array sentence[4] of type string 4
Data Initialization EX 1: array L [5] of type integer L {2, 4, 7, 8, 9} EX 2: array word [6] of type character word {‘A’, ’t’, ’e’, ’n’, ’d’} EX 3: array sentence[4] of type string Sentence {“The” , ”book” , ”is” , ”good”} 5
Processing order array word [6] of type character word {‘A’, ’t’, ’e’, ’n’, ’d’} 1 2 3 4 5 6 processing order (index) Word[1] = A Word[2] = t Word[3] = t Word[4] = e Word[5] = n Word[6] = d 6
Array Operations • Several operations are allowed on the arry: Read. Array, Write. Array, Search. Array, Sum. Array, Min. Array, Max. Array, Sort. Array, etc. . 7
Example: Write. Array Algorithm Print-characters Begin array Word [6] of type character //Data definition: declarations word {‘A’, ’t’, ’e’, ’n’, ’d’} //Data Initialization Cur. Char 1 //Obtain the first Char to process //the last character to process is not yet processed While (Cur. Char 6) do Output Word [Cur. Char] //Current character processing Cur. Char + 1 //next character obtaining End while End Print-characters
• Example: Read. Array Algorithm Read. List. Of. Integers; Begin Const List. Size = 10 Array L [List. Size] of integer Cur. Elem is Integer Cur. Elem 1 While (Cur. Elem List. Size ) do Input L[Cur. Elem] Cur. Elem + 1; End While End Read. List. Of. Integers; 9
Arrays in C++ • Data types are of two kinds: - simple data types (e. g. int, float, double, char) - Structured data type: (e. g. arrays) • An array is a collection of two or more adjacent memory cells, called array elements, that are associated with a particular symbolic name. • Arrays are of two kinds: - Arrays of one-dimension - Arrays of two-dimension 10
One-Dimensional Arrays • Declaration of one-dimension array Syntax: atype aname [ size ] ; // uninitialized array atype aname [ size ] = { initialization list } ; where atype is any data type; aname is the name given to the array; size represents the number of elements in the array. initialization list is the list of initial values given to the array. 11
One-Dimensional Arrays Examples • EX 1: int x [ 3 ]; - This tells the compiler to associate 3 memory cells with name x. - These cells will be adjacent to each other in memory. - Each element of array x contains a value of integer type • EX 2: int Y [ 4 ] = { 2, 4, 6, 8 } ; This initializes array Y to have 4 elements which contain 2, 4, 6, 8. 12
Accessing One-Dimensional Array int x [3] ; Array x in memory: 24 20 10 How to process the data stored in an array? Syntax: aname [ index ] - index is the subscript that is used to reference the desired element. The array indexing starts from 0 until the fixed size -1. E. g. A [ 2 ] // refers to second element of A. A [ 2 ] is called a subscripted variable. 13
Accessing One-Dimensional Array x in memory: Index Values 0 1 24 20 2 10 Accessing the array: x [0] to access the first element of x x [1] to access the second element of x x [2] to access the third element of x 14
• Example: Write. List. Of. Integers #include <iostream> using namespace std; void main () { int L [5]={1, 2, 3, 4, 5}; int Cur. Elem=0; while (Cur. Elem < List. Size ) { cout<<L[Cur. Elem] ; Cur. Elem = Cur. Elem + 1; } } 15
• Example: Read. List. Of. Integers #include <iostream> using namespace std; void main () { const int List. Size = 10; int L [List. Size]; int Cur. Elem=0; cout<<"please enter 10 integer number"; while (Cur. Elem < List. Size ) { cin >> L[Cur. Elem] ; Cur. Elem = Cur. Elem + 1; } } 16
Examples on One-Dimensional Arrays • Example 1: Write a C++ program that stores the first 5 integers that are multiples of 5 into array A and reads data into array B; computes the sum of the two arrays and stores the result in array C. # include <iostream. h> void main ( ) { int A [5] ; //declaring array A of 10 integers int B [5] , C [5]; //declaring arrays B and C of 10 integers for ( int i = 0; i <5 ; i++ ) { A[ i ] = ( i + 1 ) * 5; cout << “enter new element in array B”; cin >> B[ i ] ; C [ j ] = A[ j ] + B [ j ] ; cout << C [ j ] << “ “ ; } } 17
Example 1. . Cont. The trace of the previous example shows the arrays as follows if B elements are 7 6 10 13 23: 0 1 2 3 4 A 5 10 15 20 25 B C 18 0 1 2 3 7 6 10 13 0 1 2 3 12 16 25 33 4 23 4 48
Example 2: Write a C++ program to read 10 integers and store them in array A. Then it finds the even numbers to store them in array B and the odd numbers to store them in array C. 19
Example 2. . Cont. #include <iostream. h> void main ( ) { int i; int A [10], B[10], C[10] ; cout << “ Enter 10 integers : “ ; for (i=0 ; i <10; i++) { cin >> A[i] ; if (A[i] % 2 == 0) B[i] = A[i] ; else C[i] = A[i]; } cout << “B element = “ << “ C element = “ << endl; for (i=0; i<10; i++) { cout << B[i] << “ “ << C[i] << endl ; } } 20
Examples on One-Dimensional Arrays Example 3: The resultant arrays B and C in example 2 contain data stored in non consecutive locations in the arrays. Modify the program of example 2 so that the data are stored in consecutive locations. 21
Example 3. . Cont. / File arrays. cpp #include <iostream. h> void main ( ) { int i, j = 0 , k = 0 ; int A [10], B[10], C[10] ; for ( i= 0 ; i <10; i++) { cin >> A[i] ; if (A[i] % 2 == 0) { B[j]=A[i]; j ++ ; } else { C [k] = A [ i ]; k ++ ; } } cout << “B element = “ << “ for (i=0; i<10; i++) { cout << “B[i] << “ } 22 C element = “ << endl ; “ << “ “ << C[i] << endl ; }
Example 4 The problem: Write an algorithm then a C++ program that searches for an integer in array of 10 integers. A proper message should be printed out. The Analysis: A given array of integer numbers is going to be searched in order to find a given number. Requirements: Input: an integer number n, an array A of 10 integers Output: a message “yes found” or “no not found” according to weather the number is found or not. 23
Algorithm Search Begin Array L [10] of integer Integer Cur. Elem, Search. Val Boolean Found // ------------ Reading L ---------------Cur. Elem 1 While (Cur. Elem 10 ) do Input L[Cur. Elem] Cur. Elem + 1; End While //--------------- End Reading L ---------24
Input Search. Val Found False; Cur. Elem 1; While (Cur. Elem < 1 and not Found ) do If (L[Cur. Elem] = Search. Val) then Found = True Else Cur. Elem + 1 End While IF ( Found = true) then Output “ Yes, the number is found “ Else Output “ No, the number is not found “ End Search 25
The C++ Program #include <iostream. h> void main ( ) { int L [10] ; int Cur. Elem, Search. Val; bool Found; // ------------ Reading L ---------------cout<<"Enter 10 integers: "; Cur. Elem = 0 ; while (Cur. Elem < 10 ) { cin>> L[Cur. Elem] ; Cur. Elem = Cur. Elem + 1; } //--------------- End Reading L ---------26
cout<< "Enter the number that you ant to search for: "; cin>>Search. Val; Found = false; Cur. Elem = 0; while (Cur. Elem < 10 && !Found ) { if (L[Cur. Elem] == Search. Val) Found = true; else Cur. Elem =Cur. Elem + 1; } if ( Found == true) cout<<" Yes, the number is found "; else cout<<" No, the number is not found" ; } 27