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

Slides:



Advertisements
Similar presentations
1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
Advertisements

This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Arrays Programming COMP102 Prog. Fundamentals I: Arrays / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g.,
Pass by Reference. COMP104 Pass by Reference / Slide 2 Passing Parameters by Reference * To have a function with multiple outputs, we have to use pass.
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
Basic Elements of C++ Chapter 2.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
C++ Tutorial Hany Samuel and Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2006 by Douglas.
C++ Programming: Basic Elements of C++.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
Pointers and Arrays Dynamic Variables and Arrays.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Bill Tucker Austin Community College COSC 1315
Pointers What is the data type of pointer variables?
Chapter 1.2 Introduction to C++ Programming
EGR 2261 Unit 11 Pointers and Dynamic Variables
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Computer Skills2 for Scientific Colleges
The Ohio State University
Basic Elements of C++.
Chapter 2 Assignment and Interactive Input
Programming fundamentals 2 Chapter 1:Array
Basic Array Definition
Pointers and Pointer-Based Strings
CSC113: Computer Programming (Theory = 03, Lab = 01)
Student Book An Introduction
Pointer Data Type and Pointer Variables II
CSC113: Computer Programming (Theory = 03, Lab = 01)
Multi-dimensional Array
C++ Arrays.
Programming fundamentals 2 Chapter 2:Pointer
Basic Elements of C++ Chapter 2.
Array Data Structure Chapter 6
Pointers and dynamic objects
Pointers and Dynamic Variables
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Compound Assignment Operators in C++
Programming Funamental slides
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Return_DT F_name ( list of formal parameters)
Pointers, Dynamic Data, and Reference Types
Pointers and Pointer-Based Strings
Data type List Definition:
Programming Funamental slides
Lecture 12 Oct 16, 02.
Computer Skills2 for Scientific Colleges
Pointers Kingdom of Saudi Arabia
Pass by Reference.
The Function Prototype
Arrays of Two-Dimensions
7 Arrays.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Array Data Structure Chapter 6
Pointers and Pointer-Based Strings
Arrays Arrays A few types Structures of related data items
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Lecture 2 Arrays & Pointers May 17, 2004
C++ Programming Lecture 18 Pointers – Part II
Pointers and dynamic objects
Lecture 2 Arrays & Pointers September 7, 2004
CISC181 Introduction to Computer Science Dr
Pointers, Dynamic Data, and Reference Types
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
Dr. Khizar Hayat Associate Prof. of Computer Science
Programming fundamentals 2 Chapter 3:Pointer
Presentation transcript:

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

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

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.

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.

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

Accessing One-Dimensional Array Array x in memory: Index 0 1 2 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

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] ; }

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] ; }

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 ] << “ “ ; } }

Example3 (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 0 1 2 3 4 B C 5 10 15 20 25 7 6 10 13 23 12 16 25 33 48

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.

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 ;

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.

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 ; }

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.

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” ; }

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: 0 1 2 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 ] ;

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; }}

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; }}

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; }

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; }

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; }

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; }

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; }

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.

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; }

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; }

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; }

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; }

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; }

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

Pointer Arithmetic (Cont.) Example Consider an integer array V of 5 elements, and an integer pointer vPtr. 1000 1004 1008 1012 1016 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]

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)

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]

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]

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; }

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;} }

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 }

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

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

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)<< " ";