Programming Language A

Similar documents
Programming Language A

Programming Language B

Programming Language B

Programming Language A

Programming Language B

Programming Language A

Structured programming. Exercises 3

Programming Language B

WARM UP LESSONS BARE BASICS

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

Arithmetic Expressions in C

Programming Language B

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

Programming Language B

Lecture 6. Statements

C Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); }

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

EECE.2160: ECE Application Programming Spring 2016 Exam 1 Solution

Programming Language B

Expressions and Casting

Operators and expressions. (precedence and associability of operators, type conversions).

Day06 A. Young W. Lim Wed. Young W. Lim Day06 A Wed 1 / 26

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

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

C Program Structures

CS Spring 2018 Homework #5

1. The keyword main in C language is used for

CS Fall 2007 Homework #5

Basic Assignment and Arithmetic Operators

Informatica e Sistemi in Tempo Reale

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

EECE.2160: ECE Application Programming Fall 2017

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

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

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

ANSI C Programming Simple Programs

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

C LANGUAGE A Short Course

Introduction to C. Systems Programming Concepts

Q1: Multiple choice / 20 Q2: C input/output; operators / 40 Q3: Conditional statements / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

Fundamentals of Programming

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Formatted Input/Output

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

Lecture 3. More About C

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

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

ET156 Introduction to C Programming

Fundamental of Programming (C)

F28HS2 Hardware-Software Interface. Lecture 1: Programming in C 1

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

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

Data Type Fall 2014 Jinkyu Jeong

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

C Programs: Simple Statements and Expressions

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

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Programming in C Quick Start! Biostatistics 615 Lecture 4

Display Input and Output (I/O)

2. Numbers In, Numbers Out

Programming and Data Structures

Introduction To Computer Programming Laboratory Source code of a computer program is given below. What do you expect to be displayed?

Fundamental of Programming (C)

COMP1917 Computing 1 Written Exam Sample Questions

Question 1. Part (a) [2 marks] error: assignment of read-only variable x ( x = 20 tries to modify a constant) Part (b) [1 mark]

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

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

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

Engineering program development 6. Edited by Péter Vass

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

Programming for Engineers Iteration

CSE 303 Lecture 8. Intro to C programming

1.3b Type Conversion

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Arithmetic type issues

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

Q1: C input/output; operators / 46 Q2: Conditional statements / 34 Q3: While and do-while loops / 20 TOTAL SCORE / 100 Q4: EXTRA CREDIT / 10

The Hyderabad Public School, Begumpet, Hyderabad, A.P

EECE.2160: ECE Application Programming Spring 2018

BSM540 Basics of C Language

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

SECTION A TRUE / FALSE QUESTIONS (10 MARKS) (INSTRUCTION: Please answer all 10 questions)

Course Outline Introduction to C-Programming

C Programming

Chapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops.

Procedural Programming

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

More examples for Control statements

2. Numbers In, Numbers Out

Stream Model of I/O. Basic I/O in C

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Selection Statements. Pseudocode

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

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

BİL200 TUTORIAL-EXERCISES Objective:

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Transcription:

Programming Language A Takako Nemoto (JAIST) 22 October Takako Nemoto (JAIST) 22 October 1 / 28

From Homework 2 Homework 2 1 Write a program calculate something with at least two integer-valued inputs, which is not appeared in this lecture. Add comments to explain what is calculated in the program. 2 Send the program to me via e-mail, with the title Homework Lecture2. Please make sure to write your name and student ID number in the mail. Examples etc.. absolute value; area of a triangle; exponentiation; Takako Nemoto (JAIST) 22 October 2 / 28

Absolute value int main(void) { int n1, n2; puts("input two integers."); printf("integer 1: "); scanf("%d", &n1); printf("integer 2: "); scanf("%d", &n2); printf("the sum of them is %d.\n", abs(n1 - n2)); Depending on compiler, you may have an error at compiling. This is because the absolute value function abs is not in stdio.h but in stdlib.h. You will not have an error if you add #include <stdlib.h>. Takako Nemoto (JAIST) 22 October 3 / 28

Operators and operand Binary operators + plus - minus * multiplication / quotient % reminder Example int x, y; puts("input two integers"); printf("integer x: "); scanf ("%d", &x); printf("integer y: "); scanf ("%d", &y); printf("x + y is %d\n", x + y); printf("x - y = %d\n", x - y); printf("x * y = %d\n", x * y); printf("x / y = %d\n", x / y); printf("x %% y = %d\n", x % y); Input two integers Integer x: 9 Integer y: 8 x + y = 17 x + y = 1 x * y = 72 x / y = 1 x % y = 1 Takako Nemoto (JAIST) 22 October 4 / 28

Operators and operand Unary operators unary + operator: + unary operator: - Example int x; printf("input an integer x: "); scanf("%d", &x); printf("+x is %d and -x is %d\n", +x, -x); Input an integer x: 9 +x is 9 and -x is -9 %d can be used several times in one statement. The 1st %d corresponds to the 2nd argument +x and the 2nd %d to the 3rd argument -x. Takako Nemoto (JAIST) 22 October 5 / 28

Types Example int main(void) { int n1, n2; printf("input the width: "); scanf("%d",&n1); printf("input the height: "); scanf("%d",&n2); printf("the area of this triangle is %d.\n", n1 * n2 / 2); Input the lwidth: 3 Input the height: 5 The area of this triangle is 7. The last statements calls the integer-value n1 * n2 / 2. Therefore the fraction part is omitted. Takako Nemoto (JAIST) 22 October 6 / 28

Types Example int main(void) { double n1, n2; printf("input the width: "); scanf("%lf",&n1); printf("input the height: "); scanf("%lf",&n2); printf("the area of this triangle is %f.\n", n1 * n2 / 2); Input the width: 3 Input the height: 5 The area of this triangle is 7.500000. double is the type for floating point decimal. Note that the conversion specifications are %f in printf and %lf in scanf, both for value with the type double. Takako Nemoto (JAIST) 22 October 7 / 28

Type and operators In principle, the output of the operation has the same type as its inputs. If the inputs have different types, then the result has superior type. Example: 5/2 with types int and double 5 int / 2 int = 2 int 5 / 2.0 = 2.5 int double double 5.0 / 2.0 = 2.5 double double double 5.0 / 2 = 2.5 double int double Takako Nemoto (JAIST) 22 October 8 / 28

Example of typing int n1, n2, n3, n4; double d1, d2, d3, d4; n1 = 5 / 2; n2 = 5.0 / 2.0; n3 = 5.0 / 2; n4 = 5 / 2.0; d1 = 5 / 2; d2 = 5.0 / 2; d3 = 5.0 / 2; d4 = 5 / 2.0; printf("n1 = %d\n", n1); printf("n2 = %d\n", n2); printf("n3 = %d\n", n3); printf("n4 = %d\n", n4); n1 = 2 n2 = 2 n3 = 2 n4 = 2 d1 = 2.000000 d2 = 2.500000 d3 = 2.500000 d4 = 2.500000 printf("d1 = %f\n", d1); printf("d2 = %f\n", d2); printf("d3 = %f\n", d3); printf("d4 = %f\n", d4); Takako Nemoto (JAIST) 22 October 9 / 28

Example of a type error int n, m; double x, y, z, w; printf("input 4 positive integers.\n"); printf("n = "); scanf("%d", &n); printf("m = "); scanf("%d", &m); printf("x = "); scanf("%lf", &x); printf("y = "); scanf("%lf", &y); z = n / m; w = x / y; Input 4 integers. n = 5 m = 2 x = 5 y = 2 n / m is 0 x / y is 2.500000 printf("n / m is %d\n", z); printf("x / y is %f\n", w); Takako Nemoto (JAIST) 22 October 10 / 28

As usual arithmetic, * is stronger than +, so we have to write (a+b). %2.1f specifies to print the floating-point-valued (a + b) / 2.0 with at least 2 digits, including 1 fraction part. See p.36 of the textbook. What do we have if we replace 2.0 with 2? Takako Nemoto (JAIST) 22 October 11 / 28 Type and operators int a, b; puts("input two integers."); printf("a = "); scanf("%d", &a); printf("b = "); scanf("%d", &b); Input two integers. a = 5 b = 2 The average is 3.5 printf("the average is %2.1f", (a + b) / 2.0);

Cast In the previous example, we can declare that (a + b) / 2.0 has the floating-point value as follows: int a, b; puts("input two integers."); printf("a = "); scanf("%d", &a); printf("b = "); scanf("%d", &b); printf("the average is %2.1f", (double) (a + b) / 2.0); Takako Nemoto (JAIST) 22 October 12 / 28

Exponentiation Some operator has a specific type of output. #include <math.h> double pow(double x, double y); int x, y; printf("input an integer x: "); scanf("%d", &x); printf("input an integer y: "); scanf("%d", &y); printf("exp(x, y) is %lf.", pow(x,y)); /*show the yth power of x*/ Integer x: 5 Integer y: 3 exp(x,y) is 125. Takako Nemoto (JAIST) 22 October 13 / 28

If statements int n; printf("input an integer: "); scanf("%d", &no); if (n % 5) puts("your input is not \ndivisible by 5."); Input an integer: 9 Your input is not divisible by 5. Input an integer: 10 The hilighted part means that if the inside of the brackets following if is satisfied, then do the following statements. In this case, puts ("Your input is not \n divisible by 5") is executed if no % 5 is not equal to 0 Takako Nemoto (JAIST) 22 October 14 / 28

If-else statements int n; printf("input an integer: "); scanf("%d", &no); if (n % 5) puts("your input is \nnot divisible by 5."); else puts("your input is \ndivisible by 5"); Input an integer: 9 Your input is NOT divisible by 5. Input an integer: 10 Your input is divisible by 5. By else statements, you can execute something if it is not the case of if (...). Takako Nemoto (JAIST) 22 October 15 / 28

Multiple if statements int year; printf("input year: "); scanf("%d", &year); if (year % 2) puts("you will have no Olympic game."); else if (year % 4) puts("you will have an winter Olympic game."); else puts("you will have an summer Olympic game."); Input year: 2018 You will have an winter Olympic game. Takako Nemoto (JAIST) 22 October 16 / 28

Multiple if statements int year; printf("input year: "); scanf("%d", &year); if (year % 2) puts("you will have no Olympic game."); else if (year % 4) puts("you will have an winter Olympic game."); else puts("you will have an summer Olympic game."); Input year: 2019 You will have no Olympic game. Takako Nemoto (JAIST) 22 October 17 / 28

Multiple if statements int year; printf("input year: "); scanf("%d", &year); if (year % 2) puts("you will have no Olympic game."); else if (year % 4) puts("you will have an winter Olympic game."); else puts("you will have an summer Olympic game."); Input year: 2020 You will have an summer Olympic game. Takako Nemoto (JAIST) 22 October 18 / 28

Control expression The green part is called control statement: if ( ). Control statements has the int type and if( val )... means that if the value of val is not equal to 0, do the following statements. We can express many conditions with relation and logical operators. Takako Nemoto (JAIST) 22 October 19 / 28

Equality & comparison operators Let a and b be floating point (or integer). The following expressions are evaluated as 0 or 1. expression a == b a!= b a > b a < b a >= b a <= b Evaluated 1 if The value of a is equal to the one of b The value of a is not equal to the one of b The value of a is strictly greater than the one of b The value of a is strictly smaller than the one of b The value of a is greater or equal to the one of b The value of a is smaller or equal to the one of b Takako Nemoto (JAIST) 22 October 20 / 28

Logical operators Logical operators && and can compound the expressions: Logical operators For control expressions A and B, value of A value of B value of A && B value of A B 1 1 1 1 1 0 0 1 0 1 0 1 0 0 0 0 Takako Nemoto (JAIST) 22 October 21 / 28

Example int x, y, z; printf("input 3 integers\n"); printf("x = "); scanf("%d", &x); printf("y = "); scanf("%d", &y); printf("z = "); scanf("%d", &z); if (x == y y == z z == x) printf("at least two of them are equal."); else printf("none of them are equal"); Takako Nemoto (JAIST) 22 October 22 / 28

Some more examples Example: Comparison three integers int main (void){ int x, y, z, max; puts("input three integers."); printf("x = "); scanf("%d", &x); printf("y = "); scanf("%d", &y); printf("z = "); scanf("%d", &z); max = x; if (y > max) max = y; if (z > max) max = z; printf ("The maximal value of them is %d.", max); Takako Nemoto (JAIST) 22 October 23 / 28

Block You can write multiple statements after if and else using {. int x, y; printf("input an integer x: "); scanf("%d", &x); printf("input an integer y: "); scanf("%d", &y); if (x > y){ printf("the maximum value is %d.\n", x); printf("the minimum value is %d.\n", y); else{ printf("the maximum value is %d.\n", y); printf("the minimum value is %d.\n", x); Takako Nemoto (JAIST) 22 October 24 / 28

Case distinction with switch int n; printf("input an integer: "); scanf("%d", &n); switch(n % 3){ case 0 : puts("it is divisible by 3."); break; case 1 : puts("the reminder divided by 3 is 1"); break; case 2 : puts("the reminder divided by 3 is 2"); break; Depending on the value n%3, the statements after case x is executed. break means go out from the switch statement. Takako Nemoto (JAIST) 22 October 25 / 28

Switch statement The number after case is label. Labels must be a constant, not a variable. You have to put whitespace between case and the label. If you don t put break;, then the next case x is executed. Try the example List 3-20 in p. 66. Takako Nemoto (JAIST) 22 October 26 / 28

Today s exercise and homework Do Exercise 2-4 in the textbook p. 33. Write a program in C s.t. ask 4 integers; print the minimum of them. Rewrite the Olympic-year program with switch. Homework 3 1 Write a program to convert the input Celsius degree temperature into Fahrenheight with 1 floating digit. 2 Write a such program s.t. it asks the elevator A s location (floor); the elevator B s location (floor); the elevator C s location (floor) and your current location (floor), and prints the closest elevator. Hint: Function abs. You should include stdlib.h. 3 See the next page. 4 Read ch.2-4 (pp. 21-104) of the textbook. Takako Nemoto (JAIST) 22 October 27 / 28

Homework 3 Rewrite the following using switch instead of if: int no; printf("input an integer: "); scanf("%d", &no); if(no % 2) printf("it is odd."); else printf("it is even."); Takako Nemoto (JAIST) 22 October 28 / 28