34
1 0 © Janice Regan, CMPT 102, Sept. 2006 CMPT 102 Introduction to Scientific Computer Programming Course Organization © Janice Regan, CMPT 102, Sept. 2006 1 CMPT 102: Introduction to Scientific Computer Programming Monday, Wednesday, Friday 9:30-10:20 Textbook: C Program Design for Engineers Authors: Jeri R. Hanly, Elliot B. Koffman Additional References: Engineering Problem Solving with C, 3 rd edition Author: Delores M. Etter The C Programming Language Authors: B. W. Kernighan, D. M. Ritchie

CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

Embed Size (px)

Citation preview

Page 1: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

1

0© Janice Regan, CMPT 102, Sept. 2006

CMPT 102Introduction to Scientific Computer Programming

Course Organization

© Janice Regan, CMPT 102, Sept. 2006 1

CMPT 102: Introduction to Scientific Computer Programming

Monday, Wednesday, Friday 9:30-10:20Textbook:

C Program Design for EngineersAuthors: Jeri R. Hanly, Elliot B. Koffman

Additional References:Engineering Problem Solving with C, 3rd editionAuthor: Delores M. Etter

The C Programming LanguageAuthors: B. W. Kernighan, D. M. Ritchie

Page 2: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

2

© Janice Regan, CMPT 102, Sept. 2006 2

Class web-siteAll the information discussed today and more can always be found on the class web-siteTo find the class web site go to http://www.cs.sfu.ca/CourseCentral

Select Course Home pagesSelect the Homepage for CMPT 102

Course Central is also a useful link to abundant information useful to students taking a computing science course

© Janice Regan, CMPT 102, Sept. 2006 3

http://www.cs.sfu.ca/CourseCentral

Page 3: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

3

© Janice Regan, CMPT 102, Sept. 2006 4

Course selection page

Click here to go

to CMPT102 website

© Janice Regan, CMPT 102, Sept. 2006 5

CMPT 102 Website

Page 4: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

4

© Janice Regan, CMPT 102, Sept. 2006 6

My availability

© Janice Regan, CMPT 102, Sept. 2006 7

Exams Dates, Course Evaluation

Page 5: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

5

© Janice Regan, CMPT 102, Sept. 2006 8

Exams and Evaluation

© Janice Regan, CMPT 102, Sept. 2006 9

EvaluationAssignments: 30% Weekly Quizzes: 15%

No quiz in week of midterm, week of Thanksgiving or week of Remembrance day

Lab Problems: 5% Practice Problems: no creditMidterm Examination: 15% Final Examination: 35%

Page 6: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

6

© Janice Regan, CMPT 102, Sept. 2006 10

Assignments

Four assignments Each assignment worth 7.5% of your course grade Assignments are to be completed individuallyProblems similar to the assignments will appear on the midterm and the final.Help is available during weekly lab hoursComplete solutions will be posted

© Janice Regan, CMPT 102, Sept. 2006 11

Lab Problems

Four short lab problems Each lab problem is worth 1.25% of your final grade Practice Problems: no credit BUT valuable experience that will be useful when completing lab problems and assignments

Page 7: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

7

© Janice Regan, CMPT 102, Sept. 2006 12

QuizzesDuring each Wednesday’s lecture, you will be asked to answer one question based on material covered in the lab exercises that week.

You will be asked to write a short concise paragraph of 10-15 sentences, explaining a concept or method, or a short piece of code. You will be given 10-15 minutes to answer.Your answer will be graded out of 17 (each quiz is worth 1.7% of your final grade)

© Janice Regan, CMPT 102, Sept. 2006 13

Midterm and FinalOne 50 minute in class midtermOne 3 hour final examination35% short answer problems65% longer problems that utilize several important concepts and require integration of those concepts. A sample exam, including solutions, will be posted two weeks before the midterm and two weeks before the final exam

Page 8: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

8

© Janice Regan, CMPT 102, Sept. 2006 14

Assignments, Labs, Solutions

© Janice Regan, CMPT 102, Sept. 2006 15

Important Dates

Page 9: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

9

© Janice Regan, CMPT 102, Sept. 2006 16

AssignmentsAt least two weeks before the due date the assignment will be posted on the website

Assignments must be completed individuallyAssignments will require you to combine and use many concepts and tools you have learnedAssignments will be much more demanding than lab problems or practice problemsComplete solutions to all assignments will be posted Information of proper preparation of assignments and lab problems is available on the class website.

© Janice Regan, CMPT 102, Sept. 2006 17

LabsOn or before Friday of each week problems will be posted for the next week. These problems will be identified as

Lab Problems: To be completed individually or in a small group (up to four students)

Submitted for gradingPractice Problems: A chance to use a new idea in a simple application, before you need to integrate multiple ideas into assignments.

Collaboration is encouragedNot submitted for grading

Complete solutions to all problems will be posted

Page 10: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

10

© Janice Regan, CMPT 102, Sept. 2006 18

Grading InformationAssignments and Lab Problems should be:

submitted electronically using the submission serversubmitted in hard copy to the 102 drop box

Bonus points (10%) are available for submitting assignments more than 72 hours earlyNo late assignments or lab problems will be acceptedUnofficial grades will be available on GradeBook

Login: your SFU computer ID, your student number as password. Find link on Course Central

© Janice Regan, CMPT 102, Sept. 2006 19

Where to submit assignments: 1

Page 11: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

11

© Janice Regan, CMPT 102, Sept. 2006 20

Where to submit assignments: 2

Submission drop boxes

© Janice Regan, CMPT 102, Sept. 2006 21

Required Readings, Notes

Page 12: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

12

© Janice Regan, CMPT 102, Sept. 2006 22

Readings and Notes

© Janice Regan, CMPT 102, Sept. 2006 23

Academic Honesty

Page 13: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

13

© Janice Regan, CMPT 102, Sept. 2006 24

Academic Honesty

Read the policy

© Janice Regan, CMPT 102, Sept. 2006 25

Getting Started

If you have not already done so activate your SFU computing account (CCN, Campus Computing Network account.)

Use your own machine at home, or go to the assignment lab (AQ 3145)Email regarding the class will be sent to your Campus Account, so check it regularlySteps to activate your account …

Page 14: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

14

© Janice Regan, CMPT 102, Sept. 2006 26

Activating your CCN account (1)

Select mySFU

© Janice Regan, CMPT 102, Sept. 2006 27

Activating your CCN Account (2)

Select Apply for ID, the follow the instructions. You will need your student ID and your TeleReg PIN

Page 15: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

15

© Janice Regan, CMPT 102, Sept. 2006 28

Computing Science Instructional Labs (CSIL)

The CSIL LINUX lab is available for your use at all times beginning in the second week of classes. (ASB 9838)At particular times the instructor and/or TA will be available in the lab to answer your questions.

Instructor: Mon. 1:30-3:00 PM,Tue: 9:00-10:30 AM

TA: Tue: 10:00 – 10:30 AM. TBA

© Janice Regan, CMPT 102, Sept. 2006 29

Finding CSIL labs: 1

Page 16: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

16

© Janice Regan, CMPT 102, Sept. 2006 30

Finding CSIL labs: 2

CSIL Labs

© Janice Regan, CMPT 102, Sept. 2006 31

Access to CSIL

CSIL is protected by a security card access system

As a student in a computing science course you are eligible to have a security access card for CSILIf you are pre-registered your access card may be picked up from the card office at traffic and security (there is a $15 refundable deposit and a $3 processing fee)

Page 17: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

17

© Janice Regan, CMPT 102, Sept. 2006 32

CSIL RulesBefore you can use any of the computers in the CSIL you must sign and return an ethics form.

By signing the ethics form you indicate that you have read and agree to abide by the SFU and CSIL regulations regarding use of computing resources and computer labs. Links to these policies can be found on the class website or on Course CentralYou can return your ethics forms during next class, or directly to the computing science main office.

© Janice Regan, CMPT 102, Sept. 2006 33

http://www.cs.sfu.ca/CourseCentral

Select to reach Policies

Page 18: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

18

© Janice Regan, CMPT 102, Sept. 2006 34

CSIL page

IMPORTANTRead thePoliciesBefore Signing

Your Ethics form

© Janice Regan, CMPT 102, Sept. 2006 35

Getting Started in CSIL

Before you try programming in CSIL read the information provided for you

Read the general information pagesBrowse the FAQs, Now you are ready to start

Your CSIL computer ID and password and the ID an password you use for your SFU email account.

Page 19: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

19

© Janice Regan, CMPT 102, Sept. 2006 36

CSIL page

Read thegeneral

information

Browse The

FAQs

© Janice Regan, CMPT 102, Sept. 2006 37

Labs and using CSIL

The computers in CSIL are available 24/7At some times your instructor or TA will be available to answer questionsAt some times OPEN LAB TA’s will be available to answer questions.A list of TA’s presently in the lab will be posted on the white board in CSIL.

Page 20: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

20

© Janice Regan, CMPT 102, Sept. 2006 38

Optional Lab HoursAttendance at optional Lab hours is strongly encouraged

Lab Hours are held in the LINUX area of CSIL labs Lab Hours provide an opportunity to work with others sharing knowledge and experience. Lab Hours give you an opportunity to ask the instructor or TA one on one questions about problems and assignments. Lab Hours help you learn the required concepts one by one and allow you to ask questions to assure you understand each one.

© Janice Regan, CMPT 102, Sept. 2006 39

Operating System

In the CSIL labs you will be using computers that run the LINUX operating systemIn the first optional lab hours there will be tutorials on how to use the LINUX operating system (1 per hour)The tutorial will also be posted so you can try it yourself at another time

Page 21: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

21

© Janice Regan, CMPT 102, Sept. 2006 40

Reading AssignmentVisit and read the information on the class websiteBe sure you can find

Posted notesReading assignments for each lecture Assignment due datesInstructions on using the CSIL labsInstructions on using your own computer for assignments

Have any questions ready for next lecture

41© Janice Regan, CMPT 102, Sept. 2006

CMPT 102Introduction to Scientific Computer Programming

Chapter 1: Computer Components

Page 22: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

22

© Janice Regan, CMPT 102, Sept. 2006 42

Hardware and Software

A computer is a machine designed to perform operations specified with a set of instructions called a program.Hardware refers to the computer equipment.

keyboard, mouse, terminal, hard disk, printer, CPASoftware refers to the programs that describe the steps we want the computer to perform.

© Janice Regan, CMPT 102, Sept. 2006 43

CPU

Computer Hardware

CPU –Central processing unit

ALU –Arithmetic and logic unit

ROM –Read only memory

RAM –Random access memory

InternalMemory

ExternalMemory

Input OutputProcessor

ALU

Cache/Memory

Page 23: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

23

© Janice Regan, CMPT 102, Sept. 2006 44

Software: Operating System

A system of computer programs that control the interaction of the user and the computer hardware. Examples: Linux, Windows, QNX,…Purposes of the operating system

Controls communication between input and output hardware and each process running in the computer.Directs and orders all operations of the computerAllocates and shares resources (memory,processor…)Controls access to resources and data (security)Provides a user interface (UI) and/or graphical user interface (GUI)Stores and accesses data and applications

© Janice Regan, CMPT 102, Sept. 2006 45

Example: Command Line UI

Windows Command Prompt

Page 24: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

24

© Janice Regan, CMPT 102, Sept. 2006 46

Example: Graphical UI (GUI)

Windows explorer

© Janice Regan, CMPT 102, Sept. 2006 47

Computer Software: Applications

Application Software (Software Tools)Word processors (Microsoft Word, WordPerfect, ...)Spreadsheet programs (Excel, Lotus1-2-3, ...)Computer gamesCommunication software (email, chat, web browser…)Telecommunication software (VOIP, …)

Page 25: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

25

© Janice Regan, CMPT 102, Sept. 2006 48

Computer Software: Languages

Some Computer LanguagesMachine language (machine instruction set)assembly languagehigh level languages

C, C++, Ada, Fortran, Basic, JavaDo YOU know of any others?mathematical computation tools (MATLAB, Mathematica, ...)

Application software is written using computer languages.

© Janice Regan, CMPT 102, Sept. 2006 49

Machine languageEach type of processor (like Pentium 4, Athalon, Z80, …) has its own instruction setEach instruction in an instruction set does a single thing like access a piece of data, add two pieces of data, compare two pieces of data …Each instruction is represented by a unique number This # may be different for different instruction sets, but no two instructions in the same instruction set will have the same #

Page 26: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

26

© Janice Regan, CMPT 102, Sept. 2006 50

Machine Language programs

In machine language a program is a list of instructions

Each instruction is represented by a numberInside the memory of the computer each number is represented in binary (as a constant length string of 1’s and 0’s)The long string of 0’s and 1’s is easy for the computer to understand and very difficult for a Human to read or write

© Janice Regan, CMPT 102, Sept. 2006 51

Assembler

Assembler languages make it easier for the programmer.

Assembler is easier for humans to read/write The numbers that identify each of the instructions in the instruction set are replaced with mnemonics like ADD, CMP, …The code, written using these mnemonics is written into a text file.

Page 27: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

27

© Janice Regan, CMPT 102, Sept. 2006 52

Assembler ProgramsThe code for an Assembler program is written into a text file.The computer read 1’s and 0’s not textHow do we translate to machine readable form?

A computer program called a compiler is used to translate the text file (called a source file) containing the assembler code into machine readable codeThe compiler writes a binary file containing the machine readable code (called an object file)

© Janice Regan, CMPT 102, Sept. 2006 53

Programs in High Level Languages

Assembler is easier to read/write than machine language. It is still very cumbersomeHigh level languages are easier to write than assembler

The compiler is more complex, but that is a tool you use, not one you write

In this course we will learn the language CThe rest of today's discussion is equally applicable to C or to any other compiled language (C++, Fortran, …)

Page 28: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

28

© Janice Regan, CMPT 102, Sept. 2006 54

Writing your program

Editor, Type inyour program

Source File

Text saved on hard disk

CompilerTranslate text into

Machine Readable code

© Janice Regan, CMPT 102, Sept. 2006 55

Source filesContains the text you type into a text editorThe text is a programThe program is a list of instructions written in a special Human readable language (C)The program can be translated, from the Human readable language (in source file) to a machine readable language (in object file), by a compilerA compiler is a special piece of software used to translate from source files to object files

Page 29: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

29

© Janice Regan, CMPT 102, Sept. 2006 56

Source files

Source File: Write using a text editor like KateDo not write your code using a word processor like Microsoft Word. A word processor will save in a special format.The compiler reads only text, not special formats.

Beware: your text does not differentiate between a word processor and an editor

© Janice Regan, CMPT 102, Sept. 2006 57

Perfect Code? Finding Errors 1It is highly unlikely than any of us will always write perfect code that contains no errorsHow do we find errors? Are there different kinds of errors? YesA computer program can contain syntax errors. semantic errors, or logical errorsWhen and how can we detect and correct errors?

Compile errorsLink errorsRun Time Errors

Page 30: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

30

© Janice Regan, CMPT 102, Sept. 2006 58

Syntax ErrorsA computer program can contain syntax errorsA computer language follows simple rules

how words and punctuation of different types may be combined.In English syntax is similar to grammatical structure

The compiler for a high level language can detect errors that break those simple of syntax (syntax errors)Syntax Errors are usually detected at compile time

© Janice Regan, CMPT 102, Sept. 2006 59

Semantic ErrorsA computer program can contain semantic errorsSemantics relates to the meaning of the words in a sentence or a computer language commandJust like a grammatically correct English sentence can be nonsense, a syntactically correct high level computer language command can also contain semantic errorsSome semantic errors may be found by the compiler, some will be found when the program is linked, some may be found at run time

Page 31: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

31

© Janice Regan, CMPT 102, Sept. 2006 60

Logical Errors

When your program completes but gives an unexpected answer it usually means there is a error in the logic in your solution of the problemLogic errors can also cause a program to fail part way through execution

© Janice Regan, CMPT 102, Sept. 2006 61

Writing your program

Editor, Type inyour programSource File

CompilerTranslation toMachine code

Syntax or Semantic Errors, Compiler Generates error Messages. To help us findand Correct errors in the

Source File

Object File

Binary,machine readable file

Syntactically Correct

code

Code with

syntax

Or semantic

errors

Editor, CorrectSyntax Errors

Page 32: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

32

© Janice Regan, CMPT 102, Sept. 2006 62

Perfect Code? Finding Errors 2If your code contains compile time errors is it correct? NOT NECESSARILYHow do we find the remaining errors? Use a tool that does more checks than the compiler (splint). Move on to linking the code to libraries etc.

The linker resolves references, words in your program than are defined elsewhereErrors occur when the definitions cannot be foundErrors occur when the use of the word does not correspond to the definition

© Janice Regan, CMPT 102, Sept. 2006 63

Linking your programEditor, Enter

program

Source File

(Text)

CompileFind Syntax

and SemanticErrors

Object File

(binary)

CorrectErrors

LinkerResolves

References among

object files

Other Object FilesLibraries …

Executable File(load module)

(binary)

Semantic Errors Reported: Finds words with no defined meaning.

Code has linker errors

Page 33: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

33

© Janice Regan, CMPT 102, Sept. 2006 64

Perfect Code? Finding Errors 3

If your code compiles and contains no errors that can be found by the linker (or splint) is it correct? NOT NECESSARILYHow do we find the remaining logic and semantic errors?When you run your executable program

it may not complete (may or may not generate error message)It may complete and give the wrong answer

© Janice Regan, CMPT 102, Sept. 2006 65

Loading/Running your programEditor, Enter

program

Source File

(Text)

CompileFind Syntax

and someSemanticErrors

Object File

(binary)

CorrectErrors

LinkerResolves

References

Other Object Files

Executable File(binary)

Link Errors Reported:

LoaderCopies

ExecutableAnd Runs

Input data

output resultsCPU

Page 34: CMPT 102 Introduction to Scientific Computer Programming · CMPT 102: Introduction to Scientific ... 6The C Programming Language Authors: ... DBefore you can use any of the computers

34

© Janice Regan, CMPT 102, Sept. 2006 66

Summary:Executing a Computer Program

CompilerConverts a source file (containing your human readable program in C) to and object file (computer readable binary file)

LinkerConverts object program to executable program

Compile Link/load ExecuteC languageProgram inText file(source file)

MachinelanguageProgram In binary file(object file)

Program output

Input data

Otherobject files

© Janice Regan, CMPT 102, Sept. 2006 67

Summary: Types of ErrorsSyntax errors

Errors in syntax, how words are combined and usedreported by the compiler or splint

Semantic errorsErrors in the meaning of words,

Reported by the linker (linker errors)Reported by the compiler (compile time errors)Found at execution time (run-time errors)

Logic errorsErrors causing the incorrect results, not reportedErrors causing program failure (run-time errors)