Array II - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Array II

Description:

jimmy represents a bidimensional array of 3 per 5 elements of type int. ... but both assign values to the memory block called jimmy in the following way: ... – PowerPoint PPT presentation

Number of Views:103
Avg rating:3.0/5.0
Slides: 18
Provided by: Too94
Category:
Tags: array | jimmy

less

Transcript and Presenter's Notes

Title: Array II


1
Array II
  • WEEK 10
  • DEPARTMENT OF COMPUTER SCIENCE
  • FACULTY OF SCIENCE AND TECHNOLOGY
  • ASSUMPTION UNIVERSITY

2
Agenda
  • Multi-dimensional arrays
  • Multi-dimensional arrays and nested loops
    matrix computation
  • Searching through an array using linear search
  • Sorting elements of an array using bubble sort
  • Examining bubble sort algorithm

3
Multidimensional Arrays
  • Multidimensional arrays can be described as
    "arrays of arrays".
  • For example, a bidimensional array can be
    imagined as a bidimensional table made of
    elements, all of them of a same uniform data
    type.
  • jimmy represents a bidimensional array of 3 per 5
    elements of type int. The way to declare this
    array in C would beint jimmy 35

4
Multidimensional Arrays
  • The way to reference the second element
    vertically and fourth horizontally in an
    expression would be jimmy13

5
Multidimensional Arrays
  • Multidimensional arrays are not limited to two
    indices (i.e., two dimensions).
  • They can contain as many indices as needed.
  • Be careful! The amount of memory needed for an
    array rapidly increases with each dimension. For
    examplechar century 100365246060de
    clares an array with a char element for each
    second in a century, that is more than 3 billion
    chars. So this declaration would consume more
    than 3 gigabytes of memory!

6
Multidimensional Arrays
  • Multidimensional arrays are just an abstraction
    for programmers, since we can obtain the same
    results with a simple array just by putting a
    factor between its indicesint jimmy 35
    // is equivalent toint jimmy 15 // (3 5
    15)
  • With the only difference that with
    multidimensional arrays the compiler remembers
    the depth of each imaginary dimension for us.
  • Take as example these two pieces of code, with
    both exactly the same result. One uses a
    bidimensional array and the other one uses a
    simple array

7
Multidimensional Arrays
  • Multidimensional Array
  • define WIDTH 5
  • define HEIGHT 3
  • int jimmy HEIGHTWIDTH
  • int n,m
  • int main ()
  • for (n0nltHEIGHTn)
  • for (m0mltWIDTHm)
  • jimmynm(n1)(m1)
  • return 0

Pseudo-multidimensional Array define WIDTH
5 define HEIGHT 3 int jimmy HEIGHT
WIDTH int n,m int main () for
(n0nltHEIGHTn) for (m0mltWIDTHm)
jimmynWIDTHm(n1)(m1) return 0
8
Multidimensional Arrays
  • None of the previous two source codes produce any
    output on the screen, but both assign values to
    the memory block called jimmy in the following
    way

9
Multidimensional Arrays
  • We have used "defined constants" (define) to
    simplify possible future modifications of the
    program. For example, in case that we decided to
    enlarge the array to a height of 4 instead of 3
    it could be done simply by changing the
    linedefine HEIGHT 3 todefine HEIGHT 4
    with no need to make any other modifications to
    the program.

10
Multidimensional Arrays and Nested Loops
  • It is also possible to create arrays with two or
    more dimensions.
  • Two dimensional arrays are useful for storing
    matrices.// two ways of creating two
    dimensional arraysint matrix 0, 1, 2,
    3, 4, 5, 6, 7, 8, 9, 10,
    11int square new int55// creates
    5 by 5 matrix with zero values

11
Multidimensional Arrays and Nested Loops
  • Taking the previous 2D array matrix as an
    example, in order to perform operations on each
    value of a multi-dimensional array, it is
    necessary to nest several for loops.
  • The example code below sets every entry of matrix
    to 1. for (int i0 ilt3 i) for (int j0
    jlt4 j) matrixij 1

12
Linear Search
  • Linear search is a part of integer arrays which
    helps user to find the number entered by him and
    tells him the exact location of the number.

13
Linear Search
  • include ltiostreamgt
  • using namespace std
  • int main(void)
  • int list30, num, n, i
  • cin gtgt n
  • for (i0 iltn i)
  • cin gtgt listi
  • cout ltlt "Enter the number to be searched"
  • cin gtgt num
  • for (i0 iltn i)
  • if (listinum)
  • break
  • cout ltlt "The number was found at " ltlt i
  • system("PAUSE")

14
Bubble Sort
  • Sorting is the method of arranging the elements
    of an array in an order (ascending or
    descending).
  • In the bubble sort, the list in any moment is
    divided into two sublists sorted and unsorted.
  • The smallest element is bubbled from the unsorted
    sublist and moved to the sorted sublist.

15
Bubble Sort
16
Bubble Sort
  • include ltiostreamgt
  • using namespace std
  • int main(void)
  • int temp, i, j
  • int ar10
  • cout ltlt "Enter elements of array\n"
  • for (i0 ilt10 i)
  • cin gtgt ari

17
Bubble Sort
  • for (i0 ilt10 i)
  • for (j10-2 jgt0 j--)
  • if (arjgtarj1)
  • temparj
  • arjarj1
  • arj1temp
  • for (i0 ilt10 i)
  • cout ltlt endl ltlt ari
  • cout ltlt endl
  • system("PAUSE")
Write a Comment
User Comments (0)
About PowerShow.com