Lab Session-XII CSIT121 Spring 2002 - PowerPoint PPT Presentation

About This Presentation
Title:

Lab Session-XII CSIT121 Spring 2002

Description:

The temperature can be stored in degrees Celsius or degrees Fahrenheit. Assign 35 degrees Celsius to the appropriate union member and display. ... – PowerPoint PPT presentation

Number of Views:11
Avg rating:3.0/5.0
Slides: 13
Provided by: sunyfr
Category:

less

Transcript and Presenter's Notes

Title: Lab Session-XII CSIT121 Spring 2002


1
Lab Session-XII CSIT121 Spring 2002
  • Arrays and Their Usage
  • Finding the largest element
  • Lab Exercise 12-A
  • Lab Demo (12-B)
  • Practice Problems

2
Arrays and Their Usage
  • Arrays hold similar and related data items
    however data values are different
  • We can use arrays in any program where we have to
    deal with large amount of similar data using an
    INDEX into the data

3
Finding Largest Value
  • Often we will need to find the largest value in
    an array.
  • We can designate the first element as the largest
    in the beginning
  • As we compare one by one, we change mind if a
    larger value is found

4
Lab Exercise 12-A
  • Let us define an array and initialize it with
    some values
  • const int PLACES15
  • int distancesPLACES24,56,78,24,
  • 12,94,18,91,77,11,97,82,45,95,88
  • We wish to find the largest distance value in
    this array

5
Lab Exercise 12-A
  • Data Modeling
  • an array int distances
  • an integer largest as index to largest value
  • Algorithm
  • largest0, for (1 to PLACES-1) Repeat 1 2
  • 1. Designate distanceslargest as largest
  • 2. Compare with current value and change the
    largest index if found higher

6
Lab Exercise 12-B (Demo Required)
  • Develop a program that keeps information of names
    and ages of a group of 5 people.. An array of
    structs is recommended. This program calls a
    function and passes the array to it. The function
    prints the names and ages of all people in a
    nicely formatted way. Sample Data that can be
    used is as follows

7
Sample Data for Ex 12-B
  • Jason Anderson, 21
  • Christina Smith, 35
  • Brian Smith Jr., 42
  • Derek Dustin, 18
  • Jason Geiger, 19

8
Practice Problem
  • Given an array of ID numbers of library
    defaulters, search for the occurrence of a
    specific ID number to see if this person is a
    library defaulter or not
  • Data Modeling
  • int defaultersMAXSIZE
  • int target_ID
  • bool not_found

9
Practice Problem
  • Algorithm
  • not_foundtrue
  • While (not_found)
  • Match the target_ID with contents of the
    defaulters array one by one
  • If matched, assign not_foundfalse
  • Else if array end reached, person not found

10
Sample Data
  • const int MAXSIZE20
  • int defaultersMAXSIZE
  • 2435,1211,7643,2437,9834,2001,1029,1290,
  • 2340,2200,1214,4438,2105,4355,8254,9120,
  • 2502,9403,1006,1205
  • target_ID 8254
  • target_ID2000

11
Additional Practice Problems
  • Define a union that can hold temperature. The
    temperature can be stored in degrees Celsius or
    degrees Fahrenheit. Assign 35 degrees Celsius to
    the appropriate union member and display. Then
    convert it to Fahrenheit, store and display
  • Modify 12-B to include a union in the struct.
    This union should be able to sore the age in
    months or years

12
For Lab 13, Demo Problem
  • Write a value returning recursive function that
    implements the recursive formula
  • F(N) F(N-1) F(N-2)
  • Where F(1) equals 9 and F(0) equals 3
  • HINT The base case here could be reached when
    N-1 equals 1 and N-2 equals zero
Write a Comment
User Comments (0)
About PowerShow.com