Lecture Notes:  Esc 101

Date: 23/01/2008         

                                     

In the last class we looked at the following code for computing integer square root of a number x. The code below takes x = 25.

int low,high,mid;

int x = 25;

low = 0;

high = x;

while(low<high){

    mid = (low + high)/2;

    if(mid*high<x) low = mid;

    else high = mid;

}

However, the above code contains a bug. The program goes into an infinite loop as the terminating condition for the while loop ie. low<high is always true. To find out where this program goes wrong, we perform what is called as debugging by tracing the program step by step. The variables (boxes) gets modified after each iteration as shown below.

                    low             high            mid           x

iter 1             0                25               12           25

iter 2            0                12                12           25

iter 3            0                6                   6            25

iter 4            3                6                   3            25

iter 5            3                6                   4            25

iter 6            4                5                   5            25

iter 7            4                5                   4            25   ---> no progress hereafter, the variable mid keeps switching values between 4 and 5

The problem in the code is that the interval length between low and high keeps decreasing and it cant decrease below 1 which is when the code stops changing any further values and repeats the same operations.                                                                  Therefore, a possible fix for this problem can be to replace the terminating condition  (low<high)  by (high - low > 1) for the while loop.

Now, that the code runs fine, we will see which variable holds our final answer. It can be seen that the variable high holds the square root of the number x, but only when x is a perfect square. In other cases, it holds value 1 greater than desired answer. So, the following lines of code needs to be added in order to complete the program.

if((high * high)!=x)

high = high - 1;

 

do-while loop:

general format of do-while:

do{

    Body

}while (condition)

Its advantage over while or for loop is that the "Body" executes at least once, even if the "condition" is not true.

eg. finding the binary representation of a number. This would require a while loop with a terminating condition (x>0) followed by the body. For the case of x = 0, the body would never be executed with a simple while. This is where a do-while loop finds its utility.

Introduction to Strings and its usage in Implementation for finding representation of a number  x in any base b.

String : sequence of characters eg. "AB", "true" etc. Declared by keyword "String". eg. String s = ""; initializes a string s to null.

=>    '+' operator, when operated on two strings, concatenates them without modifying any of the two strings.

Code:

String s;

int digit, int n = 10; int b = 3;

do{

    digit = n%b;

    n = n/b;

    s = digit + s;

}while (n>0)