>> What I want to talk to you now
about is homework called Stats and this is basically an exercise
and all of the things you are assumed to have learned about functions and
function calls and function prototypes. There's not that many classes
and objects in homework two. That will come in homeworks
three, four and so on. So, the basic concept here is
you're going to write functions that calculate the mean, that's the
average of a sequence of integers and you got to write another function that calculates the median
of a sequence of integers. In the process of calculating the
median you will discover the numbers need to be in order. You can pick out the middle
one and so in the process of calculating the median you
are going to have to learn how to sort those integers into
ascending order and you will find that those are the three functions
you're going to have to write, which is mean, median and sort. So, this is also kind of an exercise in beginning your software
engineering skills. So we're going to specify the
prototypes, which is another way of saying we're going to specify
the header file so that we, your instructors, can write
a program to that header file and you can write a program
to that header file, both of our programs should work. And we're going to give
you a way of testing to see that you've done that correctly. So let's talk a little bit about
this homework document, homework two. So-- [ Silence ] Let me just describe
the general structure of the way we do these documents. We will have educational objectives and
I will read the first couple of those for you, but you should
look at them carefully. So after successfully completing
this assignment you should be able to accomplish the following: Use a
loop to read user input of unknown size through standard input and store it
in an array; Use conditional branching to selectively perform tasks,
switch, if, else, whatever; Declare and define functions, that's
make prototypes and the implementations; Declare and define functions
with arguments of various types including
pointers, references, const pointers and const references. Call functions, remember that's the
three things we do to the functions. We prototype them, we define
them, which is implementing them and then we use them in programs
and that's the function call. So, generally we start out
with educational objectives and those are sort of overarching
things that you should take away from not only an assignment
but, of course, you want to learn them completely from
the assignment or from the material which supports the assignment as well. We also have operational
objectives and these tend to be much more down to earth. What am I supposed to do? In this create a project
that computes the mean and median of a sequence of integers. But deliverables is also
down to earth and concrete. What are your deliverables? Well four files, stats.h,
stats.cpp, main.cpp, and makefile. Now these files constitute
self-contained project. So first some background on
what the mean and median mean for a finite set of integers. So the mean is you just add them all
up, divide by the number of integers. That's the average. Statisticians like to use the word mean. The median is the middle value
and in the case where n is odd, there is a middle value, right. If you've got five items you got
first, second, third, fourth and fifth, third one would be the
middle one if they are listed in ascending numerical order
and that would be the median. When you have an even number of values,
what you do is you take the average of the two middle elements and
that's the median in this exercise. Now, to calculate the mean you can
add the numbers up and divide by n and it doesn't matter what order you add
numbers up in, the sum will be the same. To calculate the median you first have
to sort the numbers, so get them in, in increasing order first and
then find that little element. So, you're going to have to write
a sort algorithm to do this, implement a sort algorithm to this. And the one I would like for you
to do is called insertion sort. And I've given you sort of pseudo code
logical description of insertion sort without giving you the exact C++ syntax. But you really should not have much
trouble converting what I have there into legitimate C++ code. For example, copy t into a of i is
the assignment say a of i equals t; you end a for the same way you begin
the body of a for loop with a brace. You open it with a open brace and
you end it with a closed brace. So it's not hard to convert
that to actual code. You might want to think about
when you're implementing that think about what it's doing. What you're doing with insertion
sort is you've got an array. It comes in an array and what you want
to do is reorganize that array in such that it's in increasing order. So, we start out with the
first element by itself. Well that's in order, right. An array of one element
is in order right. So then you look at the second element
and see if it's bigger or smaller than the first one and you put
it either in front of or behind that first element depending on
whether it's bigger or smaller. And look at then the third
element and you begin to see how this algorithm works. So this third element you compare
it with the one just to its left. Now, that'll be the last thing
and was already sorted now. So you compare the third
element with the second one. If it's smaller than that second
one, then you copy the second one to the third place and
compare it with the first one. If it's smaller than the first
one, you got your first and second and then put it in that first place. Otherwise you put it
in the second place. So in other words you keep doing a
compare and copy up as you work your way down to the sorted part of the array
until you reach the place where it neads to be and stop and you put it there. [ Silence ] So that's insertion sort. [ Silence ] >> Are we dealing with integers or? >> Yes, we'll just deal
with integers for now. So the next thing we typically
have is procedural requirements. So these are the steps here
I'm required to go through: Creating work within a separate
subdirectory hw2, copy two files. Notice that we're giving
you the make file. Copy make file and your
hw2submit script. And then you create three more files:
stats.h, stats.cpp, and main.cpp. And these, of course, should comply with the technical requirements
that are listed below. And that after you've done that you turn
in your files using this submit script. And then finally, what we
typically have is a section called technical requirements. And then finally, a section called ints. So just to review the way these
assignment documents looking as you've got educational objectives,
operational objectives, deliverables, sometimes but not always a background
section that gives you the basic sounds or basic understanding of
what's going on independent of this particular project, in this
case they're talking about the mean, the median and the sort, insertion
sort, procedural requirements, technical requirements and
specifications and finally hints. So before I go into the technical
requirements and specifications I'm-- I want to illustrate,
I'll make this full screen since we're recording full screen. So I've already logged
in here to Linprog. It happened to give me Linprog 2. I've gotten myself into my homework 2
directory, so I'm going to type make and you'll see make doing its magic. Notice that it's calling g++47,
that's our compiler we're using. It's calling the C++ 11 library. The minus C option is
create object code only. In other words compile only. The Wall and Wextra gives us all the
warnings you could possibly hope for. The -I. says that we got to search
for files in the current directory and we also got to search for files
in the course library cpp directory. So those are two additional
places to find files that programs might need besides the
ones that are built into the compiler. And then finally, it's going
make ID code out of main.cpp. And then it's going to make
ID code out of stats.cpp. Now I'm compiling something
called ranint.cpp. Did I give you that. I guess I need to. I may not have stated that I'm giving
it to you, but I'm pretty sure I am. And finally, stats.o. So anyway that's
going to create some executables. And remember we're calling
executables with a .x suffix, which means something only to us. And it just makes us, makes it easy
for us to identify the executables in a directory so that among other
things we can go and erase them because otherwise if we store too
many executables you begin to run into your file space limitations and
that can create problems for you. So you want to be able to get
rid of your executables simply and having a .x suffix
is a way to do that. So notice that I've created
three things: stats.x, test.x, and ranint.x. So I'm going to
just run stats.x and notice that the prompt says
enter integers x to quit. So I'm going to enter integers 2 1 4
3 7 8 10 15 -1 -4 and I'll just put 1 in there again and 2 in
there again for good measure. Now I could keep entering some more because I haven't hit an x
yet, but I'll just do 3 4 5x. When it finds that x, okay. So what it'll do it read 2 1 4
5 3 7 8 10 15 -1 -4 1 2 3 4 5 and there you see them. The data is entered 3 4 5. It stopped reading as soon
as it saw the x, okay. Now, the data is entered, you
can calculate the mean directly and that turns out to be 4.0625. That's not that interesting,
but that's what it is. The median is 3.5 and notice that
the second output is data after sort. So before the median was calculated
the data needed to be sorted, right. And we're outputting the
data after the sort here just to give us visual verification
that yea, the sort did work. And you'll notice that 3.5, so
middle two elements must be 3 and 4 and so my median is 3.5
average of those two. So that's what your program
is supposed to do. [ Silence ] Gosh I want to explain that stats.x,
that's me compiling my main program. You've got to write a
main program for yourself. It may function pretty much like, it should function pretty
much like this one. It doesn't have to be exact. But I'll recommend getting it close because it'll be easier
for you to debug. The test.x what that is, is something
that's going to help you in two ways. Test.x is our main.o compiled with your
stats.o. So, that's our main program with all this interactive behavior using
your function, mean and median and sort. Now, so you can run test.x and you'll
get a screen looking exactly like this. You can run stats.x and that'll be
your version of this screen, okay. So now let me minimize that and go
back to talking about the specs. [ Silence ] Technical requirements, so you know this
is something that will always happen and I'll probably always say it. But it should go without saying
and definitely when you get into professional environments. Anyway I shouldn't need
to tell you this. But, the product should compile
error free and warning free. So, when we compile it we're going
to use this make file you're given. And if we see warnings from the
compiler then you get, you'll get then. You've got to make those
warnings go away. So you got to make the
compiler really like your code. So when it gives you a warning
it says I don't feel good about this code even though
I can compile it, right. You want it to feel good
about your code. You can assume that the number
of integers will not exceed 100. This will help you in
writing your test program. You basically use an array of
int of statically declared array of 100 integers and keep track of how
many you read and stop when you get to 100, stop reading when you get to 100
whether there's more to enter or not. Now you're going use standard input. That's std::cin and standard
output std::cout. So we're not opening files and
stuff like that, but we're going to use redirect, which is
this very handy thing in Unix, which I will now illustrate. Notice that I have some data files. We can just make a data file. I'm going to just call it
1.x and it'll get erased. So we'll just put some
integers in there. [ Typing on keyboard ] And these could all be on one line,
some on different lines, whatever. I'm going to say that get out, so I
now got that 1.x. So I can do stats.x and this is how you redirect. The little less than symbol,
think about it as a funnel. It goes from big side to little
side, so I'm going to give it my 1.x and what that's going to do is
funnel that file into standard input. I also have a file called data1 and
data2 so I can data1.in, that's data1 and data2.in, that's data2.in. I even put an extraneous
character in data2. That won't hurt anything;
it'll just get to that. So what this-- what my loop structure
does, and I gave you a hint on how to write these loops in chapter
two of the lecture notes, but this loop just reads
until the read fails. Now it can, if you're reading from
keyboard it's time to read int, right. So when it sees that-- I could
have used any other character. So put a character in there that's not
an int character the read will fail and then the program stops reading. So files, if the file has
nothing but inters in it, it'll fail when it gets
to the end of the file. Or you could put characters that
aren't digits below all the digits. You could put a bunch of
documentation in there. And as soon as it got to the
documentation it would fail to read an integer and stop reading. So, observe that I can do
data1.in, I can do data2.in and I can put keyboard
input and terminate it with a nine digit character. No we're at the same loop
structure using standard input. I know it's time to quit. So, what I didn't get around to
is going over the technical specs. You need to read those carefully and pay
particular attention to the spelling, to the capitalizations, to the
exact types of the parameters. Because if any of those are different from specifications then
your header file and our header file won't be
compatible and you won't be able to compile your functions
with our test program. So they depend on that interface
being the agreed on interface. We're going to stop this
and take it up next time. [ Scrolling through computer screen ]