Arrays - PowerPoint PPT Presentation

About This Presentation
Title:

Arrays

Description:

Title: Arrays Author: Nadia Luka Last modified by: hanan Created Date: 5/19/2004 9:41:31 PM Document presentation format: On-screen Show Company: Philadelphia University – PowerPoint PPT presentation

Number of Views:121
Avg rating:3.0/5.0
Slides: 18
Provided by: Nadi125
Category:

less

Transcript and Presenter's Notes

Title: Arrays


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 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
7
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 Bcomputes the
    sum of the two arrays and stores the result in
    array C.
  • include ltiostream.hgt
  • 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 lt5 i )
  • A i ( i 1 ) 5
  • cout ltlt enter new element in array B
  • cin gtgt B i
  • C i A i B i
  • cout ltlt C i ltlt

8
Example1 (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
  • 0 1 2
    3 4
  • C

5
25
10
15
20
7
6
10
13
23
12
16
25
33
48
9
Example 2
  • 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.

10
Example 2 (Cont.)
  • // File arrays.cpp
  • include ltiostream.hgt
  • void main ( )
  • int i
  • int A 10, B10, C10
  • cout ltlt Enter 10 integers
  • for (i 0 i lt10 i)
  • cin gtgt Ai
  • if (Ai 2 0)
  • Bi Ai
  • else
  • Ci Ai
  • cout ltlt B element ltlt C
    element ltlt endl
  • for (i 0 i lt 10 i)
  • cout ltlt Bi ltlt ltlt Ci ltlt
    endl

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

12
Example 3 (Cont.)
  • / File arrays.cpp
  • include ltiostream.hgt
  • void main ( )
  • int i, j 0 , k 0
  • int A 10, B10, C10
  • for ( i 0 i lt10 i)
  • cin gtgt Ai
  • if (Ai 2 0)
  • B j A i j
  • else
  • C k A i k
  • cout ltlt B element ltlt C
    element ltlt endl
  • for (i0 ilt10 i)
  • cout ltlt Bi ltlt ltlt
    ltlt Ci ltlt endl

13
Example 4
  • 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.

14
The C Program for Example 4
  • //File search.cpp
  • include ltiostream.hgt
  • void main ( )
  • int n, i int A 10
  • bool flag false
  • cout ltlt Enter the number that you ant to
    search for
  • cin gtgt n
  • cout ltlt Enter 10 integers
  • for ( i 0 i lt 10 i )
  • cin gtgt Ai
  • if ( Ai n )
  • flag true break
  • if ( flag true )
  • cout ltlt Yes, the number is
    found \n
  • else cout ltlt No, the number is not
    found \n

15
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
  • Array in diagram
  • 0 1 2
  • 0
  • 1
  • Reading array of two-dimensions
  • int i , j
  • for ( i 0 i lt 2 i )
  • for ( j 0 j lt 3 j )
  • cin gtgt B i j

16
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 ltiostream.hgt
  • void main ( )
  • int i, j , clmsum 0
  • int B23
  • cout ltlt "Enter 6 array elements "
  • for ( i 0 i lt 2 i )
  • for ( j 0 j lt 3 j)
  • cin gtgt Bij
  • // Process the array now
  • for ( j 0 j lt 3 j)
  • for ( i 0 i lt 2 i )
  • clmsum clmsum Bij
  • cout ltlt " sum of column no. " ltlt j ltlt "
    is " ltlt clmsumltltendl
  • clmsum 0

17
Example2 of Two-Dimensional Array
  • Write a C program that reads an array of size
    (3 x 3) and finds the product of the diagonal
    elements.
  • //File diagonal.cpp
  • include ltiostream.hgt
  • void main ( )
  • int i, j , product 1
  • int B33
  • cout ltlt "Enter the 9 array elements "
  • for ( i 0 i lt3 i )
  • for ( j 0 j lt 3 j)
  • cin gtgt Bij
  • // Process the array now
  • for ( i 0 i lt 3 i)
  • for ( j 0 j lt 3 j )
  • if ( i j )
  • product product Bij
  • cout ltlt " The product of the diagonal
    elements " ltlt product ltlt endl
Write a Comment
User Comments (0)
About PowerShow.com