B-1 Lecture 2: Problems, Algorithms, and Programs © 2000 UW CSE University of Washington Computer Programming I.

Slides:



Advertisements
Similar presentations
Chapter 4 Computation Bjarne Stroustrup
Advertisements

CSE 105 Structured Programming Language (C)
ICS103 Programming in C Lecture 1: Overview of Computers & Programming
Lecture 1: Overview of Computers & Programming
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
COSC 120 Computer Programming
Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
1 Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
Wednesday, 9/4/02, Slide #1 1 CS 106 Intro to CS 1 Wednesday, 9/4/02  Today: Introduction, course information, and basic ideas of computers and programming.
C-1 University of Washington Computer Programming I Lecture 3: Variables, Values, and Types © 2000 UW CSE.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
Introduction to a Programming Environment
Introduction to C Programming
C programming Language and Data Structure For DIT Students.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
CCSA 221 Programming in C CHAPTER 2 SOME FUNDAMENTALS 1 ALHANOUF ALAMR.
Chapter Introduction to Computers and Programming 1.
© Janice Regan, CMPT 128, Jan CMPT 128 Introduction to Computing Science for Engineering Students Creating a program.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Chapter 1: Introduction to Computers and Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Programming With C.
Input, Output, and Processing
Chapter 1 Introduction to Computers and C++ Programming Goals: To introduce the fundamental hardware and software components of a computer system To introduce.
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 1- 1 October 20, October 20, 2015October 20, 2015October 20,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CS 127 Introduction to Computer Science. What is a computer?  “A machine that stores and manipulates information under the control of a changeable program”
Chapter 1 Computers, Compilers, & Unix. Overview u Computer hardware u Unix u Computer Languages u Compilers.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
E-1 University of Washington Computer Programming I Lecture 5: Input and Output (I/O) © 2000 UW CSE.
9/29/99B-1 CSE / ENGR 142 Programming I Variables, Values, and Types © 1998 UW CSE.
G3-1 University of Washington Computer Programming I Structuring Program Files © 2000 UW CSE.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Getting ready. Why C? Design Features – Efficiency (C programs tend to be compact and to run quickly.) – Portability (C programs written on one system.
Brief Version of Starting Out with C++ Chapter 1 Introduction to Computers and Programming.
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
© Peter Andreae Java Programs COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
Chapter 1: Introduction to Computers and Programming.
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
Software Engineering Algorithms, Compilers, & Lifecycle.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Engineering Problem Solving With C An Object Based Approach
Chapter 2 - Introduction to C Programming
ICS103 Programming in C Lecture 1: Overview of Computers & Programming
Revision Lecture
Getting Started with C.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Tonga Institute of Higher Education IT 141: Information Systems
Chapter 2 - Introduction to C Programming
Tonga Institute of Higher Education IT 141: Information Systems
University of Washington Computer Programming I
An Overview of C.
Introduction to C Programming
Presentation transcript:

B-1 Lecture 2: Problems, Algorithms, and Programs © 2000 UW CSE University of Washington Computer Programming I

B-2 Overview High-level survey Problems, algorithms, and programs Problem solving and program design Compiling and running a C program Errors and debugging Focus on the big ideas Many details to cover in future lectures Reading Sec ; start reading Ch. 2.

B-3 Key Definitions/Concepts Problem Definition of task to be performed (often by a computer) Algorithm A particular sequence of steps that will solve a problem Steps must be precise and mechanical The notion of an algorithm is a (the?) fundamental intellectual concept associated with computing Program An algorithm expressed in a specific computer programming language (C, C++, Java, Perl, …)

B-4 Programming vs. Cooking ProgrammingCooking ProblemMake fudge brownies AlgorithmRecipe ProgramRecipe written in a specific language (English, Russian, Chinese, Latvian, etc.)

B-5 Problem Solving (review) Clearly specify the problem Analyze the problem Design an algorithm to solve the problem Implement the algorithm (write the program) Test and verify the completed program

B-6 A Sample Problem Is a given number even or odd?

B-7 Analysis What numbers are allowed? Where does the number come from? What do “even” and “odd” mean? How is the answer to be reported?

B-8 More Precise Problem Restatement Given an integer number typed in from the keyboard, If it is even, write “even” on the screen If it is odd, write “odd” on the screen

B-9 An Algorithm Read in the number Divide the number by 2 If the remainder is 0, write “even” Otherwise, write “odd”

B-10 An Algorithm Read in the number Divide the number by 2 If the remainder is 0, write “even” Otherwise, write “odd” Test:

B-11 An Algorithm Read in the number Divide the number by 2 If the remainder is 0, write “even” Otherwise, write “odd” Test: An alternate algorithm: If the rightmost digit is 0, 2, 4, 6, or 8, write “even” Otherwise, write “odd”

B-12 Next, a C Program Now that have an algorithm, we would like to write a C program to carry it out. But first, what is a program? In fact, what is a computer?

B-13 What’s a Computer? CPU or processor: executes simple instructions manipulating values in memory Central Processing Unit (CPU) Main Memory Monitor Network Disk Keyboard mouse

B-14 What is a Program? The CPU executes instructions one after the other. Such a sequence of instructions is called a “program” Without a program, the computer is just useless hardware Complex programs may contain millions of instructions

B-15 Memory Memory is a collection of locations called variables Each variable has A name (an identifier) A type (the kind of information it can contain) Basic types include int (integers – whole numbers: 17, -42) double (floating-point numbers with optional fraction and/or exponent: , 6.02e23) char (character data: ‘a’, ‘?’, ‘N’, ‘ ’, ‘9’)

B-16 Program Sketch Ask the user to enter a number Read the number and call it num Divide num by 2 and call the remainder rem If rem is 0 write “even” otherwise write “odd” The actual program has LOTS of details – IGNORE THEM FOR NOW Pay attention to the main ideas

B-17 The Program in C (part I) /* read a number and report whether it is even or odd */ #include int main (void) { int num; /* input number */ int rem; /* remainder after division by 2 */ /* get number from user */ printf(“Please enter a number: ”); scanf(“%d”, &num);

B-18 The Program in C (part II) /* calculate remainder and report even or odd */ rem = num % 2; if (rem == 0) { printf(“even\n”); } else { printf(“odd\n”); } /* terminate program */ return 0; } Remember: Don’t sweat the details!!! (for now)

B-19 Sample Execution

B-20 A Quick Look at the Program Text surrounded by /* and */ are comments Used to help the reader understand the program Ignored during program execution Programs change over time. It’s important that programmers be able to understand old code - good comments are essential. /* read a number … */ #include int main (void) { int num; /* input number */ int rem; /* remainder …*/ /* get number from user */ printf(“Please enter a number: ”); scanf(“%d”, &num); /* calculate remainder … */ rem = num % 2; if (rem == 0) { printf(“even\n”); } else { printf(“odd\n”); } /* terminate program */ return 0; }

B-21 Variables Variable declarations create new variables and specify their names and types. /* read a number … */ #include int main (void) { int num; /* input number */ int rem; /* remainder …*/ /* get number from user */ printf(“Please enter a number: ”); scanf(“%d”, &num); /* calculate remainder … */ rem = num % 2; if (rem == 0) { printf(“even\n”); } else { printf(“odd\n”); } /* terminate program */ return 0; }

B-22 Statements Following the declarations are statements that specify the operations the program is to carry out Lots of different kinds Some (if, else, return) are part of the C language proper Others (scanf, printf) are contained in libraries of routines that are available for use in our programs For now, don’t worry too much about the distinction /* read a number … */ #include int main (void) { int num; /* input number */ int rem; /* remainder …*/ /* get number from user */ printf(“Please enter a number: ”); scanf(“%d”, &num); /* calculate remainder … */ rem = num % 2; if (rem == 0) { printf(“even\n”); } else { printf(“odd\n”); } /* terminate program */ return 0; }

B-23 Functions Functions are sequences of statements defined elsewhere. Some functions (such as printf and scanf here) are provided with the system. We will also learn how to write and use our own functions. /* read a number … */ #include int main (void) { int num; /* input number */ int rem; /* remainder …*/ /* get number from user */ printf(“Please enter a number: ”); scanf(“%d”, &num); /* calculate remainder … */ rem = num % 2; if (rem == 0) { printf(“even\n”); } else { printf(“odd\n”); } /* terminate program */ return 0; }

B-24 Boilerplate Some parts of the program are standard utterances that need to be included at the beginning and end. We’ll explain all of this eventually Just copy it for now in each of your programs /* read a number … */ #include int main (void) { int num; /* input number */ int rem; /* remainder …*/ /* get number from user */ printf(“Please enter a number: ”); scanf(“%d”, &num); /* calculate remainder … */ rem = num % 2; if (rem == 0) { printf(“even\n”); } else { printf(“odd\n”); } /* terminate program */ return 0; }

B-25 From C to Machine Language The computer’s processor only understands programs written in its own machine language Sequences of 1’s and 0’s Different for each processor family (x86, PowerPC, SPARC, ARM, …) How is it that it can obey instructions written in C?

B-26 Compilers and Linkers There are two steps in creating an executable program starting from C source code compiler A program called the C compiler translates the C code into an equivalent program in the processor’s machine language (1’s and 0’s) linker A program called the linker combines this translated program with any library files it references (printf, scanf, etc.) to produce an executable machine language program (.exe file) Environments like Visual Studio do both steps when you “build” the program

B-27 Compilers, Linkers, etc. library (ANSI) header (stdio.h) executable program compilercompiler linkerlinker source code object code. c file

B-28 What Could Possibly Go Wrong? Lots! Things are rarely perfect on the first attempt Both the compiler and linker could detect errors Even if no errors are are detected, logic errors (“bugs”) could be lurking in the code Getting the bugs out is a challenge even for professional software developers

B-29 Terms: Syntax vs Semantics Syntax: Syntax: the required form of the program punctuation, keywords (int, if, return, …), word order, etc. The C compiler always catches these “syntax errors” or “compiler errors” Semantics (logic): Semantics (logic): what the program means what you want it to do The C compiler cannot catch these kinds of errors! They can be extremely difficult to find They may not show up right away

B-30 Try It Yourself! Type in the even/odd program and see what happens when you: Leave off a few semicolons or misspell something (syntax) In the last printf statements, change “odd” to “even”. Run the program. What happens if you enter 17? (semantics) Experiment and see what happens

B-31 Wow!! We’ve covered a lot of new ideas Algorithms and programs Computer organization and memory The basic components of C programs Comments, declarations, statements Compilers, linkers, libraries, and program execution Errors Lots of terminology, too

B-32 What’s Next? Upcoming lectures: review what we’ve seen today and fill in details Meanwhile, get started reading and trying things on the computer!