Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays Topics to cover: Arrays Data Types One-dimensional Arrays

Similar presentations


Presentation on theme: "Arrays Topics to cover: Arrays Data Types One-dimensional Arrays"— Presentation transcript:

1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays
Two-dimensional Arrays

2 Arrays Data Types 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

3 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.

4 One-Dimensional Arrays Examples
EX1: 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 EX2: int Y [ 4 ] = { 2, 4, 6, 8 } ; This initializes array Y to have 4 elements which contain 2, 4, 6, 8.

5 Accessing One-Dimensional Array
int x [3] ; Array x in memory: 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. 24 20 10

6 Accessing One-Dimensional Array
Array x in memory: Index Values 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 24 20 10

7 Examples on One-Dimensional Arrays
Example1: WriteListOfIntegers #include <iostream> using namespace std; void main () { int L [5]={1,2,3,4,5}; for (int i=0; i<5; i++) cout<<L[i] ; }

8 Examples on One-Dimensional Arrays
Example2: ReadListOfIntegers #include <iostream> using namespace std; void main () { const int ListSize = 10; int L [ListSize]; cout<<"please enter 10 integer number"; for (int CurElem = 0; CurElem <=9; CurElem++) cin >> L[CurElem] ; }

9 Examples on One-Dimensional Arrays
Example 3: 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 5 integers int B [5] , C [5]; //declaring arrays B and C of 5 integers for ( int i = 0; i <5 ; i++ ) { A[ i ] = ( i + 1 ) * 5; cout << “enter new element in array B”; cin >> B[ i ] ; C [ i ] = A[ i ] + B [ i ] ; cout << C [ i ] << “ “ ; } }

10 Example3 (Cont.) The trace of the previous example shows the arrays as follows if B elements are : A B C 5 10 15 20 25 7 6 10 13 23 12 16 25 33 48

11 Example 4 Example 4: 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.

12 Example 4 (Cont.) // File arrays.cpp #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 ;

13 Examples on One-Dimensional Arrays
The resultant arrays B and C in example 2 contain data stored in non consecutive locations in the arrays. Modify the program of example 4 so that the data are stored in consecutive locations.

14 Example 5 (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 = “ << “ C element = “ << endl ; for (i=0; i<10; i++) { cout << “B[i] << “ “ << “ “ << C[i] << endl ; }

15 Example 6 The problem: Write C++ program that searches for an integer in array of 10 integers. A proper message should be printed out if the number is found or not. 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.

16 The C++ Program for Example 6
//File search.cpp #include <iostream.h> void main ( ) { int n, i ; int A [ 10 ] ; bool flag = false; cout << “ Enter the number that you ant to search for: “ ; cin >> n ; cout << “ Enter 10 integers: “ ; for ( i = 0 ; i < 10 ; i++ ) { cin >> A[i] ; if ( A[i] == n ) { flag = true ; break ; } } if ( flag == true ) cout << “ Yes, the number is found \n” ; else cout << “ No, the number is not found \n” ; }

17 Arrays of Two-Dimensions
Syntax ( in C++ ): atype aname [nrows] [ncolumns] ; Example: int B [2] [3] ; // declaring array B of 2 rows and 3 columns int A [3][3] = {1,2,3,4,5,6,7,8,9}; //initializing array A Array B in diagram: 1 Reading array B of two-dimensions: int i , j ; for ( i = 0 ; i < 2 ; i++ ) for ( j = 0 ; j < 3 ; j++ ) cin >> B[ i ] [ j ] ;

18 Example1 of Two-Dimensional Array
Write a C++ program that reads array A of size (2 x 3) and finds the sum of the elements in each column. //File arrays2d.cpp #include <iostream> using namespace std; void main ( ) { int i, j , clmsum = 0 ; int B[2][3]; cout << "Enter 6 array elements: " ; for ( i = 0 ; i < 2 ; i++ ) for ( j = 0 ; j < 3 ; j++) cin >> B[i][j] ; // Process the array now { for ( i = 0 ; i < 2 ; i++ ) clmsum = clmsum + B[i][j] ; cout << " sum of column no. " << j << " is " << clmsum<<endl; clmsum = 0; }}

19 Example2 of Two-Dimensional Array
Write a C++ program that reads array B of size (2 x 3) and finds the sum of the elements in each row, and store them in a one-dimensional array of size 2. //File arrays2d.cpp #include <iostream> using namespace std; void main ( ) { int i, j , rowsum = 0 ; int B[2][3], A[2]; cout << "Enter 6 array elements: " ; for ( i = 0 ; i < 2 ; i++ ) for ( j = 0 ; j < 3 ; j++) cin >> B[i][j] ; // Process the array now for ( i = 0 ; i < 2 ; i++) { for ( j = 0 ; j < 3 ; j++ ) rowsum = rowsum + B[i][j] ; A[i] = rowsum; rowsum = 0; }}

20 Example3 of Two-Dimensional Array
Write a C++ program that reads an array of size (3 x 3) and finds the product of the left diagonal elements. //File diagonal.cpp #include <iostream> using namespace std; void main ( ) { int i, j , product = 1 ; int B[3][3]; cout << "Enter the 9 array elements: " ; for ( i = 0 ; i <3 ; i++ ) for ( j = 0 ; j < 3 ; j++) cin >> B[i][j] ; // Process the array now for ( i = 0 ; i < 3 ; i++) for ( j = 0 ; j < 3 ; j++ ) if ( i == j ) product = product * B[i][j] ; cout << " The product of the diagonal elements = " << product << endl; }

21 Example3 of Two-Dimensional Array
Anther way to solve Example 3 //File diagonal.cpp #include <iostream> using namespace std; void main ( ) { int i, j , product = 1 ; int B[3][3]; cout << "Enter the 9 array elements: " ; for ( i = 0 ; i <3 ; i++ ) for ( j = 0 ; j < 3 ; j++) cin >> B[i][j] ; // Process the array now for (i=0; i<3;i++) product*=B[i][i]; cout << " The product of the diagonal elements = " << product << endl; }

22 Example4 of Two-Dimensional Array
Write a C++ program that reads an array of size (3 x 3) and finds the product of the right diagonal elements. //File diagonal.cpp #include <iostream> using namespace std; void main ( ) { int i, j , product = 1 ; int B[3][3]; cout << "Enter the 9 array elements: " ; for ( i = 0 ; i <3 ; i++ ) for ( j = 0 ; j < 3 ; j++) cin >> B[i][j] ; // Process the array now for ( i = 0 ; i < 3 ; i++) for ( j = 0 ; j < 3 ; j++ ) if (j == (-1 * i) + 2 ) product = product * B[i][j] ; cout << " The product of the right diagonal elements = " << product << endl; }

23 Example4 of Two-Dimensional Array
Another way to solve Example4. //File diagonal.cpp #include <iostream> using namespace std; void main ( ) { int i, j , product = 1 ; int B[3][3]; cout << "Enter the 9 array elements: " ; for ( i = 0 ; i <3 ; i++ ) for ( j = 0 ; j < 3 ; j++) cin >> B[i][j] ; // Process the array now j=2, product=1; for(int i=0; i<3; i++) { product*=B[i][j]; j--; } cout << " The product of the right diagonal elements = " << product << endl; }

24 Example5 of Two-Dimensional Array
Write a C++ program that initializes a 4 X 4 array A, and transpose it into array B. #include <iostream> using namespace std; void main ( ) { int A[4][4] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14, 15, 16}; int B[4][4]; int i, j; for (i=0; i<=3;i++) { for(j=0;j<=3;j++) cout<<" "<<A[i][j]<<" "; cout<<endl; } cout<<endl<<endl; for(i=0;i<=3;i++) for(j=0;j<=3;j++) B[j][i]=A[i][j]; cout<<" "<<B[i][j]<<" "; cout<<endl;} cout<<endl<<endl; }

25 Declaring an array size with an identifier
To declare an array size, you can use a fixed number: int A[5]; Or a constant: const arr_size = 10; int B[arr_size]; But, never a variable, this will cause a syntax error. To define a dynamic array (i.e. its size is decided during the program execution), you will need to use pointers. This will be discussed later.

26 Array as a parameter in a function
Array name is pointer (call by reference) When an array is sent to a function as a parameter, it is always a call by reference. #include <iostream.h> void Increment (int a[]) { for (int i=0; i<5; i++) a[i] += 10; } void main() { int b[5] = {10,20,30,40,50}; Increment(b); for(int i=0; i<5; i++) cout<<b[i]<<'\t'; cout<<endl; }

27 Array as a parameter in a function
Modify the previous example, so that the function Increment can receive any array of any size. #include <iostream.h> void Increment (int a[], int arr_size) { for (int i=0; i<arr_size; i++) a[i] += 10; } void main() { int b[5] = {10,20,30,40,50}; Increment(b, 5); for(int i=0; i<5; i++) cout<<b[i]<<'\t'; cout<<endl; }

28 Array element as a parameter in a function
- Array element as a parameter (call by value). #include <iostream.h> void Inc_element(int a) { ++a; } void main() { int b[5] = {10,20,30,40,50}; Inc_element(b[1]); cout<<b[1]<<endl; }

29 Array element as a parameter in a function
- Array element as a parameter (call by reference). #include <iostream.h> void Inc_element(int &a) { ++a; } void main() { int b[5] = {10,20,30,40,50}; Inc_element(b[1]); cout<<b[1]<<endl; }

30 Swapping two arrays using a function
#include <iostream> using namespace std; void swaparray (int a[], int b[], int arr_size) { int temp; for (int i=0; i<arr_size; i++) { temp = a[i]; a[i] = b[i]; b[i] = temp;} } void main () { int x[5] = {10,20,30,40,50}; int y[5] = {60,70,80,90,100}; cout << "Before swap "<<endl<<endl<<"Array x"<<endl; for (int i=0; i<5; i++) cout << x[i]<< " "; cout<< endl<<endl<<"Array y"<<endl; for (int j=0; j<5; j++) cout << y[j]<< " "; cout<< endl<<endl; swaparray(x, y, 5); cout << "After swap "<<endl<<endl<<"Array x"<<endl; }

31 Pointer Arithmetic Increment / decrement pointers ( ++ or -- )
Add / subtract an integer to/from a pointer ( + or += , - or -= ) Pointers may be subtracted from each other Pointer arithmetic is meaningless unless performed on an array

32 Pointer Arithmetic (Cont.)
Example Consider an integer array V of 5 elements, and an integer pointer vPtr. Pointer variable vPtr int *vPtr = V; //or you can declare it this way. int *vPtr = &V[0]; vPtr pointes to first element V[0] (location 1000) i.e. vPtr = 1000 vPtr +=2; sets vPtr to 1008 i.e. vPtr points to V[2] V[0] V[1] V[2] V[3] V[4]

33 Pointer Arithmetic (Cont.)
Subtracting pointers - Returns the number of elements between two addresses e.g. if v is an array and vPtr1 = &v[0]; vPtr2 = &v[2]; then vPtr2 – vPtr1 = 2 (i.e. 2 addresses)

34 Relations Between Pointers and Arrays
Arrays and pointers are closely related. - Array name is like constant pointer - Pointers can do array subscribing operations - If we declare an array A[4] and a pointer aPtr  aPtr is equal to A aPtr = A  aPtr is equal to the address of the first element of A aPtr = &A[0]

35 Relations Between Pointers and Arrays (Cont.)
Accessing array elements with pointers: - Element A[i] can be accessed by *(aPtr+i)  This is called pointer/offset notation - Array itself can use pointer arithmetic  A[3] is same as *(A+3) - Pointers can be subscripted (i.e. pointer/subscript notation)  aPtr [3] is same as A[3]

36 Example1: Pointers as arrays
#include <iostream> using namespace std; void main () { int A[5] = {10,20,30,40,50}; int *p; p= A; cout <<"First element of the array "<<*p<<endl; cout <<"Fourth element of the array "<<*(p+3)<<endl; }

37 Example2: Pointers as arrays
#include <iostream> using namespace std; void main () { int A[5] = {10,20,30,40,50}; int *p; p= &A[0]; for (int i=0; i<5; i++) cout << *(p+i) << " "; cout << endl;} }

38 Example3: Pointers as arrays
#include <iostream> using namespace std; void main () { int A[5] = {10,20,30,40,50}; int *p; p = A; cout << p++ << endl; //this will print the current address of the //pointer, then moves to the next location cout << ++p << endl; //this will move the pointer to the next location //and then prints the new address cout << (*p)++<<endl; //this will print the value that this pointer points //to, and then increment this value }

39 Dynamic Array using Pointers Example1
#include <iostream> using namespace std; void main () { int err_size; cin >> err_size; int *d = new int[err_size]; for (int i=0; i<err_size; i++) cin >> d[i]; cout<< endl; cout << d[i] <<endl; } err_size Reading into array Output

40 Dynamic Array using Pointers Example1: Re-written
#include <iostream> using namespace std; void main () { int err_size; cin >> err_size; int *d = new int[err_size]; for (int i=0; i<err_size; i++) cin >> *(d+i); cout<< endl; cout << *(d+i) <<endl; } err_size Reading into array The same Output

41 Dynamic Array using Pointers Example2
#include <iostream> using namespace std; void main () { int *p = new int[]; int arr_size = 0; char ch = 'y'; while (ch =='y') { cout << "enter an element"<<endl; cin >> p[arr_size]; //or you can use: cin >> *(p+arr_size); arr_size++; cout << "do you want to continue? (y/n)"<<endl; cin >> ch; } cout <<endl; for (int i=0; i<arr_size; i++) cout<< p[i]<< " "; //or you can use: cout<< *(p+i)<< " ";


Download ppt "Arrays Topics to cover: Arrays Data Types One-dimensional Arrays"

Similar presentations


Ads by Google