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

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

C Functions. 5.2 Program Modules in C

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

Functions. Systems Programming Concepts

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

C Programs: Simple Statements and Expressions

CSE123. Program Design and Modular Programming Functions 1-1

Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

Lecture 04 FUNCTIONS AND ARRAYS

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

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design

Assoc. Prof. Dr. Tansu FİLİK

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

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

Programming Fundamentals for Engineers Functions. Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. Modular programming.

Beginning C Programming for Engineers

ECET 264 C Programming Language with Applications

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

Chapter 3 - Functions

Chapter 5 C Functions

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

CS3157: Advanced Programming. Outline

Programming Assignment #4 Arrays and Pointers

Introduction to Programming

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

The C standard library

Introduction to C Language

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

JTSK Programming in C II C-Lab II. Lecture 3 & 4

H192 Midterm 1 Review. Tom Zajdel

BİL200 TUTORIAL-EXERCISES Objective:

Functions and Recursion

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

C++ PROGRAMMING SKILLS Part 3 User-Defined Functions

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

Function Call Stack and Activation Records

C: How to Program. Week /Apr/23

Multiple Choice Questions ( 1 mark)

Visual Studio. Visual Studio is an extremely complex interactive development environment capable of handling many languages and tools.

Chapter 4 Functions By C.K. Liang

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 5. Playing with Data Modifiers and Math Functions Getting Controls

today cs3157-fall2002-sklar-lect05 1

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

Programming in C Quick Start! Biostatistics 615 Lecture 4

Programming for Engineers Functions

C Functions Pearson Education, Inc. All rights reserved.

Programming and Data Structure

بسم اهلل الرمحن الرحيم

C++, How to Program. Spring 2016 CISC1600 Yanjun Li 1

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

Lecture 04 FUNCTIONS AND ARRAYS

Functions. Autumn Semester 2009 Programming and Data Structure 1. Courtsey: University of Pittsburgh-CSD-Khalifa

Fundamentals of Programming & Procedural Programming

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

Fundamentals of Programming Session 8

C++ Programming Lecture 11 Functions Part I

Calling Prewritten Functions in C

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

C Programming Language

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

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

Lecture 14. Daily Puzzle. Math in C. Rearrange the letters of eleven plus two to make this mathematical statement true. Eleven plus two =?

Function I/O. Function Input and Output. Input through actual parameters. Output through return value. Input/Output through side effects

Fundamentals of Computer Programming Using C

Technical Questions. Q 1) What are the key features in C programming language?

Function I/O. Last Updated 10/30/18

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

ECET 264 C Programming Language with Applications. C Program Control

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I

BSM540 Basics of C Language

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

AMCAT Automata Coding Sample Questions And Answers

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

Introduction to Programming

Biostatistics 615 / 815

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Arithmetic Expressions in C

Midterm Exam. CSCI 2132: Software Development. March 4, Marks. Question 1 (10) Question 2 (10) Question 3 (10) Question 4 (10) Question 5 (5)

Methods: A Deeper Look

C Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables

Computer System and programming in C

Chapter 1 & 2 Introduction to C Language

The Math Class. Using various math class methods. Formatting the values.

CSE 1320 INTERMEDIATE PROGRAMMING - OVERVIEW AND DATA TYPES

Introduction to Functions in C. Dr. Ahmed Telba King Saud University College of Engineering Electrical Engineering Department

Unit 7. Functions. Need of User Defined Functions


Tutorial 10 Pointers in C. Shuyue Hu

An Introduction to C Through Annotated Examples

UNIT-I Input/ Output functions and other library functions

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

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

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

Transcription:

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

Examples Display the first 10 Even Numbers > 0; Display the odd numbers between 60 and 70; Display a random number between 1 and 10, as many times as the user requests. First 10 even numbers, > 0 What are we after? The trick is to understand the question and document a proper solution. We know: METHOD 1: we can check for an even number by using modulus 2 and check if the remainder is 0. METHOD 2: or we can figure out that the values we are after are 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 and the pattern is to start from 2 and increment by 2 2

Method 2: simpler! int main() int num; // stores the even number for (num = 2; num <= 20; num+=2) printf("%d ", num); return 0; Display Odd numbers between 60 and 70 What is an odd number? A number when divided by 2 the remainder is 1. or given the question parameters, a number list: 61, 63, 65, 67, 69 3

code the simpler method int main() int num; // stores the odd number for (num = 61; num <= 69; num+=2) printf("%d ", num); return 0; Problem 3: Display a random number between 1 and 10, as many times as the user requests. What is a random number? we can find that someone else figured this one out! the calculation is packaged into a function and shipped with the C compiler tools as part of the STANDARD LIBRARY 4

What is the Standard Library? Standard means that the solution or function is supported by a national and international committee (ANSI / ISO) and is known to work the same way across different compilers. A Library is a term used to refer to the packaged functions and files shipped together. Standard Library Functions If your compiler is following a standard (say ANSI) then we know that there are many (thousands) of functions that are shipped with that compiler tools as part of a standard library. We can make use of any of these functions to save on development time and reliability of our code. (standard functions are often rigorously tested and well documented) 5

So, What is a FUNCTION? Functions allow us to divide a large task into smaller components or modules and conquer each one independently. They are a prime example of software reuse and avoiding re-inventing the wheel As a general rule, any piece of logic that is likely to be repeated is worth placing it in a separate function. Where do functions fit in a program? You have already been using functions! int main ( ) is a function! We can: Use existing functions (standard library) or Make our own! We will begin by understanding how to use existing functions first. 6

Sample Libraries common math library functions (math.h) #include <math.h> you have to include the math header file so you can tell the compiler where to find the definitions of each of the math functions you want to use. sqrt( x ) square root of x fabs( x ) absolute value of x ceil( x ) rounds x up floor( x ) rounds x down pow( x, y ) x raised to the power y Example: #include <math.h>... double x, y;... x = sqrt( 25 ) + 5; y = 10 + pow(x, 3);... // Square root. // x raised to // power of 3. 7

More Libraries: fig 5.6 page 153, 154 <string.h> string processing <time.h> time and date <stdio.h> input / output <stdlib.h> conversions, memory allocations, random numbers, and other utilities Random Number Generation found in the library <stdlib.h> function is: rand( ) rand( ) generates an int between 0 and 32767 sample usage: 1 + (rand () % 6 ) ) generates an integer between 1 and 6 Related functions: srand( time( NULL ) ); uses time.h to generate a unique seed from the clock 8

Back to Problem 3: Display a random number between 1 and 10, as many times as the user requests. Given: we already have the function rand( ) to generate a random number, we need to modify its results to match what we are looking for. We need to build an interactive code with the user to display the random number as many times as the user requests. rand ( ) #include <stdio.h> #include <stdlib.h> #include <time.h> int main() char done = N ; char skip; srand( time( NULL )); do printf( %d, 1 + ( rand() % 10 ) ); printf( \ndo you want another number? ); scanf( %c%c, &done, &skip); while (done == Y done == y ); return 0; 9

rand ( ) #include <stdio.h> #include <stdlib.h> #include <time.h> int main() char done = N ; char skip; srand( time( NULL )); do printf( %d, 1 + ( rand() % 10 ) ); printf( \ndo you want another number? ); scanf( %c%c, &done, &skip); while (done == Y done == y ); return 0; 10