Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Arrays and Their Usage Finding The Largest Element Lab Exercise 12-A Lab Demo (12-B) Practice Problems

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 12

Lab Session-XII CSIT121

Spring 2002
 Arrays and Their Usage
 Finding the largest element
 Lab Exercise 12-A
 Lab Demo (12-B)
 Practice Problems

1
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

2
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

3
Lab Exercise 12-A
 Let us define an array and initialize it with
some values
 const int PLACES=15
 int distances[PLACES]={24,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
4
Lab Exercise 12-A
Data Modeling
 an array int distances[]
 an integer largest as index to largest value

Algorithm
 largest=0, for (1 to PLACES-1) Repeat 1 & 2
 1. Designate distances[largest] as largest
 2. Compare with current value and change the
largest index if found higher
5
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:
6
Sample Data for Ex 12-B
 Jason Anderson, 21
 Christina Smith, 35
 Brian Smith Jr., 42
 Derek Dustin, 18
 Jason Geiger, 19

7
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 defaulters[MAXSIZE];
 int target_ID;
 bool not_found;
8
Practice Problem
Algorithm
 not_found=true;
 While (not_found)
 Match the target_ID with contents of the
defaulters array one by one
 If matched, assign not_found=false
 Else if array end reached, person not found

9
Sample Data
 const int MAXSIZE=20;
 int defaulters[MAXSIZE] =
 {2435,1211,7643,2437,9834,2001,1029,1290,
 2340,2200,1214,4438,2105,4355,8254,9120,
 2502,9403,1006,1205};
 target_ID = 8254
 target_ID=2000
10
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

11
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

12

You might also like