CPE 101. Overview. Programming vs. Cooking. Key Definitions/Concepts B-1

Similar documents
CSE / ENGR 142 Programming I

C-1. Overview. CSE 142 Computer Programming I. Review: Computer Organization. Review: Memory. Declaring Variables. Memory example

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

C: How to Program. Week /Mar/05

Chapter 2 - Introduction to C Programming

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Chapter 11 Introduction to Programming in C

Display Input and Output (I/O)

Variables, Data Types, and Arithmetic Expressions Learning Objectives:

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

Chapter 11 Introduction to Programming in C

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 1

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

printf( Please enter another number: ); scanf( %d, &num2);

AMCAT Automata Coding Sample Questions And Answers

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Chapter 11 Introduction to Programming in C

Midterms Save the Dates!

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Chapter 11 Introduction to Programming in C

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

A Fast Review of C Essentials Part I

3. Simple Types, Variables, and Constants

Chapter 1 & 2 Introduction to C Language

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Chapter 11 Introduction to Programming in C

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

CS110: PROGRAMMING LANGUAGE I

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

CS16 Exam #1 7/17/ Minutes 100 Points total

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

BLM2031 Structured Programming. Zeyneb KURT

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

Chapter 11 Introduction to Programming in C

Unit 7. Functions. Need of User Defined Functions

C++ Support Classes (Data and Variables)

Chapter 11 Introduction to Programming in C

Full file at C How to Program, 6/e Multiple Choice Test Bank

Chapter 1: Why Program? Main Hardware Component Categories 8/23/2014. Main Hardware Component Categories: Why Program?

CSE 374 Programming Concepts & Tools

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Fundamentals of Programming. Lecture 3: Introduction to C Programming

COMP s1 Lecture 1

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

Lecture 3 Tao Wang 1

Expressions and Casting

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

2. Numbers In, Numbers Out

COSC121: Computer Systems: Runtime Stack

Program Organization and Comments

Computer Software: Introduction

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Introduction to Computing Lecture 01: Introduction to C

EL2310 Scientific Programming

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Getting started with C++ (Part 2)

Chapter 1: Introduction to Computers and Programming

Memory, Data, & Addressing I

Chapter 1 Getting Started Structured Programming 1

WARM UP LESSONS BARE BASICS

CSCI 2132 Software Development. Lecture 8: Introduction to C

Computer System and programming in C

Lecture 5: C programming

H192 Midterm 1 Review. Tom Zajdel

SU2017. LAB 1 (May 4/9) Introduction to C, Function Declaration vs. Definition, Basic I/O (scanf/printf, getchar/putchar)

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #13. Loops: Do - While

CS102: Variables and Expressions

Fundamentals of Programming Session 4

Motivation was to facilitate development of systems software, especially OS development.

CSE 1320 INTERMEDIATE PROGRAMMING - OVERVIEW AND DATA TYPES

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

CS1100 Introduction to Programming

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

BM214E Object Oriented Programming Lecture 4

COMP163. Introduction to Computer Programming. Introduction and Overview of the Hardware

Lecture 03 Bits, Bytes and Data Types

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

CS113: Lecture 4. Topics: Functions. Function Activation Records

An Introduction to Python (TEJ3M & TEJ4M)

Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1 Lecture 7. COMP s1. Before we begin. Strings. Programming Fundamentals. Overview. Andrew Bennett

Lecture 4 CSE July 1992

Motivation was to facilitate development of systems software, especially OS development.

Introduction to Computing Lecture 09: Functions (Part II)

CMSC 104 -Lecture 6 John Y. Park, adapted by C Grasso

CSE 303 Lecture 8. Intro to C programming

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

2. Numbers In, Numbers Out

An Introduction to MATLAB

B+ Tree Review. CSE332: Data Abstractions Lecture 10: More B Trees; Hashing. Can do a little better with insert. Adoption for insert

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Lecture 12 Modular Programming with Functions

Algorithms and Programming I. Lecture#12 Spring 2015

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Transcription:

CPE 101 Lecture 2: Problems, Algorithms, and Programs (Slides adapted from a UW course, copyrighted and used by permission) 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 2000 UW CSE B-1 B-2 Key Definitions/Concepts Programming vs. Cooking 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, ) Programming Problem Algorithm Program Cooking Make fudge brownies Recipe Recipe written in a specific language (English, Russian, Chinese, Latvian, etc.) B-3 B-4 B-1

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 A Sample Problem Is a given number even or odd? B-5 B-6 Analysis What numbers are allowed? Where does the number come from? What do even and odd mean? How is the answer to be reported? 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-7 B-8 B-2

An Algorithm Read in the number Divide the number by 2 If the remainder is 0, write even Otherwise, write odd An Algorithm Read in the number Divide the number by 2 If the remainder is 0, write even Otherwise, write odd Test: 234784832792543 B-9 B-10 An Algorithm Read in the number Divide the number by 2 If the remainder is 0, write even Otherwise, write odd Test: 234784832792543 An alternate algorithm: If the rightmost digit is 0, 2, 4, 6, or 8, write even Otherwise, write odd B-11 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-12 B-3

What s a Computer? Disk Central Processing Unit (CPU) Main Memory Monitor Keyboard Network mouse CPU or processor: executes simple instructions manipulating values in memory B-13 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 B-14 instructions 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: 3.14159, 6.02e23) char (character data: a,?, N,, 9 ) B-15 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-16 B-4

The Program in C (part I) /* read a number and report whether it is even or odd */ int rem; /* remainder after division by 2 */ B-17 The Program in C (part II) /* calculate remainder and report even or odd */ else { Remember: Don t sweat the details!!! (for now) B-18 Sample Execution B-19 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. else { B-20 B-5

Variables Variable declarations create new variables and specify their names and types. else { B-21 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 else { B-22 Functions Boilerplate 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. else { B-23 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 else { B-24 B-6

From C to Machine Language Compilers and Linkers 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-25 B-26 Compilers, Linkers, etc. What Could Possibly Go Wrong?.c file header (stdio.h) source code c o m p i l e r 010 110 library (ANSI) object code l i n k e r executable program debugger 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-27 B-28 B-7

Terms: Syntax vs Semantics 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-29 B-30 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 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! Lots of terminology, too B-31 B-32 B-8