Note: a sheet of paper with just a name earns 1 point (for participating)
and with all three parts earns 2 points: assigned work+participation.
Exercise
As a member of the CSci Honors committee I have to calculate a special GPA
of all people who apply for honors each qusrter. This is a GPA of
just the computer science classes -- and the main computer on campus
runs the Student Information System (SIS+) and doesn't do this for me.
So I need to store upto 30 floating point numbers, add them up, find the maximum and minimum, and print out these three statistics.
Write the Simplest Possible Program that could do this...
Online notes on topics in survey
Use for when counting or taking each item in a vector, array, string, etc. in turn.
Notice that
for(A; B; C;) Sdoes the same thing as
{ A;
while(B)
{
S;
C;
}
}
As a rule --
(KISS): Keep It Simple ...
What does break do
The break statement in C/C++ terminates a loop.
It also terminates a case in switch (see later)
Is there a 1 or 2 page handout on the 'vi' editor
Yes. See
[ Vi in resources ]
Are arrays important
You need to understand them because they occur in most
older programs and save storage and run time compared to vectors
and strings. When we ask for extra memory we often use an array
to get the data. Similarly when we read data off a disk or
tape we get a fixed amount of data in a fixed layout and an
array is best way to go.
Use an array only when you know, for an absolute fact, how many items you will need or (for experts) when you can calculate the absolute maximum number of items. Otherwise use vectors or dequeues.
They will be in the labs. I'll try to keep them out of the quizzes and final. They may be part of some projects. They have to turn up in several lectures/discussions.
In lab02 I will try to convince you that arrays can be dangerous.
We will do more with arrays in class as well.... in particular
we will wrap one up (chapter 7) in a class so it can be used safely.
Distinguish deques vectors and arrays
An
array
is a fixed number of numbered items. You can only
declare it with a constant size. You can not put in or remove items
from it.
A vector is like an array but it's size can change. It lets you push new items on to it at one end, and also remove (pop) them from the same end.
A deque Is like a vector except you can push and pop at both ends.
Here is an application of a deque -- simulating the line in the Arrowhead Post Office in North San Bernardino. People come in and join the line at one end and leave it at the other... but sometime somebody comes in joins the end (push_back) and, before any one else comes in, they give up and leave (pop_back).
Note: There are also stacks and queues. A queue you 'push' at one end
and 'pop' at the other. A stack you 'push' and 'pop' at the same end. More
later on
[ stl.html ]
when we cover the Standard Template Library.
What is the difference between = and ==
v=e //v is a variable, and e an expressionEvaluates e, and takes the value and stuffs it into v.
e1==e1 //e1 and e2 expressionsEvaluates BOTH e1 and e2, and compares them. If they are equal it return true otherwise false.
int twice(int i) { return i+i;}
double twice(double i) { return 2.0*i;}
string twice(string i) { return i+i;}
But they all do similar things.
The compiler picks the right one, so it is a waste of time
giving them different names.
How to trace a complex algorithm or program
Find a large sheet of paper or an erasable board. Divide
it into columns: one for each piece of data. Keep one finger
on the code/algorithm to make where you have got to. Write
new values in the right column. BE CAREFUL.
double s=0.0;
for(int i=1; i<5; i++)
s=s+i;
| Variables | s | i |
|---|---|---|
| Command | double | int |
| double s=0.0; | 0 | |
| i=1 | 0 | 1 |
| i<5? | ||
| s=s+i; | 1 | 1 |
| i++ | 0 | 2 |
| i<5? | ||
| s=s+i; | 3 | 2 |
| i++ | 3 | 3 |
| I<5? | ||
| s=s+i; | 6 | 3 |
| i++ | 6 | 4 |
| i<5? | ||
| s=s+i; | 10 | 4 |
| i++ | 10 | 5 |
| i<5? FALSE |
Avoiding guessing until you've traced it, step by step,...
What is a recursive function?
A recursive function is defined by a set of statements that
include the possibility of calling the function again on a
simpler or smaller set of data.
You can learn a lot about it from working out what a simple (useless) program does: [ 02rec.cpp ] , for example.
If you haven't figured it out yet, try this link [ lookup.php?search=recursive ]
Why does the program that translate American and British dates need an array of 9 chars?
There are 6 digits (MMDDYY) + 2 slashes(//) + one null character ('\0')
used to terminate the text strings. You may be able to do it with out
the two slashes and so need only 7 characters, however. I'm not
sure if this might end up being more confusing!
When should use a sentinel like -1 and when a CTRL/D to terminate input.
Use sentinels when there is more data to be sent after the sentinel.
Use a sentinel when normal humans provide the input.
Use CTRL/D or CTRL/Z when the data comes from a file, another
program, or a three star computer geek.
When to use ints and when to use doubles.
Is the data counting or indexing something? Is it always a
whole number? Then use int.
Is the data a measurement? Is it approximate? Can it have fractions?
Then use double.
Is if(not(cin>>a)) new?
No. It (or something like it) goes back to the very first book on C
in the late 1970's. Neat hacks like this tend to survive forever.
Will we be programming much graphics in the class.
No. Unless you want to! We will use simple graphic tools
like Dia to draw UML diagrams.
Will we be doing exceptions?
Yes in a later class..... see the schedule
Where do 'break' statement go?
In the middle of a loop, or at the end of a 'case' in a 'switch'(later).
Difference between while and do while
The while loop
while(C){B}
does this
C; B; C; B; ...; B; !C
The do-while loop
do{B}while(C)
does this
B; C; B; ...; B; !C
| Array | Class |
|---|---|
| is an object | is a data type |
| has numbered items | has objects with named members |
| is old fashioned | is object oriented
LabReview: [ lab01.html ]NextHow data is stored and retrieved on a computer (chapter 5) [ 03.html ]. . . . . . . . . ( end of section CSci202 Computer Science II, Session 02, Review) <<Contents | End>> AbbreviationsEnd |