1 / 42

Computer Science 31 Introduction To Computer Science

Computer Science 31 Introduction To Computer Science. Winter 2005 Lecture #1 - TR 10am-12pm 5419 Boelter Hall. Joseph R. Shinnerl (shinnerl@ucla.edu). Welcome to Computer Science 31. CS31 is an introduction to Computer Science and programming in the C++ language

dakota
Download Presentation

Computer Science 31 Introduction To Computer Science

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Science 31Introduction To Computer Science Winter 2005 Lecture #1 - TR 10am-12pm 5419 Boelter Hall Joseph R. Shinnerl (shinnerl@ucla.edu)

  2. 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

  3. 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!

  4. 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

  5. Projects, Midterms & Finals Don't Cheat! • 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

  6. Project #1 • Due10: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

  7. 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!

  8. 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.

  9. 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

  10. 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

  11. Answer: 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 3 17 Numbers: 3, 17, 2 7.333 22 3 0 + = 20 + = 3 20 3 0 22 / 3 = 7.333 2 + 20 = 22 The answer is 7.333!

  12. 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.”

  13. 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)

  14. 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

  15. 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.

  16. Machine Language Example Instruction # What the computer should do 1Reserve a slot in memory 2Add two numbers and store the result 3End the program; its done! … 184Print a message to the screen … 900Play a note on the PC’s speakers … This is what a program looks like: 184 900 186 110 205 33 1 17 1 25 ...

  17. 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.

  18. Machine Language Hmm what does 184 mean? Oh, it means print something to the screen. 184 184 900 186 110 205 33 ... CS31

  19. Machine Language Memory chip, please give me the next number. 184 900 186 110 205 33 ... CS31

  20. Machine Language Hmmm.. Oh yeah, 900 means play a note on the speakers. 900 184 900 186 110 205 33 ... … And on it goes, until the computer reaches an instruction that says “the program is done” CS31

  21. 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” PLAYFREQ 1000 ADD 10, 20 SUB 15, 5 END For example, instead of typing in “1” to reserve a slot in memory, the programmer types in “RESERVE”.

  22. 184 900 186 110... 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 “CS31” PLAYFREQ 1000 ADD 10, 20 SUB 15, 5 END

  23. 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.

  24. High Level Languages 184 900 186 110 205 ... C++ Compiler // A C++ PROGRAM #include <iostream> void main(void) // start { std::cout << “CS31”; PlayNoteOnSpeakers(); } // end You use a “compiler” program to convert your high level source codeinto a machine language program the computer can understand.

  25. C++ Compiler (For Macintosh) 5 16 4 336 21 65 ... High Level Languages // A C++ PROGRAM #include <iostream> void main(void) { std::cout << “CS31”; PlayNoteOnSpeakers(); } If you want to, you can “compile” your program for other types of computers too (like an Apple)… (Notice the #s are different!)

  26. How Many High Level Languages Can You Name? Modula-2 AWK APL TCL ADA Java Total: 2500 languages!!! Fortran PL/1 Cecil C Eiffel REXX Forth Pascal Perl Simula Lisp Algol Smalltalk C++ MCPL Snobol BASIC ML B BETA Logo C# Cobol Delphi Python Scheme Prolog AMPL Haskell

  27. 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

  28. 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.

  29. The Programming Process Design your program Edit your program No Compile your program Does it compile without errors? Yes Run and test your program No Does it work correctly? Yes

  30. 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?

  31. 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!

  32. Compiling your Program #include <iostream> void main(void) { std::cout << “GO BRUINS!"; } Goal: Translate your program from C++ source code into a working program (into machine code)! 57 119 10 46 99 3 … MyProg.exe

  33. Compiling your Program GO BRUINS! Goal: Translate your program from C++ source code into a working program! (What’s happening under the hood is a bit more complicated than this, but that’s the general idea…) MyProg

  34. 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.

  35. 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

  36. // Our first C++ program: Hello world! #include <iostream> int main(void) { std::cout << "Hello "; std::cout << “world..."; } world… Hello

  37. // 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.

  38. iostream // Our first C++ program: Hello world! #include <iostream> int main(void) { std::cout << "Hello "; std::cout << “world..."; } #include <cmath> 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.

  39. main function header main function body // Our first C++ program: Hello world! #include <iostream> int main(void) { std::cout << "Hello "; std::cout << “world..."; } 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.

  40. // Our first C++ program: Hello world! #include <iostream> int main(void) { std::cout << "Hello "; std::cout << “world..."; } #include <iostream>int main(void){std::cout<< "Hello ";std::cout << “world...\n ";} std::cout << “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.

  41. // 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.

  42. 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

More Related