Functions Dr. Sajib Datta CSE@UTA.

Slides:



Advertisements
Similar presentations
CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
Advertisements

Dr. Sajib Datta  We can also have arrays of arrays, also known as multidimensional arrays.  E.g., A two-dimensional array is an array of several.
Search and Recursion pt. 2 CS221 – 2/25/09. How to Implement Binary Search Take a sorted data-set to search and a key to search for Start at the mid-point.
1 Introduction to Recursion  Introduction to Recursion  Example 1: Factorial  Example 2: Reversing Strings  Example 3: Fibonacci  Infinite Recursion.
Monday, 12/9/02, Slide #1 CS 106 Intro to CS 1 Monday, 12/9/02  QUESTIONS??  On HW #5 (Due 5 pm today)  Today:  Recursive functions  Reading: Chapter.
1 CSE1301 Computer Programming Lecture 27 Recursion (Part 1)
Recursion.  Identify recursive algorithms  Write simple recursive algorithms  Understand recursive function calling  With reference to the call stack.
Programming Arrays. Example 1 Write a program that reads 3 numbers from the user and print them in reverse order. How many variables do we need to store.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
What is recursion? Imagine checking the dictionary for “apple”  a round fruit with a firm white flesh and a green, red or yellow skin  the usually sweet-tasting.
Recursion.
Building Java Programs Chapter 13 Searching reading: 13.3.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
EENG212 Algorithms and Data Structures
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
Functions Programming Applications. Functions every C program must have a function called main program execution always begins with function main any.
While loop Write a program that asks the user to enter a number, then displays whether this number is even or odd. The program repeats until the user quits.
DATA TYPE, MEMORY, AND FUNCTION Dong-Chul Kim BioMeCIS UTA 2/18/
Starting Out with C++, 3 rd Edition Chapter 19 Recursion.
Dr. Sajib Datta Feb 11,  Example of declaring and initializing an array. ◦ double someData[3]; /* declare the array someData that will.
Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Dr. Sajib Datta Feb 14,  Ordering elements in some way  For numeric data, ascending order is the most common  Lots of techniques for.
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
Dr. Sajib Datta Sep 10,  #include  void main()  {  int a = 25;  int b = 0;  int c = -35;  if( a || b ) ◦ printf("Test1\n");  else.
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
Dr. Sajib Datta  Ordering elements in some way  For numeric data, ascending order is the most common  Lots of techniques for sorting  These.
Recursion You may have seen mathematical functions defined using recursion Factorial(x) = 1 if x
Recursion.
Recursion CENG 707.
Data Structures 1st Week
Computer Science 210 Computer Organization
Functions and Pointers
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
CSE 1320 Search, Sort and Strings
CSE1320 Loop Dr. Sajib Datta
Functions Dr. Sajib Datta
Module 5 Sorting and Searching: Bubble sort Selection sort
Module 4 Functions – function definition and function prototype.
CHAPTER 7 RECURSIVE FUNCTION
Functions and Pointers
Computer Science 210 Computer Organization
Control Structures Lecture 7.
Recursive functions.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Proving algorithms (programs) correct with induction
Pointers.
Algorithm design and Analysis
Introduction to Search Algorithms
Programming application CC213
Arrays.
Recursion Department of Computer Science-BGU יום שני 10 דצמבר 2018.
A function with one argument
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Week 6 CPS125.
Exercise 5 1. We learned bubble sort during class. This problem requires you to modify the code for bubble sorting method to implement the selection sorting.
ICS103 Programming in C Lecture 11: Recursive Functions
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Sorting Algorithms.
Presentation transcript:

Functions Dr. Sajib Datta CSE@UTA

Multiple value return In your function, the key word “return” can only return a single value, NOT multiple values. But you can define an array in main function body, and pass it to your function. In your function, we may store multiple values in that array, which will be visible in the main function. So it is sort of returning multiple values. See the example in the next slide.

A function to return the min and the max of an array of integers #include <stdio.h> void minmax(int myarr[], int myminmax [], int s); int main(void) { int arr[] ={42, 2, 443, 23, 12}, minmaxarr[2], size; size = sizeof(arr)/sizeof(int); minmax(arr, minmaxarr, size); printf("The array has a minimum value of %d, and a maximum value of %d.\n", minmaxarr[0], minmaxarr[1]); return 0; } void minmax(int myarr[], int myminmax [], int s) int i; myminmax[0] = myarr[0];// myminmax[0] for the minimum value myminmax[1] = myarr[0];// myminmax[1] for the maximum value for(i = 1; i < s; i ++) if (myminmax[0] > myarr[i]) myminmax[0] = myarr[i]; else if (myminmax[1] < myarr[i]) myminmax[1] = myarr[i];

Recursion Recursive Function Definition Why Recursive Function Recursive function is a function that contains a call to itself. Why Recursive Function Recursive function allows you to divide your complex problem into identical single simple cases which can handle easily. Note on Using Recursive Function Recursive function must have at least one exit condition that can be satisfied. Otherwise, the recursive function will call itself repeatedly into a dead loop until the runtime stack overflows.

Recursion # include<stdio.h> int sum(int number) { if(number <= 1) return 1; return number + sum(number - 1); } void main() int x = 5; printf("The sum of all positive integers (<= %d) is %d.\n", x, sum(x)); return 0; How to implement factorial?

Fibonacci Sequence Define a recursive function to implement FS Given n, print out the first n integers in FS 0,1,1,2,3,5,8,13,21,34,55…..(1st 10 numbers)

#include <stdio.h> int fibonacci(int n); int main(void) { int input, i; printf("Please input a positive integer value:"); scanf("%d", &input); for(i = 0; i <= input; i++) printf("%d ", fibonacci(i)); } int fibonacci(int n) if(n < 2) return n; else return fibonacci(n-1)+fibonacci(n-2);

Binary Search

while(left <= right) { mid = (left+right)/2; if(intarr[mid] == target) printf("The index of the target in the array is %d.\n", mid); break; } else if (target > intarr[mid]) left = mid + 1; else right = mid - 1; if(left > right) printf("The target is not in the array.\n"); return 0; #include <stdio.h> #define ARRSIZE 7 int main(void) { int intarr[ARRSIZE], target, i, left, right, mid; printf("Please input %d integers which are sorted in the ascending order and a target value.", ARRSIZE); scanf("%d", &target); for(i = 0; i<ARRSIZE; i++) scanf("%d", &intarr[i]); } left = 0; right = ARRSIZE-1;

Binary Search- Recursive #include<stdio.h> #define ARR_SIZE 10 void binarySearch(int arr[], int left, int right, int t); int main(void) { int intarr[ARR_SIZE] = {-12, -3, 0, 5, 11, 15, 27, 33, 49, 59}, target; puts("Please input a target value:"); scanf("%d", &target); binarySearch(intarr, 0, ARR_SIZE-1, target); return 0; } void binarySearch(int arr[], int left, int right, int t) { int mid; printf("The current range is %d, %d.\n", left, right); if(left<=right) mid = (left+right)/2; if(arr[mid] == t) printf("The index of the target is %d.\n.", mid); else if(arr[mid] > t) binarySearch(arr, left, mid-1, t); else binarySearch(arr, mid+1, right, t); } printf("The target is not found.");