Class Examples 7/18/02 Example #1 ---------- #include int f(int,int); void g(int,int); void main() { int i = 10; int j = 20; i = f(j,i); g(i,j); cout << "I = " << i << endl; cout << "J = " << j << endl; } int f(int i,int j) { j = i - j; i = j - i; cout << "I = " << i << endl; cout << "J = " << j << endl; return i-j; } void g(int i,int j) { i = i + j; j = j - i; cout << "I = " << i << endl; cout << "J = " << j << endl; } Example #2 ---------- #include const int SIZE = 4; const double TIP_RATE = 0.15; int Power(int, int); int Triangle_Number(int); int Fibonacci(int); void Tip_Chart(int, int); void Print_Triangle(int,char); void Print_Line(int, char); bool Search(int[], int, int, int); int Num_Inc_Subseq(int[], int, int, int, int); // Precondition: exponent is a non-negative integer. // Postcondition: Function returns the value base^exponent. int Power(int base, int exponent) { if (exponent == 0) return 1; else return base*Power(base,exponent-1); } // Precondition: n is a non-negative integer. // Postcondition: Function returns the nth triangle number. int Triangle_Number(int n) { if (n == 0) return 0; else return n + Triangle_Number(n-1); } // Precondition: n is a non-negative integer. // Postcondition: Function returns the nth Fibonacci number. int Fibonacci(int n) { if (n<2) return n; else return Fibonacci(n-1)+Fibonacci(n-2); } // Precondition: Both first_val and last_val are positive. // Postcondition: The function will print out a tip chart at 15% for each // dollar increment from the first_val to the last_val. void Tip_Chart(int first_val, int last_val) { if (!(first_val > last_val)) { cout << "On a meal of $" << (double)first_val << " you should tip $"; cout << first_val*TIP_RATE << endl; Tip_Chart(first_val+1, last_val); } } // Precondition: n is a non-negative integer. // Postcondition: Will print a line of the character c, exactly n times, // followed by a new line character. void Print_Line(int n, char c) { for (int i=0;i 0) { Print_Triangle(n-1,c); Print_Line(n,c); } } // Precondition: low and high must be valid indices to the array list. // Postcondition: The function will return true if value is stored in one // of the indices in the array, in between low and high, // inclusive. bool Search(int list[],int low, int high, int value) { int midindex; // Check trivial case & assign the next "guess" for the search. if (high < low) return false; else midindex = (high+low)/2; // Either return true if value is found, or narrow search accordingly. if (value == list[midindex]) return true; else if (value > list[midindex]) return Search(list, midindex+1, high, value); else return Search(list, low, midindex-1, value); } // Precondition: low and high are valid indices to the array list, and // len must be a positive integer less than or equal to the // size of the array list. // Postcondition: Returns the number of non-decreasing sequences of numbers // of length len in the array list in between the indices // of low and high, inclusive. int Num_Inc_Subseq(int list[], int cur_val, int low, int high, int len) { // First take care of base cases. if (len == 0) return 1; else if (high < low) return 0; else { // Check to see if current value can create new sequences. int bonus = 0; if (list[low]>= cur_val) bonus = Num_Inc_Subseq(list, list[low], low+1, high, len-1); // Add these to sequences found in the rest of the array. return Num_Inc_Subseq(list, cur_val, low+1, high, len) + bonus; } }