Lab Session-X CSIT121 Spring 2002 - PowerPoint PPT Presentation

About This Presentation
Title:

Lab Session-X CSIT121 Spring 2002

Description:

(We have only 5 different scores reported) Record frequencies. Plot histogram using # sign ... Continue doing so until the remaining array consists of only one element ... – PowerPoint PPT presentation

Number of Views:10
Avg rating:3.0/5.0
Slides: 11
Provided by: sunyfr
Category:
Tags: csit121 | lab | session | spring | usage

less

Transcript and Presenter's Notes

Title: Lab Session-X CSIT121 Spring 2002


1
Lab Session-X CSIT121 Spring 2002
  • 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 the 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 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 (DEMO)
  • 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 displaying contents of
    original and reversed arrays

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 so until the remaining array
    consists of only one element
  • Try implementing the algorithm
  • Post Lab Exercise 9-3
  • Test sorting with a test case as below
  • Scores10 9,7,8,4,7,2,4,1,7,5

10
Practice Problems
  • Declare a character array consisting of 10
    locations. Read any ten characters from the
    keyboard into the array and then print the whole
    array using coutltltarrayname
  • Declare an integer array having 90 locations and
    initialize it with odd numbers in a counter
    controlled loop. For example, I0 1, I13,
    I25 I37 and so on
Write a Comment
User Comments (0)
About PowerShow.com