CS1321: Introduction to Programming - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

CS1321: Introduction to Programming

Description:

... a grasp on how the class as a whole is doing just by looking at the grade sheets. ... working for the nefarious Dr. Doom (he teaches over in the Math Department) ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 29
Provided by: daniel141
Category:

less

Transcript and Presenter's Notes

Title: CS1321: Introduction to Programming


1
CS1321Introduction to Programming
  • Georgia Institute of Technology
  • College of Computing
  • Module 34
  • Vector Problem Course Management

2
In this module, well examine several problems
that will review working with vectors and loops.
3
Problem Statement
Its getting toward the end of the semester, and
much like the students on campus, instructors are
getting more and more concerned with grades.
Often times these instructors are working with
huge sections with a few hundred students
(theoretically) packing the lecture halls to the
brim. In this situation, its often very
difficult to get a grasp on how the class as a
whole is doing just by looking at the grade
sheets. There are simply too many numbers! In
order to get an overall feel for the class,
instructors often make use of histograms to
reduce the numbers to an easily read format.
4
Histograms
Smith, David 95 Lerner, Daniel
80 Greenlee, Jim 76 Sweat, Monica
88 Dagon, David 100 Burdell,
George 64 Kirby, Jack
85 Pratchett, Terry 83 Jordan, Robert
61 Goodkind, Terry 50 Frost, Robert
70 Jobs, Steve 61 Gates, Bill 30
We want to reduce all of these grades into some
sort of grouping for easy viewing.
5
Your Job
Youre a lowly teaching assistant working for the
nefarious Dr. Doom (he teaches over in the Math
Department). Hes teaching a section with 350
students this semester, and out of idle curiosity
wishes to know how the class is doing, and wants
you to tell him. Hell give you all the
students overall grades (out of 100) in a
350-element vector, and wants you to write some
functions that will do the following
  • Generate a histogram for groups of 10 points, 5
    points, and 1 point
  • Report the Mean and Mode for the class

6
Set-up
Before we start working with sensitive student
information, why dont we practice on some made
up information first? In order to do this, lets
start off by creating a vector to work with.
(define my-class (make-vector 350))
7
fill-vector!
Working with a 350-element vector of zeroes is
pretty boring, so why dont we fill it with
random numbers? Well make use of the scheme
function random and a loop in order to fill the
vector with somewhat realistic scores. (code on
next slide)
8
(define (fill-vector! in-vector) (do (i 0
( i 1)) (stop (vector-length
in-vector)) ( i stop) begin
(vector-set! in-vector i (random
101))))
9
Histograms as Vectors
All histograms really do is count how many
entries from our larger set of values (in this
case, the grades of the entire class) fit into a
certain category of value. For example, if our
histogram was counting by 1s, wed like to know
how many students got 50s, how many got 51s,
52s and so on. If we were counting by 5s, wed
like to know how many students got 50 through 54,
55 through 59, 60 through 64, and so on. If we
were counting by 10s, wed like to know how many
students got 50 through 59, 60 through 69, and so
on.
10
Histograms as Vectors
When you see a histogram, they look something
like
0 - 9 XXXXXX 10-19 XXXXX 20-29 XXX 30-39
XXXXXXX 40-49 XXXXXXXXX 50-59 XXXXXXXX 60-69
XXXXX
11
Histograms as Vectors
But really what youre seeing is the visual
representation of this
0 - 9 6 10-19 5 20-29 3 30-39 7 40-49
9 50-59 8 60-69 5
12
Histograms as Vectors
Which could in turn be represented as a vector
0 - 9 6 10-19 5 20-29 3 30-39 7 40-49
9 50-59 8 60-69 5
6
0
5
1
2
3
3
7
4
9
5
8
6
5
13
Creating our Histogram Vectors
Lets define a few vectors that will store our
histogram values. Well call them by-ones,
by-fives by-tens respectively.
(define by-ones (make-vector 101)) (define
by-fives (make-vector 21)) (define by-tens
(make-vector 11))
14
(define by-ones (make-vector 101)) (define
by-fives (make-vector 21)) (define by-tens
(make-vector 11))
Each element in these vectors represent a certain
range of values. Index 10 in by-ones represents
all the students who have a score of 10. In
by-fives, the same position represents all the
students who fall between 50 (5 10) and 54. In
by-tens, that position will hold all the students
who score 100 or above (if Dr Doom gives out
extra credit).
15
Filling our histogram vectors
Now we must ask ourselves how we are going to
translate all of those raw numbers stored in
my-class into ranges. Obviously we need to go
through each element in our my-class vector and
decide which range it fits into. But how do we
do that?
16
Approach 1 The Cumbersome Way
One possible approach we could take could be from
the point of view of the histogram we are trying
to create
  • For each element in our histogram
  • Determine the minimum and maximum values for the
    range
  • Loop through my-class and count how many elements
    fit into that range.

(code on next slide)
17
(define (fill-histogram1 in-class in-histogram
in-range) (do (i 0 ( i 1))
(stop (vector-length in-histogram)) (min
0 ( min in-range)) (max (- in-range 1)
( max in-range)) ( i stop)
in-histogram begin
(do (j 0 ( j 1))
(stop2 (vector-length in-class))
( j stop2) begin
(cond (and (lt min
(vector-ref in-class j))
(gt max (vector-ref in-class j)))
(vector-set! in-histogram i
( (vector-ref in-histogram i) 1))
else ""))))
18
(define (fill-histogram1 in-class in-histogram
in-range) (do (i 0 ( i 1))
(stop (vector-length in-histogram)) (min
0 ( min in-range)) (max (- in-range 1)
( max in-range)) ( i stop)
in-histogram begin
(do (j 0 ( j 1))
(stop2 (vector-length in-class))
( j stop2) begin
(cond (and (lt min
(vector-ref in-class j))
(gt max (vector-ref in-class j)))
(vector-set! in-histogram i
( (vector-ref in-histogram i) 1))
else ""))))
Of course, this is a highly in-efficient way of
doing things. (What is the Big O of these loops?)
19
Approach 2
A different way of viewing this problem would be
from the raw datas point of view. After all,
each number inherently knows which range it fits
into as long as you tell it how big the ranges
are
value 98 range size
10 value/range 9.8 floor (value/range) 9
20
Approach 2
  • For each element of our raw data
  • Determine which range it would fall into
  • Increment that element in our histogram by 1.

(code on next slide)
21
(define (fill-histogram2 in-class in-histogram
in-range) (do (i 0 ( i 1)) (stop
(vector-length in-class)) ( i stop)
in-histogram (begin
(let (position (floor (/ (vector-ref in-class i)
in-range))) (vector-set!
in-histogram position ( 1 (vector-ref
in-histogram position)) )))))
22
(define (fill-histogram2 in-class in-histogram
in-range) (do (i 0 ( i 1)) (stop
(vector-length in-class)) ( i stop)
in-histogram (begin
(let (position (floor (/ (vector-ref in-class i)
in-range))) (vector-set!
in-histogram position ( 1 (vector-ref
in-histogram position)) )))))
What is the Big O of this solution?)
23
Not done yet
As his name implies, Dr Doom is not a nice
person. Thus, hes not really happy with your
histogram functions. He wants to see nice pretty
graphs! Lets write some functions to do just
that.
24
display-line
Your histogram vectors hold a series of numbers,
representing the number of students that fall
into that range of values. Write a function
called display-line that takes in a number and
converts that number into a printed line of Xs,
where each X equals one person. (code on next
slide)
25
(define (display-line X) (do (count X (-
count 1)) ( count 0) (newline)
begin (display
"X")))
26
display-histogram
Using display-line, write a function
display-histogram that will take in your
histogram vectors and translate them into pretty
ASCII graphs. (code on next pagenote parameters)
27
(define (display-histogram in-grades gap)
(do (i 0 ( i 1)) (lead 0 ( lead
gap)) (stop (vector-length
in-grades)) ( i stop)
begin (display lead)
(display " ") (display-line
(vector-ref in-grades i))))
28
Additional Functionality
More fun with vectors! Utilizing either your raw
data or your histograms, write functions to
calculate
  • The Mean (or average) value
  • The Median value (which value is the absolute
    middle of your raw data? Hint this may involve
    sorting)
  • The Mode value (which numeric value appears most
    often?)
Write a Comment
User Comments (0)
About PowerShow.com