introNachenberg - UCLA Computer Science

Download Report

Transcript introNachenberg - UCLA Computer Science

Computer Science 31
Introduction To
Computer Science
Winter 2005
Lecture #1 - TR 10am-12pm
5419 Boelter Hall
Joseph R. Shinnerl
([email protected])
Welcome to Computer Science 31
• CS31 is an introduction to Computer Science
and programming in the C++ language
• Workload: Between 5 and 30 hours per week:
– Homeworks (Take 2-10 hours)
– Projects (Take 2-40 hours)
– CS31 is a “weeder” class
Welcome to Computer Science 31
• How best to learn:
– Read the book ahead of class
– Type in programs from the book and from
lectures. Run them. See how they work.
– Write your own programs… for fun!
• Watch out!
– If you’ve never programmed before, this is
going to be a nightmare class.
– Start projects the day they are assigned.
– People used to sleep in the computer labs!
• Classes:
More Details
– Tuesdays and Thursdays at 10am in 5419 Boelter Hall
• Discussion Sections:
– Fridays 8-10 AM in 5249 Boelter Hall
• Office hours:
– Tuesdays amd Thursdays 2:30-3:30PM
– 4731A Boelter Hall
• Class website(s):
– http://www.cs.ucla.edu/classes/winter05/cs31/
– All homework and project assignments will be posted on the
website. Check it every day or two!
– Homework Project #1 is posted right now!
• SEASNET Resources
– Labs: BH 3436, BH 4405, BH 4442
– For copies of Visual Studio.NET: See Julie Austin at BH 2567
Projects, Midterms & Finals
• Your grade is based on…
–
–
–
–
30% programming homework
10% written homework
20% midterm exam
40% final exam
• Midterm:
– Tuesday, February 15th during lecture
• Final exam:
– Thursday, March 24th, 2005, 11:30am-2:30pm
• To pass the course with grade C or better, you must
– pass both the programming homework and the final exam
– complete all programming assignments on-time.
• See the online syllabus for all of the details
Project #1
• Due 10:00 PM Friday, January 14th
• For your first project, you have to:
– Install the C++ compiler software (Visual Studio.NET) on
your computer
– Type in a simple C++ program that we provide
– Make some changes to the program to see what happens
– Turn in your C++ files and documentation on-line
• Make sure to start ASAP!
– You may run into problems installing the C++ compiler
software
– Make sure to follow the instructions on the website exactly
when submitting your project.
• Details on the class website:
– http://www.cs.ucla.edu/classes/winter05/cs31/hw/p1/spec.html
Computer Scientists Study
• How to write software
• How software should efficiently perform
specific tasks
• How software should store and retrieve
information
• How software and people should
communicate with each other
• What programs can and cannot do
– Yes – certain problems can’t be solved with a
computer now matter how hard we try!
Computer Science in Action
Artificial Intelligence: Computer chess
programs have tied and/or won world
chess championships!
Bioinformatics: Computers are helping
us fight diseases and understand our
genome.
Viruses: Computers can be used for bad
as well as good. Watch as Code Red
spreads across the world in 8 hours.
Where CS31 Fits In
• Before you run, you have to learn to crawl
• We’ll learn the C++ programming language
• We’ll learn basic programming constructs
• We’ll learn how to write programs
using these constructs
to solve simple problems
Computer Programs (Software)
All computer programs are made of sequences of
instructions that tell the computer what to do.
Here’s an example program that computes the average
of a set of N numbers:
1. Reserve a slot in the computer’s memory to hold the
answer
2. Store a value of 0 in this reserved slot
3. For each number in the set
A. Add the current number and value in the slot
B. Store this sum back in the slot
4. Divide the value in the slot by the set size N
5. Print out the value in the slot
Computer Programs (Software)
1. Reserve a slot in the computer’s memory to hold the
answer
2. Store a value of 0 in this reserved slot
3. For each number in the set
A. Add the current number and value in the slot
B. Store this sum back in the slot
4. Divide the value in the slot by the set size N
5. Print out the value in the slot
Numbers: 3
3, 17
17, 2
Answer:
7.333
22
30
+22
3
2=+ 20
/20
3 = 22
7.333
The answer is 7.333!
Computer Programs (Software)
1. Reserve a slot in the computer’s memory to hold the
answer
2. Store a value of 0 in this reserved slot
3. For each number in the set
A. Add the current number and value in the slot
B. Store this sum back in the slot
4. Divide the value in the slot by the set size N
5. Print out the value in the slot
Unfortunately, we can’t write our programs in English…
Natural languages like English are ambiguous:
“Time flies like an arrow, fruit flies like a banana.”
Modern Computer Architecture
CPU: The “brains” of the PC
PCs follow instructions at a
rate of up to 3.2billion/sec!
Hard Drive: Stores files (e.g.
your documents and games)
Typical PC HD size: 40-160GB
(Slow access: 10ms access)
Memory: Also known as “RAM”
Fast-access memory used by the
computer to complete tasks.
(Fast access: a few nanoseconds)
Different Computer Languages
Instead we use specially designed programming languages
to write programs.
Let’s examine 3 different types of programming
languages that we can use to write software:
• Machine languages
• Assembly languages
• High-level languages
Machine Language
Machine language is the simplest programming language
that you can use to write a program.
Each type of computer (e.g. Macintosh, PC) has its own
specific machine language.
Machine language programs are made of a series of
numbers.
The computer understands that each number represents a
specific instruction to the computer.
Machine Language Example
Instruction #
1
2
3
…
184
…
900
…
What the computer should do
Reserve a slot in memory
Add two numbers and store the result
End the program; its done!
Print a message to the screen
Play a note on the PC’s speakers
This is what a program looks like:
184 900 186 110 205 33 1 17 1 25 ...
Machine
Language
Memory chip, please give
me the first number.
184
900
186
110
205
33
...
The CPU fetches one number
at a time from its memory,
decides what to do, and then
moves on to the next number.
Hmm whatLanguage
does 184 mean? Oh,
Machine
it means print something to the
screen.
184
CS31
184
900
186
110
205
33
...
Machine
Language
Memory chip, please give
me the next number.
184
900
186
110
205
33
...
CS31
Machine
Language
Hmmm.. Oh yeah, 900 means
play a note on the speakers.
900
CS31
184
900
186
110
205
33
...
… And on it goes, until
the computer reaches an
instruction that says
“the program is done”
Assembly Language
While the computer explicitly understands machine
language, its hard for people to remember all those numbers!
So, computer scientists invented Assembly language.
It has the same structure and commands as machine language,
but the programmer uses easy-to-remember words/names
instead of numbers.
Our same program in Assembly Language:
OUTPUT
“CS31”
SUB
END
15, 5
ForPLAYFREQ
example, 1000
instead of typing in “1” to reserve a slot
in
the 20
programmer types in “RESERVE”.
ADDmemory,10,
Assembly Language
Unfortunately, computers don’t speak assembly language.
So we need to translate our human-readable assembly
programs into machine language before the computer can
run them.
To do this, we use an “assembler” program to convert
the assembly language source code into machine
language.
OUTPUT
PLAYFREQ
ADD
SUB
END
“CS31”
1000
10, 20
15, 5
184
900
186
110
...
High Level Languages
As you can see, assembly language is still pretty confusing.
So computer scientists invented “high-level languages” that
are closer to English and easier to program in.
High-level languages are called “high-level” because they’re
closer to human languages and further from machine
languages.
C++ is an example of a high-level language.
High Level Languages
// A C++ PROGRAM
C++ Compiler
#include <iostream>
void main(void)
// start
{
std::cout << “CS31”;
PlayNoteOnSpeakers();
}
// end
184
900
186
110
205
...
You use a “compiler” program to convert your high level
source code into a machine language program the
computer can understand.
High Level Languages
// A C++ PROGRAM
C++ Compiler
(For Macintosh)
#include <iostream>
void main(void)
{
std::cout << “CS31”;
PlayNoteOnSpeakers();
}
5
16
4
336
21
65
...
If you want to, you can “compile” your program for
other types of computers too (like an Apple)…
(Notice the #s are different!)
How Many High Level
Languages Can You Name?
Modula-2
AWK
APL
TCL
ADA
Java
PL/1
Cecil
Eiffel C
REXX
Forth
Pascal
Lisp
ML
Python
C++
B
Simula
Logo
Scheme
Algol
Snobol
Perl
Smalltalk
BETA
Cobol
AMPL
Fortran
BASIC
Delphi
Prolog
MCPL
C#
Haskell
What is C++
• C++ is the most popular programming language today
• C++ is a high-level programming language
• C++ was designed by Bjarne Stroustrup of Bell Labs
in 1983
• C++ is basically a superset of the C language
• So in CS31, you’re learning both C and C++!
• An international standards organization now sets the
standards for C++
• In fact, the C++ standard is so complex that no
fully-compliant compiler exists!
• C++ is an object oriented programming language
• That’ll make more sense next quarter
Famous Computer Disasters
•
•
•
•
•
–
–
–
–
–
Mars Climate Orbiter (1999)
The orbiter crashed because of a unit
conversion bug in the control software!
Cancer Deaths (1985-1987)
Three cancer patients died due to
radiation overdoses from a software bug
in the radiation machine
Ariane 5 Rocket Crash (1996)
The Ariane rocket crashed 40 seconds
after liftoff due to a software glitch in
its guidance computer! It cost $500
million!
NORAD False Alarm (1980)
On June 3, 1980, NORAD reported that
the U.S. was under missile attack. The
report was traced to a faulty computer
circuit.
Sewage Dump (1988)
Computers were blamed when, in three
separate incidents, 3 million, 5.4 million,
and 1.5 million gallons of raw sewage were
dumped into Willamette River.
The Programming Process
Design your
program
Edit your
program
Compile
your
program
No
No
Does it compile without errors?
Yes
Run and
test your
program
Does it work correctly?
Yes
Design
Goal: Figure out how your program is going to
work before you write a single line of logic.
Things to think about:
• What is the problem you’re trying to solve?
• What are your requirements?
• What are the main tasks required to solve
the problem?
• How should you organize your program to
best address the problem?
Editing Your Program
Just as English
majors use a
word processor
to write papers…
// A C++ PROGRAM
#include <iostream>
Programmers use
a similar program
called an “editor”
to type in their
program logic
(aka source code)
void main(void)
{
std::cout << “CS31”;
PlayNoteOnSpeakers();
}
Microsoft’s Visual Studio is an editor and compiler in one package.
You can type in your source code and compile it right away!
Compiling your Program
Goal: Translate your program from C++ source
code into a working program (into machine code)!
#include <iostream>
57119 10 4699 3
void main(void)
{
std::cout << “GO BRUINS!";
}
…
MyProg.exe
Compiling your Program
Goal: Translate your program from C++ source
code into a working program!
MyProg
GO BRUINS!
(What’s
happening
under the
hood is a bit
more
complicated
than this,
but that’s
the general
idea…)
Running and Testing Your Program
Goal: Make sure you don’t have any errors in
your program logic.
Things to do:
• Test your program to make sure it works
• Make sure your program works properly
even if the user doesn’t use it correctly.
• Fix any problems you find and then repeat
needed testing.
Our First C++ Programs
Goal: Learn about C++ by working through simple
examples.
Things to think about:
• Learn proper C++ syntax
• Learn proper programming style
• Learn how to accomplish certain tasks:
• Printing to the screen
• Inputting data from the user
• Making decisions
• Learn how to spot errors
// Our first C++ program: Hello world!
#include <iostream>
int main(void)
{
std::cout << "Hello ";
std::cout << “world...";
}
Hello world…
// Our first C++ program: Hello world!
#include <iostream>
int main(void)
{
std::cout << "Hello ";
std::cout << “world...";
}
/* a comment */
Everything following the // until the end of the line is
called a “comment” and is totally ignored by the compiler.
Comments are used by the programmer to document how
their program works.
You can also use the /* comment */ notation.
// Our first C++ program: Hello world!
#include <iostream>
#include <cmath>
int main(void)
{
std::cout << "Hello ";
std::cout << “world...";
iostream
std::cout << “The cosine of 52 is “ << cos(52);
}
The #include<filename> command basically means: “Hey compiler,
the file called “filename” contains important information you’ll
need to compile my program.”
iostream is a special file that contains information on how to print
information to the computer screen.
Similarly, you could #include<cmath> if your program used math
operations like sin/cosine/sqrt.
// Our first C++ program: Hello world!
#include <iostream>
int main(void)
{
std::cout << "Hello ";
std::cout << “world...";
main function header
main
function
body
}
All C++ programs have a “main function.”
When you run your program, the computer executes the
first instruction in the main program, then the second
instruction, etc.
When the computer reaches the last line in the main
function, your program is finished.
// Our first C++ program: Hello world!
#include <iostream>
#include <iostream>
int main(void){std::cout<<
"Hello ";std::cout <<
“world...\n ";}
int main(void)
{
std::cout << "Hello ";
std::cout
std::cout << “world...";
<< “world..."; // this is just fine!
}
Each line in the main function, separated by a semicolon ; is
called a statement.
A statement can span one or more lines, as required.
A statement performs one or more operations in a program.
Good style hint: indent your statements with a tab.
// Our first C++ program: Hello world!
#include <iostream>
int main(void)
{
std::cout << "Hello ";
std::cout << “world...";
}
To print text to the screen, use the std::cout command.
std::cout prints the data following the << to the screen.
Think of this line as: Send “Hello” to the screen (i.e. cout).
Any text enclosed in double “quotation marks” is called a
string.
Lets Learn Visual Studio
• Let’s learn how to create a new
project
• And add a new C++ file
• And then compile our program
• And then run our program