C programming Lecture 5. School of Mathematics Trinity College Dublin. Marina Krstic Marinkovic Marina Krstic Marinkovic 1

Similar documents
Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Functions Recursion. C++ functions. Declare/prototype. Define. Call. int myfunction (int ); int myfunction (int x){ int y = x*x; return y; }

Sources: On the Web: Slides will be available on:

Section IV.1: Recursive Algorithms and Recursion Trees

Analysis of Binary Search algorithm and Selection Sort algorithm

Data Structures. Algorithm Performance and Big O Analysis

Computer Science 2nd Year Solved Exercise. Programs 3/4/2013. Aumir Shabbir Pakistan School & College Muscat. Important. Chapter # 3.

2010/9/19. Binary number system. Binary numbers. Outline. Binary to decimal

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Statements and Control Flow

I PUC - Computer Science. Practical s Syllabus. Contents

1. Convert the following base 10 numbers into 8-bit 2 s complement notation 0, -1, -12

Senem Kumova Metin & Ilker Korkmaz 1

Lecture Notes on Linear Search

arrays C Programming Language - Arrays

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

1 Description of The Simpletron

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Computer Science 210: Data Structures. Searching

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Informatica e Sistemi in Tempo Reale

10CS35: Data Structures Using C

Lecture Outline. Stack machines The MIPS assembly language. Code Generation (I)

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

Introduction to Java

Introduction to Python

The While Loop. Objectives. Textbook. WHILE Loops

Recursion and Dynamic Programming. Biostatistics 615/815 Lecture 5

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Java Interview Questions and Answers

C++FA 5.1 PRACTICE MID-TERM EXAM

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Digital System Design Prof. D Roychoudhry Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Binary search algorithm

A single register, called the accumulator, stores the. operand before the operation, and stores the result. Add y # add y from memory to the acc

UNIVERSITY OF CALIFORNIA, DAVIS Department of Electrical and Computer Engineering. EEC180B Lab 7: MISP Processor Design Spring 1995

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

MISRA-C:2012 Standards Model Summary for C / C++

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Number Representation

Oct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8

Computer Science 281 Binary and Hexadecimal Review

Mathematical Induction. Lecture 10-11

What Is Recursion? 5/12/10 1. CMPSC 24: Lecture 13 Recursion. Lecture Plan. Divyakant Agrawal Department of Computer Science UC Santa Barbara

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

Data Structure with C

Graduate Assessment Test (Sample)

Object Oriented Software Design

FEEG Applied Programming 5 - Tutorial Session

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Here are some examples of combining elements and the operations used:

Parallel Computing. Shared memory parallel programming with OpenMP

PROBLEMS (Cap. 4 - Istruzioni macchina)

The Answer to the 14 Most Frequently Asked Modbus Questions

Lecture 2. Binary and Hexadecimal Numbers

C Programming. Charudatt Kadolkar. IIT Guwahati. C Programming p.1/34

Object Oriented Software Design

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

Recursion. Slides. Programming in C++ Computer Science Dept Va Tech Aug., Barnette ND, McQuain WD

Winter 2002 MID-SESSION TEST Friday, March 1 6:30 to 8:00pm

Introduction to Visual C++.NET Programming. Using.NET Environment

Example of a Java program

PES Institute of Technology-BSC QUESTION BANK

Overview. java.math.biginteger, java.math.bigdecimal. Definition: objects are everything but primitives The eight primitive data type in Java

Lecture 12: Entity Relationship Modelling

Introduction to Lex. General Description Input file Output file How matching is done Regular expressions Local names Using Lex

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 6 Program Control

Collatz Sequence. Fibbonacci Sequence. n is even; Recurrence Relation: a n+1 = a n + a n 1.

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

OS Lab Manual. To provide an understanding of the design aspects of operating system. Recommended Systems/Software Requirements:

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Computer Systems Architecture

Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings:

Lab 5 Introduction to Java Scripts

1. Define: (a) Variable, (b) Constant, (c) Type, (d) Enumerated Type, (e) Identifier.

In This Lecture. SQL Data Definition SQL SQL. Notes. Non-Procedural Programming. Database Systems Lecture 5 Natasha Alechina

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

1.3 Conditionals and Loops

Algorithms and Data Structures Exercise for the Final Exam (17 June 2014) Stack, Queue, Lists, Trees, Heap

Solution for Homework 2

SQL Programming. CS145 Lecture Notes #10. Motivation. Oracle PL/SQL. Basics. Example schema:

NEW TECHNIQUE TO DEAL WITH DYNAMIC DATA MINING IN THE DATABASE

God created the integers and the rest is the work of man. (Leopold Kronecker, in an after-dinner speech at a conference, Berlin, 1886)

Programming Database lectures for mathema

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

Answers to Review Questions Chapter 7

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Multiplying and Dividing Listen & Learn PRESENTED BY MATHMANIAC Mathematics, Grade 8

Fibonacci numbers introduce vectors, functions and recursion.

Assignment 5 - Due Friday March 6

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

Negative Integer Exponents

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Transcription:

C programming 5613 Lecture 5 Marina Krstic Marinkovic mmarina@maths.tcd.ie School of Mathematics Trinity College Dublin Marina Krstic Marinkovic 1 / 10 5613 - C programming

Assignment 0 - Fibonacci numbers: recursion // Program to find k-th number in a Fibonacci sequence // For a non-negative integer k, F_k=F_k-1+F_k-2, F_0=F_1=1 #include<stdio.h> int fib(int k) if (k==0) return 1; /*O-th element of Fib. seq.*/ if (k==1) return 1; /*1-th element of Fib. seq.*/ return fib(k-1)+fib(k-2); int main() int i; for (i=0;i<50;i++) printf("%d %d\n",i,fib(i)); /*printing all Fibonacci numbers from F_0 to F_50 */ Marina Krstic Marinkovic 2 / 10 5613 - C programming

Assignment 0 - Fibonacci numbers: for loop // Program to find k-th number in a Fibonacci sequence using FOR loop // For a non-negative integer k, F_k=F_k-1+F_k-2, F_0=F_1=1 #include<stdio.h> int main() int i,f0,f1,fk,n; printf("enter how many elements of Fibonacci sequence you want to compute\n"); scanf("%d",&n); f0=1; /* 0-th element: F_0=1 */ f1=1; /* 1-th element: F_1=1 */ for (i=2;i<=n;i++) fk=f0+f1; /*computing Fib. term fk*/ f0=f1; /*f0 captures f_k-1*/ f1=fk; /*f0 captures fk*/ /*both will be used in the next step: f_k+1=f_k-1+f_k*/ printf("f_%d = %d\n",i,fk); return 0; Marina Krstic Marinkovic 3 / 10 5613 - C programming

Assignment 0 - Fibonacci array: for loop // Program to find k-th number in a Fibonacci sequence using FOR loop // For a non-negative integer k, F_k=F_k-1+F_k-2, F_0=F_1=1 #include<stdio.h> int main() int i,n,a[50]; printf("enter how many elements of Fibonacci sequence you want to compute (n<50) \n"); scanf("%d",&n); a[0]=1; /* 0-th element: F_0=1 */ a[1]=1; /* 1-th element: F_1=1 */ if (i<2) printf("f_%d = %d\n",i,a[i]); else for (i=2;i<=n;i++) /* completing array containing F_0,F_1... F_n */ a[i]=a[i-1]+a[i-2]; /*filling in array a[] with the element F_i*/ printf("f_%d = %d\n",i,a[i]); Which value would your code give for f46? Marina Krstic Marinkovic 4 / 10 5613 - C programming

Assignment 0 - Fibonacci array: while loop // Program to find k-th number in a Fibonacci sequence using WHILE loop // For a non-negative integer k, F_k=F_k-1+F_k-2, F_0=F_1=1 #include<stdio.h> int main() int i; long a[50]; a[0]=1; /* 0-th element: F_0=1 */ a[1]=1; /* 1-th element: F_1=1 */ i=2; while (i<50) /* we will only construct elements F_0... F_45 */ a[i]=a[i-1]+a[i-2]; /*filling in array a[] with the element F_i*/ printf("%d %ld\n",i,a[i]); i++; Which value would your code give for f46? Marina Krstic Marinkovic 5 / 10 5613 - C programming

Control Statements: shorthand for if else Branching if/ if else/ else if/?/ switch //? as ternary operator: (condition)? expression1 : expression2; //? as binary operator: (expression1)? : expression2; GNU C only! Ternary operator: returns value of expression1 if condition is true, and otherwise evaluates and returns expression2 Binary (Elvis) operator:returns value of expression1 if that value is true, and otherwise evaluates and returns its second operand Marina Krstic Marinkovic 6 / 10 5613 - C programming

Control Statements: shorthand for if else As ternary operator: // c gets the value of the larger of two numbers: a,b c = (a > b)? a : b //shortand for: if (a>b) c=a; else c=b; As binary operator: // sets x equal to the result of f() if that result is a true value // othewise its sets x to the result of g() x = f()?: g() //equivalent to x = f()? f() : g() Marina Krstic Marinkovic 7 / 10 5613 - C programming

Control Statements: switch case Branching if/ if else/ else if/?/ switch case switch (variable or an integer expression) case constant1: //codes ; case constant2: //codes ; case constant3: //codes ; default: //codes ; Marina Krstic Marinkovic 8 / 10 5613 - C programming

Control Statements: switch case // Performs addition, subtraction, multiplication or division depending the input from user # include <stdio.h> int main() char operator; double firstnumber,secondnumber; printf("enter an operator (+, -, *,): "); scanf("%c", &operator); printf("enter two operands: "); scanf("%lf %lf",&firstnumber, &secondnumber); switch(operator) case '+': printf("%.1lf + %.1lf = %.1lf\n,firstNumber, secondnumber, firstnumber + secondnumber); break; case '-': printf("%.1lf - %.1lf = %.1lf\n",firstNumber, secondnumber, firstnumber - secondnumber); break; case '*': printf("%.1lf * %.1lf = %.1lf\n,firstNumber, secondnumber, firstnumber * secondnumber); break; case '/': printf("%.1lf / %.1lf = %.1lf\n",firstNumber, secondnumber, firstnumber / firstnumber); break; // operator doesn't match any case constant (+, -, *, /) default: printf("error! operator is not correct\n ); return 0; Marina Krstic Marinkovic 9 / 10 5613 - C programming

Exercises 1. Write a line of C-code that evaluates the expression z = ( min(x, y) for y 0, max(x 2,y 2 ) for y<0 for given x and y, withouth using if operator. 2. Write a C program that writes the number of days in a given month, if a user enters as the input the number of the month (e.g. 1=January, 2=February, etc.). In case the entered value is 2, the program should ask the user whether a year is a leap year or not. 3. For a given integer K between 1 and 2697, determine which digit is on the K-th place of an array 101102103104 998999 that contains the digits of all numbers from 101 to 999 in an ascending order. Marina Krstic Marinkovic 10 / 10 5613 - C programming