CSE / ENGR 142 Programming I

Similar documents
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)

Display Input and Output (I/O)

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

CPE 101 slides adapted from UW course. Overview. Chapter UW CSE H1-1. An Old Friend: Fahrenheit to Celsius. Concepts this lecture

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

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

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

What are the most likely declarations of "month" in the old and new versions of the program?

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

Fundamentals of Programming. Lecture 3: Introduction to C Programming

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

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

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

COP 3275: Chapter 02. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

Fundamentals of Programming Session 4

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

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

User Defined Functions

AMCAT Automata Coding Sample Questions And Answers

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

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

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

A Fast Review of C Essentials Part I

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

Chapter 11 Introduction to Programming in C

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

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

BLM2031 Structured Programming. Zeyneb KURT

Should you know scanf and printf?

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

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

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

Chapter 11 Introduction to Programming in C

Grade 6 Math Circles November 6 & Relations, Functions, and Morphisms

Chapter 11 Introduction to Programming in C

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

IS12 - Introduction to Programming. Data Types and Variables

Fundamental of Programming (C)

ET156 Introduction to C Programming

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

CSCI 2132 Software Development. Lecture 8: Introduction to C

COMP 202 Java in one week

3. Simple Types, Variables, and Constants

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Chapter 11 Introduction to Programming in C

CSE 374 Programming Concepts & Tools

Annotation Annotation or block comments Provide high-level description and documentation of section of code More detail than simple comments

COMP s1 Lecture 1

Topic 2: C++ Programming fundamentals

IT 1033: Fundamentals of Programming Data types & variables

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

CpSc 1111 Lab 4 Formatting and Flow Control

AP Computer Science Unit 1. Writing Programs Using BlueJ

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

CS-211 Fall 2017 Test 1 Version A Oct. 2, Name:

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

Fundamentals of Programming Session 8

Introduction to Computing Lecture 01: Introduction to C

Data Types & Variables

BSM540 Basics of C Language

Arrays in C C Programming and Software Tools. N.C. State Department of Computer Science

Use of scanf. scanf("%d", &number);

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

2. Numbers In, Numbers Out

Some Computer Preliminaries

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

C: How to Program. Week /Mar/05

Full file at

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

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

1.1 Introduction to C Language. Department of CSE

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

Fundamentals of Programming

Lesson 6A Loops. By John B. Owen All rights reserved 2011, revised 2014

Variables and Constants

Getting started with C++ (Part 2)

CS16 Week 2 Part 2. Kyle Dewey. Thursday, July 5, 12

Computing Seminar Introduction Oct

CMPE-013/L. Introduction to C Programming

Syntax and Variables

Arithmetic type issues

ENGG1811 Computing for Engineers Week 1 Introduction to Programming and Python

Use a calculator and c = 2 π r to calculate the circumference of a circle with a radius of 1.0.

AP Computer Science Unit 1. Writing Programs Using BlueJ

A brief intro to pointers for the purposes of passing reference parameters

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

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

CMSC 246 Systems Programming

These are notes for the third lecture; if statements and loops.

CS1 Lecture 3 Jan. 22, 2018

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2016

Transcription:

CSE / ENGR 142 Programming I Variables, Values, and Types Chapter 2 Overview Chapter 2: Read Sections 2.1-2.6, 2.8. Long chapter, short snippets on many topics Later chapters fill in detail Specifically: Types, variables, values Expressions, assignment Input / Output (scanf, printf) Programming style You ll learn enough to write a simple but useful C program! 1998 UW CSE 9/29/99-1 9/29/99-2 Review: What s a Computer? Disk Central Processing Unit Main Memory Monitor Keyboard Network mouse Memory and Data All data created and manipulated by a program is stored in the computer s memory We think of memory as being organized as a sequence of words. Each word has three attributes: its location or address (its name ) its value (the data stored in that location) its type (the kind of value stored there) 9/29/99-3 9/29/99-4 Memory and Data Variables Location Value 6: m' 5: 3.1416 4: -112 3: 214 2: 'y' 1: 3 location 5 holds the value 3.1416 location 3 holds the value 214 If we had to name memory locations explicitly by number we'd go crazy, so... In our programs we name memory symbolically, by defining variables. Each variable must be declared. The declaration indicates the name and the type of the variable. The type indicates how to interpret the value stored there. 9/29/99-5 9/29/99-6

Declaring Variables int months; Integer variables represent whole numbers: 1, 17, -32, 0 Not 1.5, 2.0 double pi; Floating point variables represent real numbers: 3.14, -27.5, 6.02e23, 5.0 Not 3 char first_initial, middle_initial, marital_status; Character variables represent individual keyboard characters: a, b, M, 0, 9, #, Not "ill" 9/29/99-7 Memory, Variables, Values, and Types Location Value Type Possible variable name 6: m' char first_initial 5: 3.1416 double almost_pi 4: -112 int my_balance 3: 214 int your_balance 2: 'y' char answer 1: 3 int Image3D 9/29/99-8 Identifiers "Identifiers" are names for things in a program for examples, names of variables In C, identifiers follow certain rules: use letters, numerals, and underscore ( _ ) do not begin with a numeral cannot be reserved words are "case-sensitive" can be arbitrarily long but... Style point: Good choices for identifiers can be extremely helpful in understanding programs Often useful: noun or noun phrase describing variable 9/29/99-9 contents Reserved words Certain words have a "reserved" (permanent, special) meaning in C We ve seen int, double, char already Will see a couple of dozen more eventually These words always have that special meaning, and cannot be used for other purposes. Cannot be used names of variables Must be spelled exactly right Sometimes also called keywords 9/29/99-10 Declarations vs Statements Programs are made up of declarations and statements Declarations declare or define something We ve already seen variable declarations Later we ll see constant declarations and function declarations Statements tell the CPU to do something We re about to see our first kind of statement Eventually we ll see about a dozen other kinds of statements Storing values in variables A variable declaration gives a name to a memory location, but how do we place a value in that memory location? Placing a value in a location is called "storing" One way to store a value is with the assignment statement. Later we ll see that scanf can also store values into a variable. 9/29/99-11 9/29/99-12

Assigning Values An assignment statement places a value into a variable. The assignment may specify a simple value to be stored, or an expression int area, length, width; length = 16; width = 32; area = length * width; /* declaration of 3 variables */ /* "length gets 16" */ /* "width gets 32" */ /* "area gets length times width" */ The result: store the value of the expression on the right into the variable on the left. 9/29/99-13 C Program Structure variable declarations program statements The shaded parts will be in every program (we ll explain later) 9/29/99-14 Problem Solving and Program Design (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 The test-debug cycle Maintain and update the program 9/29/99-15 Example Problem: Fahrenheit to Celsius Problem (specified): Convert Fahrenheit temperature to Celsius Algorithm (result of analysis): Celsius = 5/9 (Fahrenheit - 32) What kind of data (result of analysis): 9/29/99-16 Fahrenheit to Celsius (I) An actual C program celsius = (fahrenheit - 32.0) * 5.0 / 9.0; 9/29/99-17 Fahrenheit to Celsius (II) printf("enter a Fahrenheit temperature: "); scanf("%lf", &fahrenheit); celsius = (fahrenheit - 32.0) * 5.0 / 9.0; printf("that equals %f degrees Celsius.", celsius); 9/29/99-18

Running the Program Enter a Fahrenheit temperature: 45.5 That equals 7.500000 degrees Celsius Program trace: fahrenheit after declaration?? after first printf?? after scanf 45.5? after assignment 45.5 7.5 after second printf 45.5 7.5 celsius 9/29/99-19 Fahrenheit to Celsius (III) printf("enter a Fahrenheit temperature: "); scanf("%lf", &fahrenheit); celsius = fahrenheit - 32.0 ; celsius = celsius * 5.0 / 9.0 ; printf("that equals %f degrees Celsius.", celsius); 9/29/99-20 Assignment step-by-step my_age = my_age+1 celsius = celsius * 5.0 / 9.0 ; 1. Evaluate right-hand side a. Find current value of celsius 13.5 b. Multiply by 5.0 67.5 c. Divide by 9.0 7.5 2. Assign 7.5 to be the new value of celsius (old value 13.5 of celsius is lost.) 9/29/99-21 The same variable may appear on both sides of an assignment statement! my_age = my_age + 1 ; balance = balance + deposit ; The old value of the variable is used to compute the value of the expression, before the variable is changed. You wouldn t do this in math! 9/29/99-22 Note on lecture examples Slides often leave out important details my_age = my_age + 1; This is a legal C statement only if: my_age has previously been declared in the program my_age has a proper type (e.g. int) the statement occurs in a legal position; the full program has int main (void), etc., etc. Use your creative powers and common sense to deduce what s missing in the examples! 9/29/99-23 Initializing variables Initialization means giving something a value for the first time. Anything which changes the value of a variable is a potential way of initializing it. For now, that means assignment statement General rule: variables have to be initialized before their value is used. Failure to initialize is a common source of bugs. Variables in a C program are not automatically initialized to 0! 9/29/99-24

Does Terminology Matter? "variable", "reserved word", "initialization", "declaration", "assignment", etc., etc. You can write a complicated program without using these words ut you can t talk about your programs without them! Learn the exact terminology as you go, and get in the habit of using it. Your TAs, consultants, and tutors will bless you and will be able to better help you 9/29/99-25 Declaring vs Initializing int main (void) double income; income = 35500.00; /*declaration of income, not an assignment, not an initialization*/ /*assignment to income, initialization of income, not a declaration.*/ printf ("Old income is %f", income); income = 39000.00; /*assignment to income, not a declaration, not an initialization */ printf ("After raise: %f", income); 9/29/99-26 Compilers, Linkers, etc..c file header (stdio.h) c o m p i l e r 010 110 library (ANSI) l i n k e r executable program debugger source code object code 9/29/99-27