Computer Programming: Skills & Concepts (CP) Variables and ints

Similar documents
Summary of Lecture 4. Computer Programming: Skills & Concepts (INF-1-CP1) Variables; scanf; Conditional Execution. Tutorials.

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

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

COMP s1 Lecture 1

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

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

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

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

CpSc 1111 Lab 4 Formatting and Flow Control

Welcome! COMP s1. Programming Fundamentals

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

BSM540 Basics of C Language

scanf erroneous input Computer Programming: Skills & Concepts (CP) Characters Last lecture scanf error-checking our input This lecture

Programming Language B

Structured programming

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

CpSc 1111 Lab 5 Formatting and Flow Control

Introduction to Computing Lecture 03: Basic input / output operations

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

Hacking C Code - Local Machine

CP Lab 4: Simple graphics with descartes

This Lecture. Computer Programming: Skills & Concepts (INF-1-CP1) Intro to Practical 1; more on scanf. Summary of Lecture 6. descartes.

Programming Language A

BLM2031 Structured Programming. Zeyneb KURT

EL2310 Scientific Programming

Functions. Arash Rafiey. September 26, 2017

What we have learned so far

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point?

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

CP Lab 1: Introduction to DICE and the C compiler

Introduction: The Unix shell and C programming

Computer Programming: Skills & Concepts (INF-1-CP1) Intro to Practical 1. 5th October, 2010

PDS: CS Computer Sc & Engg: IIT Kharagpur 1. for Statement

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

Getting started in C

Welcome! COMP s1. Programming Fundamentals

The C language. Introductory course #1

Chapter 7 Functions. Now consider a more advanced example:

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

Physics 306 Computing Lab 1: Hello, World!

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

2. Numbers In, Numbers Out

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

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

The Compilation Process

EC 413 Computer Organization

NCSU ECE 209 Sections 602 Exam 1 Fall September, I have neither given not received unauthorized assistance on this test.

CP Lab 5: Functions, pointers, some arrays

CP Lab 5: Functions, pointers, some arrays

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

Computer Programming

CSE 303 Lecture 8. Intro to C programming

DECISION CONTROL AND LOOPING STATEMENTS

Functions Introduction to Algorithms and Programming II School of Computer Science Term: Summer 2013 Instructor: Dr. Asish Mukhopadhyay

Welcome! COMP s1. Programming Fundamentals

CSC 270 Survey of Programming Languages

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

ET156 Introduction to C Programming

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

Programming for Electrical and Computer Engineers. Loops

C and Programming Basics

1. The Mac Environment in Sierra Hall 1242

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Decision Making and Loops

CC112 Structured Programming

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

ECE264 Fall 2013 Exam 1, September 24, 2013

2. Numbers In, Numbers Out

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

9/10/2016. Time for Some Detailed Examples. ECE 120: Introduction to Computing. Let s See How This Loop Works. One Statement/Step at a Time

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

1. The Mac Environment in SIE 1222

Introduction to Computers II Lecture 4. Dr Ali Ziya Alkar Dr Mehmet Demirer

Lectures 4 and 5 (Julian) Computer Programming: Skills & Concepts (INF-1-CP1) double; float; quadratic equations. Practical 1.

Computer Programming: Skills & Concepts (CP) Strings

Exercise: Inventing Language

CSCI 2132 Software Development. Lecture 18: Functions

Debugging (Part 1) The material for this lecture is drawn, in part, from The Practice of Programming (Kernighan & Pike) Chapter 5

Language comparison. C has pointers. Java has references. C++ has pointers and references

Government Polytechnic Muzaffarpur.

Lecture 2: C Programming Basic

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

Flow Chart. The diagrammatic representation shows a solution to a given problem.

Introduction to C An overview of the programming language C, syntax, data types and input/output

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

EL2310 Scientific Programming

CS133 C Programming. Instructor: Jialiang Lu Office: Information Center 703

Introduction to C Programming

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Lecture 3. More About C

Outline Arrays Examples of array usage Passing arrays to functions 2D arrays Strings Searching arrays Next Time. C Arrays.

Lecture 6. Statements

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

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Subject: Fundamental of Computer Programming 2068

Chapter 3: Arrays and More C Functionality

Transcription:

CP Lect 3 slide 1 25 September 2017 Computer Programming: Skills & Concepts (CP) Variables and ints C. Alexandru 25 September 2017

CP Lect 3 slide 2 25 September 2017 Week 1 Lectures Structure of the CP course What is programming? Imperative Programming? C programming? recipe analogy to imperative programming. Hello World in detail.

Today s lecture Variables and change-of-state Our Squaring program Finding the maximum. Updating/Assigning variables CP Lect 3 slide 3 25 September 2017

CP Lect 3 slide 4 25 September 2017 Variables For most computational tasks, the result (or output) will depend on some input hello.c was unusual (no input, always does the same thing); The mypoem.c program you write in your first lab is similarly unusual. If we read input, we need to store it somewhere to keep a record of its value. We store inputs in objects called Variables. The word Variable means changing no fixed value; like a blackboard or slate where we can store one value, and change it later.

Program Environment in general In this environment we have two variables x and y. They are shown as being un-initialized (no particular value). CP Lect 3 slide 5 25 September 2017

CP Lect 3 slide 6 25 September 2017 Variables and Imperative programming Imperative programming ie carried out as a sequence of step-by-step commands; Some commands will fetch input, some will send output, most commands will change the state of the program environment by changing a variable; Variables can appear in a few ways: (i) At the start of a command, with an =, for example: x =... This is an assignment command, used to put a value (whatever appears on the right) into the variable x. (ii) Combined inside some other expression like:...(x+2)*4... OR...5*x-3... This is an evaluation of the expression, where the value of x is looked up and used in evaluation. Evaluation in C is done as call-by-value (more later)

CP Lect 3 slide 7 25 September 2017 Defining variables Usually have one variable for each piece of input. May have other variables besides the variables storing inputs. May want/need to give names to (and store) important quantities; May be helpful to store certain quantities for re-use; As the computational problems get more complex/interesting, more necessity for interesting choice of variables. In C, and most other programming languages, variables must have a fixed type (more later...). In C, a variable name starts with a letter, and can be made up of letters (a z, A Z), digits (0 9) and underscores ( ). Actually, it can also start with, but by convention this is only done for private variables that are internal to a particular code package.

CP Lect 3 slide 8 25 September 2017 Squaring a Number input: Ask the user to input a (integer) number. problem-solving: Compute the square of the integer. output: Tell the user what the squared value is. We will need at least one variable, to store the number input, because this value varies. We will choose to have an extra variable to store the squared value.

Variables for square.c Must declare variables in advance of using them. int x, y; Variable declaration is terminated by a semi-colon. Says Make two integer variables x and y available for computation (in the program environment). Called a declaration (not a command). Declaration must come before the variable is ever used in a command (and preferably just inside the start of main) CP Lect 3 slide 9 25 September 2017

CP Lect 3 slide 10 25 September 2017 Input with scanf scanf is the twin of printf. Reads numbers from input and stores them in variables. But scanf requires a & before its arguments. (Explanation later in the course... for now it s just magic.) For example: int x, y; scanf("%d", &x);

CP Lect 3 slide 11 25 September 2017 Doing the Squaring The scanf("%d", &x); statement will read the user-provided value into the variable x. We need to compute the value of this number squared. The expression x*x represents the value of x squared. We can store this squared value in the variable y using an assignment command y = x*x; Then squared value is ready to print out.

CP Lect 3 slide 12 25 September 2017 square.c #include <stdlib.h> #include <stdio.h> int main(void) { int x; int y; printf("input the integer: "); scanf("%d", &x); y = x*x; printf("the square of %d is %d.\n", x, y); return EXIT_SUCCESS; }

Program Environment for square.c CP Lect 3 slide 13 25 September 2017

Program Environment for square.c cont. CP Lect 3 slide 14 25 September 2017

CP Lect 3 slide 15 25 September 2017 Finding the Maximum input: Ask the user for two integers, one at a time. problem-solving: Find the larger of the two integers. output: Output the larger of the two. We will need at least two variables, to store the inputs.

CP Lect 3 slide 16 25 September 2017 Variables and scanf for Maximum Declare variables in advance of using them. int x, y, m; Says Make three integer variables x, y and m available for computation (in the program environment). Now we can store values in them. int x, y, m; scanf("%d", &x); scanf("%d", &y);

MAX of two integer variables if (x > y) { m = x; } else { m = y; } printf("max is %d: ", m); (x > y) is the condition to be evaluated. It evaluates to True only if x is larger than y. where did we get the values x and y? CP Lect 3 slide 17 25 September 2017

max.c #include <stdlib.h> #include <stdio.h> int main(void) { int x, y, m; printf("input the two integers: "); scanf("%d", &x); scanf("%d", &y); if (x > y) { m = x; } else { m = y; } printf("max is %d: ", m); return EXIT_SUCCESS; } CP Lect 3 slide 18 25 September 2017

Variables in C Variables are boxes to store a value A C variable holds a single value; Have to define what type of item a variable will hold, eg: int x; or maybe int x = 2; In C, the value can change over time as a result of program statements which act on the variable, eg: x = x + 1; CP Lect 3 slide 19 25 September 2017

CP Lect 3 slide 20 25 September 2017 int n; Updating Variables n is declared but not given a value! n = 2 * n; n is doubled (from what? ERROR) n = 9; n gets the value 9 n = n + 1; n gets the value 9+1, i.e. 10 n = 22 * n + 1; n gets the value?

CP Lect 3 slide 21 25 September 2017 The Assignment Statement A variable is updated by an assignment statement n = 22 * n + 1; The left-hand side n is the variable being updated. The right-hand side 22 * n + 1 is an expression for the new value. First compute the expression, then change the variable to the new value.

CP Lect 3 slide 22 25 September 2017 Reading Assignment Note: Reading assignments point out the sections in our recommended book that discuss what we ve done in the lecture. If you re using a different book, then find the appropriate section in it. If you re not using a book, and want more info, google some keywords! Read sections 1.1, 1.2, 1.3, 1.5 and 1.6 (all in Chapter 1) of A Book on C (skip 1.4).