Lec17 18
Lec17 18
Lec17 18
I Semester 2008-09
Lecture 17+18
1
Syntax of a method
2
Methods offer a structured (top-down) way of writing the programs
3
Advantages of structured programming
4
Two Examples of Structured Programming
5
P : Computing Max Perm for an integer
P : MaxPerm(n)
6
P : Computing Max Perm for an integer
P : MaxPerm(n)
Q: Perm_with_Largest_Digit_at_MSI(n)
7
P : Computing Max Perm for an integer
P : MaxPerm(n)
Q: Perm_with_Largest_Digit_at_MSI(n)
8
P : Computing Max Perm for an integer
P : MaxPerm(n)
Q: Perm_with_Largest_Digit_at_MSI(n)
9
The method Num of digits(int n)
// function for computing number of digits in n
public static int Num_of_Digits(int n)
{
int digits = 0;
while(n>0)
{
digits = digits + 1;
n = n/10;
}
return digits;
}
10
The method Power of Ten(int i)
// function for computing 10ˆi.
public static int Power_of_Ten(int i)
{
int power = 1;
while(i>0)
{
power = power*10;
i = i-1;
}
return power;
}
11
The method for Max Digit
public static int Max\_Digit\_Index(int n, int digits)
{ int max_digit = n%10;
int max_digit_index=1;
n = n/10;
for(int i=2; i<=digits; i = i+1)
{
int current_digit = n%10;
if(current_digit>max_digit)
{
max_digit = current_digit;
max_digit_index = i;
}
n = n/10;
}
}
12
The method : Value of Digit of n at Index()
// function for computing the digit of n at ’index’
public static int Value_of_Digit_of_n_at_Index(int n, int index)
{
int power = Power_of_Ten(index-1);
n = n/power;
return(n%10);
}
13
The method : Permutation with Max Digit at MSI
public static int Permutation_with_Max_Digit_at_MSI(int n, int digits)
{
int power = Power_of_Ten(digits-1);
int digit_at_MSI = n/power;
int max_digit_index = Max_Digit_Index(n,digits);
int value_of_max_digit = Value_of_Digit_of_n_at_Index(n,max_digit_index);
int new_n=n;
if(max_digit_index != digits)
{
int difference_of_digits = value_of_max_digit - digit_at_MSI;
int lower_power = Power_of_Ten(max_digit_index-1);
new_n = n + (power-lower_power)*difference_of_digits;
}
return new_n;
}
14
The method Max Perm(int n)
public static int Max_Perm(int n)
{ int upper_n;
int lower_n;
int digits = Num_of_Digits(n);
int power= Power_of_Ten(digits);
for(int i = digits; i>1; i =i-1)
{ upper_n= n/power;
lower_n= n%power;
lower_n= Permutation_with_Max_Digit_at_MSI(lower_n,i);
n = upper_n*power+ lower_n;
power = power/10;
}
return n;
}
15
P : Computing Max Perm for an integer
P : MaxPerm(n)
Q: Perm_with_Largest_Digit_at_MSI(n)
16
II : Common Steps in Problem Solving
17
An interesting example : Clever Prisoner problem
18
An easier problem
// The following function computes the position of the prisoner
// who will be sentenced to life term when there are n prisoners
// in the row and the selection-number is m
public static int The_Unlucky_Position(int m, int n)
{
???
19
An easier problem
// The following function computes the position of the prisoner
// who will be sentenced to life term when there are n prisoners
// in the row and the selection-number is m
public static int The_Unlucky_Position(int m, int n)
{
if(m%n==0)
return n;
else
return m%n;
}
20
A simple instance : what is Clever Prisoner Position(2,m) ?
21
Extending to the case : n=3 for any m
22
Extending to the case : n=3 for any m
Based on the key idea of previous slide there are two cases :
23
Extending to the case : n=3 for any m
Based on the key idea of previous slide there are two cases :
24
Solving the problem in full generality
1. Unlucky Position(n+1,m)>i :
Clever Prisoner Position(n+1,m)=i
2. Unlucky Position(n+1,m)<=i :
Clever Prisoner Position(n+1,m)=i+1
25