Computer Science 31 Introduction To Computer Science Joseph R. Shinnerl ([email protected]) Winter...
-
date post
20-Dec-2015 -
Category
Documents
-
view
214 -
download
0
Transcript of Computer Science 31 Introduction To Computer Science Joseph R. Shinnerl ([email protected]) Winter...
Computer Science 31Introduction To
Computer Science
Joseph R. Shinnerl ([email protected])
Winter 2005Lecture #1 - TR 10am-12pm
5419 Boelter Hall
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
• 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!
Welcome to Computer Science 31
More Details• Classes:
– 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 answer2. Store a value of 0 in this reserved slot3. For each number in the set A. Add the current number and value in the slot B. Store this sum back in the slot4. Divide the value in the slot by the set size N5. Print out the value in the slot
Computer Programs (Software)
1. Reserve a slot in the computer’s memory to hold the answer2. Store a value of 0 in this reserved slot3. For each number in the set A. Add the current number and value in the slot B. Store this sum back in the slot4. Divide the value in the slot by the set size N5. Print out the value in the slot
Numbers: 3, 17, 2
Answer: 0
3
0 + = 33
17
3 + = 20202 + 20 = 2222 22 / 3 = 7.3337.333
The answer is 7.333!
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.”
Computer Programs (Software)
1. Reserve a slot in the computer’s memory to hold the answer2. Store a value of 0 in this reserved slot3. For each number in the set A. Add the current number and value in the slot B. Store this sum back in the slot4. Divide the value in the slot by the set size N5. Print out the value in the slot
Modern Computer Architecture
CPU: The “brains” of the PCPCs 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
Let’s examine 3 different types of programming languages that we can use to write software:
• Machine languages• Assembly languages• High-level languages
Instead we use specially designed programming languages to write programs.
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 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 Example
Instruction # What the computer should do1 Reserve a slot in memory 2 Add two numbers and store the result3 End the program; its done!…184 Print a message to the screen…900 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
184 900 186 110 205 33 ...
Memory chip, please give me the first number.
The CPU fetches one number at a time from its memory, decides what to do, and then moves on to
the next number.
Machine Language
184 900 186 110 205 33 ...
184
Hmm what does 184 mean? Oh, it means print something to
the screen.
CS31
Machine Language
184 900 186 110 205 33 ...
Memory chip, please give me the next number.
CS31
Machine Language
184 900 186 110 205 33 ...
900
Hmmm.. Oh yeah, 900 means play a note on the speakers.
CS31 … 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.
For example, instead of typing in “1” to reserve a slot in memory, the programmer types in
“RESERVE”.
Our same program in Assembly Language:
OUTPUT “CS31” PLAYFREQ 1000 ADD 10, 20 SUB 15, 5 END
Assembly Language
OUTPUT “CS31”PLAYFREQ 1000 ADD 10, 20SUB 15, 5END
Unfortunately, computers don’t speak assembly language.
184 900 186 110...
To do this, we use an “assembler” program to convert the assembly language source code into
machine language.
So we need to translate our human-readable assembly programs into machine language before
the computer can run them.
High Level Languages
High-level languages are called “high-level” because they’re closer to human languages and further from
machine 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.
C++ is an example of a high-level language.
// A C++ PROGRAM #include <iostream> void main(void) // start{
std::cout << “CS31”;PlayNoteOnSpeakers();
} // end
High Level Languages
You use a “compiler” program to convert your high level source code into a machine language
program the computer can understand.
184 900 186 110 205 ...
C++ Compiler
High Level Languages
If you want to, you can “compile” your program for other types of computers too (like an Apple)…
// A C++ PROGRAM #include <iostream> void main(void){
std::cout << “CS31”;PlayNoteOnSpeakers();
}
(Notice the #s are different!)
5 16 4 336 21 65...
C++ Compiler(For Macintosh)
How Many High Level Languages Can You
Name?ADA
Algol
APL
BASICB
C
C++
Java
Cobol
Forth
Fortran
Lisp
Scheme
Pascal
Delphi
Perl
Python
TCL
Prolog
SnobolLogo
Smalltalk
REXX
PL/1
Eiffel
C#ML
Modula-2
Simula
HaskellAMPL
AWK
BETA
Cecil
MCPL
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 ProcessDesign your
program
Edit your program
Compileyour
program
Run andtest your program
Does it compile without errors?Yes
No
Does it work correctly?NoYes
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…
Programmers use a similar
program called an “editor” to type in their
program logic(aka source
code)
// A C++ PROGRAM #include <iostream> 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 ProgramGoal: Translate your program from C++ source
code into a working program (into machine
code)!
#include <iostream>void main(void){ std::cout << “GO BRUINS!";}
571191046993 …
MyProg.exe
Compiling your ProgramGoal: 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...";
}
Everything following the // until the end of the line is called a “comment” and is totally ignored by the compiler.
You can also use the /* comment */ notation.
/* a comment */
Comments are used by the programmer to document how their program works.
// Our first C++ program: Hello world! #include <iostream> int main(void){
std::cout << "Hello ";std::cout << “world...";
}
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.
#include <cmath>
iostream
Similarly, you could #include<cmath> if your program used math operations like sin/cosine/sqrt.
std::cout << “The cosine of 52 is “ << cos(52);
// Our first C++ program: Hello world! #include <iostream> int main(void){
std::cout << "Hello ";std::cout << “world...";
}
All C++ programs have a “main function.”
main function header
main function
body
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> int main(void){
std::cout << "Hello ";std::cout << “world...";
}
Each line in the main function, separated by a semicolon ; is called a statement.
A statement can span one or more lines, as required.
std::cout << “world..."; // this is just fine!
A statement performs one or more operations in a program.Good style hint: indent your statements with a tab.
#include <iostream>int main(void){std::cout<< "Hello ";std::cout << “world...\n ";}
// 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.
Any text enclosed in double “quotation marks” is called a string.
Think of this line as: Send “Hello” to the screen (i.e. cout).
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