Programming Language B

Similar documents
Programming Language A

Programming Language B

Programming Language A

Programming Language A

Programming Language B

Programming Language A

Programming Language B

Programming Language B

Programming Language B

Programming Language B

Introduction to Programming

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

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

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 2018

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

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

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

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

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

16.216: ECE Application Programming Fall 2013

Question Bank (SPA SEM II)

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

Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2

16.216: ECE Application Programming Fall 2011

Lecture 6. Statements

Worksheet 4 Basic Input functions and Mathematical Operators

Chapter 2. Section 2.5 while Loop. CS 50 Hathairat Rattanasook

Functions. Arash Rafiey. September 26, 2017

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

COL 100 Introduction to Programming- MINOR 1 IIT Jammu

More examples for Control statements

n Group of statements that are executed repeatedly while some condition remains true

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

ECE 15 Fall 15 Final Solutions

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

SAMPLE MIDTERM SOLUTION

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

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

EECE.2160: ECE Application Programming Fall 2017

Lecture 10: Recursive Functions. Computer System and programming in C 1

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

Structured programming. Exercises 3

COP 3223 Section 4 Exam #2 Form A Multiple Choice Spring /4/2015

COMP 208 Computers in Engineering

Lecture 02 C FUNDAMENTALS

Programming & Data Structure Laboratory. Day 2, July 24, 2014

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

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Fundamentals of Programming

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

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities

Operators And Expressions

Arrays. Here is the generic syntax for an array declaration:

Indian Institute of Technology Kharagpur Programming and Data Structures (CS10001) Autumn : Mid-Semester Examination

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04

Computer Programing. for Physicists [SCPY204] Class 02: 25 Jan 2018

Tutorial 5. PDS Lab Section 16 Autumn Functions The C language is termed as function-oriented programming

Precedence and Associativity Table. % specifiers in ANSI C: String Control Codes:

ECE 15 Fall 16 Midterm Solutions

Dalhousie University CSCI 2132 Software Development Winter 2018 Midterm Examination II March 12 15:37-16:24

Structured programming

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction

Week 2 / Lecture 2 15 March 2017 NWEN 241 Control constructs, Functions. Alvin Valera

Structured programming

The detail of sea.c [1] #include <stdio.h> The preprocessor inserts the header file stdio.h at the point.

Introduction to Programming (Java) 4/12

For questions 4 through 7, select the value assigned to the relevant variable, given the declarations: 3) ) This is not allowed

Informatica e Sistemi in Tempo Reale

ME 172. C Programming Language Sessional Lecture 8

Procedural Programming

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

Arrays and Strings. Antonio Carzaniga. February 23, Faculty of Informatics Università della Svizzera italiana Antonio Carzaniga

Problem # 1. calculate the grade. Allow a student the next grade if he/she needs only 0.5 marks to obtain the next grade. Use else-if construction.

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

BSM540 Basics of C Language

Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II)

Computer Science & Engineering 150A Problem Solving Using Computers

Programming & Data Structure Laboratory. Arrays, pointers and recursion Day 5, August 5, 2014

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

Array Lesson 1 Outline

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Functions. Chapter 5

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

Computer Programming: Skills & Concepts (CP1) Files in C. 18th November, 2010

1. The keyword main in C language is used for

16.216: ECE Application Programming Fall 2011

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Structured programming

Computer Programming. Decision Making (2) Loops

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

7/21/ FILE INPUT / OUTPUT. Dong-Chul Kim BioMeCIS UTA

80 Minutes CENG 230 MidtermExam :40

Make sure the version number is marked on your scantron sheet. This is Version 1

LOOPS. 1- Write a program that prompts user to enter an integer N and determines and prints the sum of cubes from 5 to N (i.e. sum of 5 3 to N 3 ).

What we have learned so far

Friday, February 3, Lab Notes. Functions in C Arrays as parameters Pass by value vs pass by reference Programs 1 and 2

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

Transcription:

Programming Language B Takako Nemoto (JAIST) 3 December Takako Nemoto (JAIST) 3 December 1 / 18

Today s topics 1 Function-like macro 2 Sorting 3 Enumeration 4 Recursive definition of functions 5 Input/output and characters Takako Nemoto (JAIST) 3 December 2 / 18

Function-like macro (8-2) #define sqr(x) ((x)*(x)) int n; double x; printf("input an integer: "); scanf("%d", &n); printf("the square of it is %d.\n", sqr(n)); printf("input a real number: "); scanf("%lf", &x); printf("the square of it is %f\n", sqr(x)); return 0; Takako Nemoto (JAIST) 3 December 3 / 18

Function-like macro: Expansion #define sqr(x) ((x)*(x)) int n; double x; printf("input an integer: "); scanf("%d", &n); printf("the square of it is %d.\n", ((n)*(n))); printf("input a real number: "); scanf("%lf", &x); printf("the square of it is %f\n", ((x)*(x))); return 0; Takako Nemoto (JAIST) 3 December 4 / 18

Some points of function-like macro How the following will be expanded? sqr(a++); It will be expanded as follows: ((a++)*(a++)); and so is the same as ((a)*(a+1)); a = a+1; What happen if we write #define sqr (x) ((x)*(x))? Then sqr is replaced by (x) ((x)*(x)). Do not insert a white space between sqr and (x). Takako Nemoto (JAIST) 3 December 5 / 18

Some points of function-like macro Assume we have #define sum_of(x,y) x+y. Then how the following will be expanded? We will have which is same as z = sum of(a,b) * sum of(c,d); z = a+b * c+d; z = a + (b * c) + d; Put brackets to each variables and the total macro. Takako Nemoto (JAIST) 3 December 6 / 18

Comma operator (8-4) #define put_newline(str) ( putchar('\n'), puts(str) ) Input an integer: 3 int n; It is not 0. printf("input an integer: "); scanf("%d", &n); if (n) put_newline("it is not 0."); else put_newline("it is 0."); return 0; Takako Nemoto (JAIST) 3 December 7 / 18

Comma operator To use comma operator, use brackets ( ). int a=1, b=2, c=3, i;// commas as separators. i = (a, b); // comma as an operator. b is assigned into i. i = a, b; // a is assigned into i. Same as (i = a), b; i = (a += 2, a + b); // after evaluating a = a+2, a+b is assigned into i. i = a += 2, a + b; // after evaluating a = a+2, a is assigned into i. i = a, b, c; // a is assigned into i i = (a, b, c); // c is assigned into i. int j=0; j++, j++; // j = j+1, j=j+1; a = c++, j++; // c++ is assigned into a. printf("a = %d\n", a); printf("b = %d\n", b); printf("c = %d\n", c); printf("i = %d\n", i); printf("j = %d\n", j); Takako Nemoto (JAIST) 3 December 8 / 18

Sorting: Bubble sorting (8-5) #define NUMBER 5 void bsort(int a[], int n){ int i, j; for (i = 0; i < n - 1; i++){ for (j = n - 1; j > i; j--){ if (a[j - 1] > a[j]){ int temp = a[j]; a[j] = a[j - 1]; a[j - 1] = temp; int i; int height[number]; for (i = 0; i < NUMBER; i++){ printf("no.%2d: ", i + 1); scanf("%d", &height[i]); bsort(height, NUMBER); puts("sorted in the ascending order:"); for (i = 0; i < NUMBER; i++) printf("no. %2d: %d\n", i+1, height[i]); return 0; Takako Nemoto (JAIST) 3 December 9 / 18

Enumeration type (8-6) enum animal {Dog, Cat, Monkey, Invalid; void dog(void){puts("bow-wow"); void cat(void){puts("meow"); void monkey(void){puts("oh oh ah ah"); enum animal select(void){ int tmp; do{printf("0..dog, 1..Cat, 2..Monkey, 3..Quit: "); scanf("%d", &tmp); while (tmp < Dog tmp >Invalid); return tmp; enum animal selected; do{switch (selected = select()){ case Dog : dog(); break; case Cat : cat(); break; case Monkey : monkey(); break; while (selected!= Invalid); return 0; enum animal is a declaration of enumeration, with the identifier animal. Dog, Cat, Monkey, Invalid are enumeration constants, which are assigned 0, 1, 2, 3, respectively. enum animal is a enumerated type. enum animal selected is a declaration that the variable selected has the type enum animal. Takako Nemoto (JAIST) 3 December 10 / 18

Enumeration type (8-6) enum animal {Dog, Cat, Monkey, Invalid; void dog(void){puts("bow-wow"); void cat(void){puts("meow"); void monkey(void){puts("oh oh ah ah"); while(selected = select()) means selected = select(); while(selected) enum animal select(void){ int tmp; do{printf("0..dog, 1..Cat, 2..Monkey, 3..Quit: "); scanf("%d", &tmp); while (tmp < Dog tmp >Invalid); return tmp; enum animal selected; do{switch (selected = select()){ case Dog : dog(); break; case Cat : cat(); break; case Monkey : monkey(); break; while (selected!= Invalid); return 0; Takako Nemoto (JAIST) 3 December 11 / 18

Recursive definition of functions int factorial (int n){ if (n > 0) return n * factorial(n-1); else return 1; int main (void){ int num; printf("input an integer: "); scanf("%d", &num); printf("its factorial is %d.", factorial(num)); return 0; Takako Nemoto (JAIST) 3 December 12 / 18

Input/output and characters int ch; while ((ch = getchar())!= EOF) putchar(ch); return 0; EOF means the special character for the end of the file. In the command prompt in Windows, it can be inputed by Ctrl+z (+Entr). In Linux or OS X, it can be input by Ctrl+d. Takako Nemoto (JAIST) 3 December 13 / 18

// Example 8-9 int i, ch; int cnt[10] = {0; while((ch = getchar())!= EOF){ switch (ch){ case '0' : cnt[0]++; break; case '1' : cnt[1]++; break; case '2' : cnt[2]++; break; case '3' : cnt[3]++; break; case '4' : cnt[4]++; break; case '5' : cnt[5]++; break; case '6' : cnt[6]++; break; case '7' : cnt[7]++; break; case '8' : cnt[8]++; break; case '9' : cnt[9]++; break; puts("the number of the occurrences:"); for (i = 0; i < 10; i++) printf("'%d': %d\n", i, cnt[i]); return 0; while ((ch = getchar())!= EOF) means getchar() is assigned into ch and if it is not EOF, the loop body is executed. '1' means the number assigned into 1 in the system. So this program counts and shows the number of the occurrence of each number. Takako Nemoto (JAIST) 3 December 14 / 18

The code for each number Recall the program to answer the code for each characters: char char1[10]; printf("input a character: "); scanf("%s", char1); printf("%d",char1[0]); Let v[i] be the assigned for the alphabet i in your system. Verify that v[i]-v[j]=i-j for i, j {0,..., 9. Takako Nemoto (JAIST) 3 December 15 / 18

A simplification (8-10) int i, ch; int cnt[10] = {0; while((ch = getchar())!= EOF){ if (ch >= '0' && ch <='9') cnt[ch - '0']++; puts("the number of occurrences:"); for (i = 0; i < 10; i++) printf("'%d': %d\n", i, cnt[i]); return 0; Takako Nemoto (JAIST) 3 December 16 / 18

Input and output with files Assume that we get an executable file 8-10.exe. Then we can apply it to a file. 8-10.exe < inputfile > outputfile.txt Takako Nemoto (JAIST) 3 December 17 / 18

Today s homework 1 Do Exercise 8-4 ( 8-4) in p.222 (in the latest edition). 2 Do Exercise 8-7 ( 8-7) in p.227 (in the latest edition). 3 Make a program in C to count the number of lines of the input. Apply it to the file https://www.gutenberg.org/files/215/215-0.txt and report also the result as a text file. As usual, send the programs and the text file as attached files to me via e-mail, with the title Homework Lecture 9 by the next lecture. Please make sure to include your name and student ID number. Takako Nemoto (JAIST) 3 December 18 / 18