第三次小考. #include using namespace std; int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout<<"u="<<u<<"\n";

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Advertisements

Lecture 14 Today: Overloading: Revision on this Revision on increment operators the assignment operator the [] operator Book: p , 215,
For(int i = 1; i
Computer Science 1620 Math Library. Remember this program? suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to.
CPSC 441 TUTORIAL – JANUARY 16, 2012 TA: MARYAM ELAHI INTRODUCTION TO C.
PHYS707: Special Topics C++ Lectures Lecture 3. Summary of Today’s lecture: 1.Functions (call-by-referencing) 2.Arrays 3.Pointers 4.More Arrays! 5.More.
CS 240 Computer Programming 1
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Factorial Preparatory Exercise #include using namespace std; double fact(double); int fact(int); int main(void) { const int n=20; ofstream my_file("results.txt");
Chapter 16 Exception Handling. What is Exception Handling? A method of handling errors that informs the user of the problem and prevents the program from.
Vectors, lists and queues
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline some useful problems.
Functions Prototypes, parameter passing, return values, activation frams.
Problem You require an algorithm that will receive the length of the hypotenuse and one of the sides of a right triangle, and calculate the length of the.
ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne
Template Implicit function overload. Function overload Function overload double ssqq(double & a, double & b) { return(a*b);} float ssqq(float & a, float.
Derived data types Dealing with data –Where the information is stored –What value is kept there –What kind of information is stored Address operator Pointers.
Exercise 2.
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Revision.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 5 Functions for All Subtasks.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab:
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Tinaliah, S. Kom.. * * * * * * * * * * * * * * * * * #include using namespace std; void main () { for (int i = 1; i
Triana Elizabeth, S.Kom. #include using namespace std; void main () { for (int i = 1; i
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
Conditional Operator (?:) Conditional operator (?:) takes three arguments (ternary) Syntax for using the conditional operator:
Chapter 4 Summation.
1 Session-9 CSIT 121 Spring 2006 Lab Demo (first 20 minutes) The correct way to do the previous lab sheet Function calls Calling void functions Calling.
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
TEXT FILES. Text Files Files Data saved in external storage and can be referenced by a single name File types Document, image, audio, video, etc. Program.
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
1 Original Source : and Problem and Problem Solving.ppt.
Objective: Students will be able to: Declare and use variables Input integers.
Lecture 4 1 Loops - Exercises. Lecture 4 Exercise 1 2 What is the value of x when the do... while loop completes? int x = 10, k; do{ k = ++x; x -= 2;
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Classes Sujana Jyothi C++ Workshop Day 2. A class in C++ is an encapsulation of data members and functions that manipulate the data. A class is a mechanism.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
Copyright © Curt Hill STL Priority Queue A Heap-Like Adaptor Class.
1 Compiler directive: #define, usage 1 #include using namespace std; #define TAX //double TAX=0.08; #define LAST_NAME "Li" #define FIRST_NAME "Dennis"
1 Generating Random Numbers Textbook ch.6, pg
MR. CRONE Generating Random Numbers. Random Numbers Many programs require the computer to generate random numbers Random numbers are used in many applications.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
General Computer Science for Engineers CISC 106 Lecture 27 Dr. John Cavazos Computer and Information Sciences 04/27/2009.
Chapter five exercises. a. false; b. true; c. false; d. true; e. true; f. true; g. true; h. false.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
#define #include<iostream> using namespace std; #define GO
Intro to Programming Week # 6 Repetition Structure Lecture # 10
Command Line Arguments
Chapter 4 Loops Case Studies
Random Number Generation
More About File Reading
Name: Rubaisha Rajpoot
Screen output // Definition and use of variables
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
Anatomy of a Function Part 1
Code::Block vs Visual C++
الوحدة الرابعة البرمجة وصياغة حل المسائل البرمجة وأهميتها أهداف الدرس الأول مفهوم البرمجة. الفرق بين المبرمج ومستخدم البرنامج. الحاجة إلى البرامج.
CS1201: Programming Language 2
foo.h #ifndef _FOO #define _FOO template <class T> class foo{
Pointers & Functions.
Anatomy of a Function Part 1
Programming Strings.
Introduction to Algorithms and Programming COMP151
Presentation transcript:

第三次小考

#include using namespace std; int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout<<"u="<<u<<"\n"; v=aaa(ib,m+1,a2); cout<<"v="<<v<<"\n"; if(u>v)return u;else return v; } int main() { int ia[10]={0,2,3,6,8,6,12,28,16,11}; cout<<aaa(ia,0,9)<<"\n"; return 0; }

aaa(ib,0,9)

int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout<<"u="<<u<<"\n"; v=aaa(ib,m+1,a2); cout<<"v="<<v<<"\n"; if(u>v)return u;else return v; } 傳入參數 a1=0 , a2=9 m=(0+9)/2 …… 因為 m 屬於 int ,所以 m 等於整數部份 m = 4 a1=0 , a2=9  沒有 return 傳入 aaa(ib,0,4)

aaa(ib,0,9) aaa(ib,0,4)

int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout<<"u="<<u<<"\n"; v=aaa(ib,m+1,a2); cout<<"v="<<v<<"\n"; if(u>v)return u;else return v; } 傳入參數 a1=0 , a2=4 m=(0+4)/2 …… 因為 m 屬於 int ,所以 m 等於整數部份 m = 2 a1=0 , a2=4  沒有 return 傳入 aaa(ib,0,2)

aaa(ib,0, 9) aaa(ib,0, 4) aaa(ib,0, 2)

int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout<<"u="<<u<<"\n"; v=aaa(ib,m+1,a2); cout<<"v="<<v<<"\n"; if(u>v)return u;else return v; } 傳入參數 a1=0 , a2=2 m=(0+2)/2 …… 因為 m 屬於 int ,所以 m 等於整數部份 m = 1 a1=0 , a2=2  沒有 return 傳入 aaa(ib,0,1)

aaa(ib,0,9) aaa(ib,0,4) aaa(ib,0,2) aaa(ib,0,1)

int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout<<"u="<<u<<"\n"; v=aaa(ib,m+1,a2); cout<<"v="<<v<<"\n"; if(u>v)return u;else return v; } 傳入參數 a1=0 , a2=1 m=(0+1)/2 …… 因為 m 屬於 int ,所以 m 等於整數部份 m = 0 a1=0 , a2=1  沒有 return 傳入 aaa(ib,0,0)

aaa(ib,0,9) aaa(ib,0,4) aaa(ib,0,2) aaa(ib,0,1) aaa(ib,0,0)

int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout<<"u="<<u<<"\n"; v=aaa(ib,m+1,a2); cout<<"v="<<v<<"\n"; if(u>v)return u;else return v; } 傳入參數 a1=0 , a2=0 m=(0+2)/2 …… 因為 m 屬於 int ,所以 m 等於整數部份 m = 1 a1=0 , a2=0  return ib[0]

aaa(ib,0,9) aaa(ib,0,4) aaa(ib,0,2) aaa(ib,0,1) aaa(ib,0,0) return ib[0] (ib[0]=0) u=aaa(ib,0,0)=0

int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout<<"u="<<u<<"\n"; v=aaa(ib,m+1,a2); cout<<"v="<<v<<"\n"; if(u>v)return u;else return v; } 傳入參數 a1=0 , a2=1 m=(0+1)/2 …… 因為 m 屬於 int ,所以 m 等於整數部份 m = 0 a1=0 , a2=1  沒有 return aaa(ib,0,0) return ib[0] 所以 u=ib[0] (u=0) u=0 傳入 aaa(ib,1,1)

aaa(ib,0,9) aaa(ib,0,4) aaa(ib,0,2) aaa(ib,0,1) aaa(ib,0,0) u=0 return ib[0] (ib[0]=0) aaa(ib,1,1)

int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout<<"u="<<u<<"\n"; v=aaa(ib,m+1,a2); cout<<"v="<<v<<"\n"; if(u>v)return u;else return v; } 傳入參數 a1=1 , a2=1 m=(1+1)/2 …… 因為 m 屬於 int ,所以 m 等於整數部份 m = 1 a1=a2=1  return ib[1]

aaa(ib,0,9) aaa(ib,0,4) aaa(ib,0,2) aaa(ib,0,1) aaa(ib,0,0) u=0 return ib[0] (ib[0]=0) aaa(ib,1,1) v=2 return ib[1] (ib[1]=2) v=aaa(ib,1,1)=2

int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout<<"u="<<u<<"\n"; v=aaa(ib,m+1,a2); cout<<"v="<<v<<"\n"; if(u>v)return u;else return v; } 傳入參數 a1=0 , a2=1 m=(0+1)/2 …… 因為 m 屬於 int ,所以 m 等於整數部份 m = 0 a1=0 , a2=1  沒有 return 傳入 aaa(ib,0,0) u = 0 v = aaa(ib,1,1) v = 2 u=0,v = 2 v>u return v (v=2)

v>u return v (v=2) aaa(ib,0,9) aaa(ib,0,4) aaa(ib,0,2) aaa(ib,0,1) u=2 aaa(ib,0,0) u=0 return ib[0] (ib[0]=0) aaa(ib,1,1) v=2 return ib[1] (ib[1]=2)

int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout<<"u="<<u<<"\n"; v=aaa(ib,m+1,a2); cout<<"v="<<v<<"\n"; if(u>v)return u;else return v; } 傳入參數 a1=0 , a2=2 m=(0+2)/2 …… 因為 m 屬於 int ,所以 m 等於整數部份 m = 1 a1=0 , a2=2  沒有 return u=aaa(ib,0,1)=2 u=2 傳入 aaa(ib,2,2)

aaa(ib,0,9) aaa(ib,0,4) aaa(ib,0,2) aaa(ib,0,1) u=2 aaa(ib,0,0) u=0 return ib[0] (ib[0]=0) aaa(ib,1,1) v=2 return ib[1] (ib[1]=2) aaa(ib,2,2)

int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout<<"u="<<u<<"\n"; v=aaa(ib,m+1,a2); cout<<"v="<<v<<"\n"; if(u>v)return u;else return v; } 傳入參數 a1=2 , a2=2 m=(2+2)/2 …… 因為 m 屬於 int ,所以 m 等於整數部份 m = 2 a1=a2=2  return ib[2] (ib[2]=3)

aaa(ib,0,9) aaa(ib,0,4) aaa(ib,0,2) aaa(ib,0,1) u=2 aaa(ib,0,0) u=0 return ib[0] (ib[0]=0) aaa(ib,1,1) v=2 return ib[1] (ib[1]=2) aaa(ib,2,2) v=3 return ib[2] (ib[2]=3) v=aaa(ib,2,2)=3

int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout<<"u="<<u<<"\n"; v=aaa(ib,m+1,a2); cout<<"v="<<v<<"\n"; if(u>v)return u;else return v; } 傳入參數 a1=0 , a2=2 m=(0+2)/2 …… 因為 m 屬於 int ,所以 m 等於整數部份 m = 1 a1=0 , a2=2  沒有 return u=aaa(ib,0,1)=2 u=2 傳入 aaa(ib,2,2) v=3 u=2,v = 3 v>u return v (v=3)

v>u return v (v=3)

int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout<<"u="<<u<<"\n"; v=aaa(ib,m+1,a2); cout<<"v="<<v<<"\n"; if(u>v)return u;else return v; } 傳入參數 a1=0 , a2=4 m=(0+4)/2 …… 因為 m 屬於 int ,所以 m 等於整數部份 m = 2 a1=0 , a2=4  沒有 return 傳入 aaa(ib,0,2) aaa(ib,0,2) = 3 u = 3 傳入 aaa(ib,3,4)

v>u return v (v=3)

加油 ~~!! 繼續做下去一定會是 以下結果

aaa(ib,0,9) 28 aaa(ib,0,4) u=8 aaa(ib,0,2) u=3 aaa(ib,0,1) u=2 aaa(ib,0,0) u=0 return ib[0] aaa(ib,1,1) v=2 return ib[1] aaa(ib,2,2) v=3 return ib[2] aaa(ib,3,4) v=8 aaa(ib,3,3) u=6 return ib[3] aaa(ib,4,4) v=8 return ib[4] aaa(ib.5,9) v=28

aaa(ib,0,9) 28 aaa(ib,0,4) u=8 aaa(ib.5,9) v=28 aaa(ib,5,7) u=28 aaa(ib,5,6) u=12 aaa(ib,5,5) u=6 return ib[5] aaa(ib,6,6) v=12 return ib[6] aaa(ib,6,7) v=28 aaa(ib,6,6) u=12 return ib[6] aaa(ib,7,7) v=28 return ib[7] aaa{ib,8,9) v=16 aaa(ib,8,8) u=16 return ib[8] Aaa(ib,9,9) v=11 return ib[9]