Lab SessionIX CSIT121 Fall 2000 - PowerPoint PPT Presentation

About This Presentation
Title:

Lab SessionIX CSIT121 Fall 2000

Description:

Arrays and Their Usage. Examples and Lab Exercises. Passing Arrays to Functions. Examples and Exercises. Sorting an Array. 2. Arrays and Their Usage ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 10
Provided by: sunyfr
Category:
Tags: sessionix | csit121 | fall | lab

less

Transcript and Presenter's Notes

Title: Lab SessionIX CSIT121 Fall 2000


1
Lab Session-IX CSIT121 Fall 2000
  • Arrays and Their Usage
  • Examples and Lab Exercises
  • Passing Arrays to Functions
  • Examples and Exercises
  • Sorting an Array

2
Arrays and Their Usage
  • Arrays consist of several data items that are of
    same data type and related to each other.
  • An array associates one name to a group of
    related data items that belong to the same data
    type
  • Example
  • const int ARRSIZE5
  • int studentsARRSIZE0
  • cingtgtstudent_index
  • if (student_index ltARRSIZE)
  • cout ltltstudentsstudent_indexltltendl

3
Arrays and Their Usage
  • One serious problem in array handling is the
    unintentional indexing outside bounds of an array
  • For example
  • studentsARRSIZE2 32
  • This problem may corrupt memory cells outside the
    array boundary

4
Lab Exercise 9-1
  • Let us work on making a histogram of performance
    for some students in a class.
  • int gradesARRSIZE7,14,13,13,11,7,7,8,8,13,13,1
    4,14,7,14,8,14,11
  • int frequency5
  • (We have only 5 different scores reported)
  • Record frequencies
  • Plot histogram using sign

5
Passing Arrays to Functions
  • Arrays are passed to functions as reference
    arguments
  • Functions can modify arrays if programmer does
    not protect the arrays
  • The way to protect is to add const keyword before
    the array name in declaration and definition of
    the function

6
Passing Arrays to Functions
  • As an example, consider multiplying all the
    elements of an array with a value and storing the
    results in another array.
  • We would like to protect the original array when
    we call this function
  • int original54,7,12,31,24
  • int multiplied50

7
Lab Exercise 9-2
  • Define a function that takes in two arrays of the
    same size and stores the reverse of the first
    array into the second array.
  • Protect the first array with const identifier
  • Check if protection works by trying to violate it
  • Complete the program by storing the original and
    reversed array into a file

8
Sorting an Array
  • Let us try to sort an array in ascending order
  • The basic algorithm is as follows
  • Find the smallest value in the array
  • Bring this value to index 0
  • Find the smallest value in the remaining array
  • Bring this value to index 1

9
Sorting an Array
  • Continue doing it until the remaining array
    consists of only one element
  • Try implementing the algorithm
  • Lab Exercise 9-3
  • Test sorting with a test case as below
  • Scores10 9,7,8,4,7,2,4,1,7,5
Write a Comment
User Comments (0)
About PowerShow.com