Learning to Drive A Car – Day 6

The day it dawns on you that the driver is no longer interested in teaching you anything 😦 I mean he has taught you all basics and now what’s left is explore that. There is some occasional clutch brake control and slight left, slight right, other than that nothing, virtually nothing.

So drove for 8 kms today in 1 hour :O ‘that’s all?’ You might ask. C’mon if you are not allowed to even touch the accelerator and move with only clutch and brake, how much more kms can you expect to drive? And that too driving through the worst mud roads I have ever witnessed in Bangalore, you know the places where it was deep forest and was acquired at cheap rate by some big land mafia’s and getting converted to residential areas to lure the newcomers to Bangalore, ya the roads that lead to those areas are we talking about. It was essentially a torture to have today’s ride.

I feel that the 1 hour of journey is cut short by 9-10 mins these days, you know but somehow didn’t feel like complaining. Just 4 more days to go, why to pick up a fight and be cursed for life?

Learning to Drive A Car – Day 5

So today the actual day 5 of learning to drive a car took place. So basically we know what each thing does, what to do and not to do when a particular situation arises and the only thing left is to integrate all of them and apply as and when the situation arises.

So started the car, put on the seat belt, did a bit of reverse (the driver did it, I still have no clue how to do it on my own), then started cruising through the traffic and onto a familiar kachcha road. The most important thing to learn is the clutch and brake control for which kachcha road is the best. The driver was checking if I am bringing my leg over the accelerator. I managed to keep my legs hovering over clutch and brake alone. You know this, a fully released clutch can give you a speed of upto 15 km/hr, one of my friend told me. While on clutch and brake, I kept looking at the speedometer to check if it was true, ya and it was pretty close.

It was a bumpy ride through the un-leveled roads but was very good at cracking the ratio between the B and C. So mostly it was a ride on first gear and very scarce second gear if for some reason if we stepped on intermittent pakka roads.

It went on for an hour and it seemed pretty great.

I’m Learning C# – Part 6

Today we will study about methods or functions. We already know one method which is main (). In the statement, static void main (string [] args), main is the name of the function that returns nothing or void. 

How does a method work? First we need to define a method with the return type if any and also mention the arguments that will be passed to that method. Then we need to call the method from our program. The advantage of this is we can call the method any number of times without having to actually repeat the lines of code over and over.

The following is a code that makes use of method ‘area’ to calculate and print the area of a circle when the radius is inputted.

static double area (double x)
{
return 3.14 * x * x;

}
static void Main (string [] args)
{
Console.WriteLine(“enter the number”);
double num = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(“The area is {0}”, area(num));
}

If you have issues with remembering the order in which the arguments are defined in the methods, during passing you can name the arguments in the style given below:

static int volume (int x, int y, int z)
{
return x*y*z;

}
static void Main (string [] args)
{
int x, y, z;
Console.WriteLine(“The volume is {0}”, volume(x:4, z:5, y:2));
}

In these examples even though the parameters where passed to method and operations were performed on them, the actual values of the parameters remain the same. This is called ‘ call by value’.

static void num (ref int y)
{
y /= 2; ;

}
static void Main (string [] args)
{
int x = 10;
num(ref x);
Console.WriteLine(“x is {0} – call by reference”, x);
}

In call by reference, you are actually not making a copy of the parameter being passed but passing the memory location which means any operation you do in the method on the parameter will be stored in the same memory location itself. In the above case, the output will not be 10 but 5. Remember the ‘ref’ keyword is required in the method and while calling.

static void num (out int y)
{
y = 2;

}
static void Main (string [] args)
{
int x;
num(out x);
Console.WriteLine(“x is {0} – call by output”, x);
}

You use the out keyword when you want to pass value from the method to the main function. In this case, output is 2 which is nothing but the value of y passed into x through the out.

In the following code we use method overloading which means methods that have same name but only differ in the arguments.

static int sum (int x, int y)
{
return x+y;

}
static int sum(ref int x, ref int y)
{
x += 5;
y *= 2;
return x + y;

}
static int sum1(out int x, out int y)
{
x = 9;
y = 10;
return x + y;

}
static void Main (string [] args)
{
int x = 1, y = 2;
Console.WriteLine(“x is {0} y is {1} and returned values is {2}”, x,y,sum(y:3,x:5)); // x is 1 y is 2 and returned values is 8
sum(ref y, ref x);
Console.WriteLine(“x is {0} y is {1}”, x, y); //x is 2 y is 7
sum1(out y, out x);
Console.WriteLine(“x is {0} y is {1}”, x, y);//x is 10 y is 9
}

I’m Learning C# – Part 5

In the precious post, we had used if, else if and else in the code for for loop.  What it does is it states under this condition, you execute the following statements.

Another similar statement is the switch.

We are using the same code used for fibo nacci series

static void Main(string[] args)
{

Console.WriteLine(“enter the number of Fibonacci numbers required”);
int x = Convert.ToInt32(Console.ReadLine());
switch (x)
{
case 0: Console.WriteLine(“no numbers are printed”);
break;
case 1: Console.WriteLine(“The number is 0”); ;
break;
case 2: Console.WriteLine(“The numbers are 0 1 “);
break;
case 3: Console.Write(“The numbers are 0 1 1 “);
break;
default:
{
int y = 1;
int z = 1;
int sum = 0;
Console.Write(“The numbers are 0 1 1 “);
for (; x > 3; x -= 1)
{
sum = y + z;
Console.Write(” {0} “, sum);
z = y;
y = sum;

}
break;
}
}

}

We need to know what break is. Break is used to break free from the current execution statement and move on to the next. So if the break statement is found inside a loop, the execution inside the loop gets terminated and any remaining code outside the loop is executed next.

Another important terminology is continue. Continue skips just the current iteration of a loop but resumes with the rest of the loop iteration.

Now some logical operators:

&& – AND  (LHS and RHS should be True for the condition to be True)

|| – OR (either LHS or RHS should be True for the condition to be True)

! – NOT (works on single operand; if operand is false, the condition is True and vice versa)

I’m Learning C# – Part 4

Today we will be learning about loops. What does a loop do? Suppose we want to add the number 2 to 10, 4 times. So instead of writing 10+2+2+2+2 we go in for a loop where we show the action once and indicate that it has to be repeated.

While loop

Now here is a code which uses the while loop. It will keep on asking to “enter the number” until you enter the value 10 at which the while loop will exist. Only if the condition while(True) does the loop get executed.

static void Main (string [] args)
{
int x = 0;
while (x != 10)
{
Console.WriteLine(“enter the number”);
x = Convert.ToInt32(Console.ReadLine());
}
}

Another loop is the do – while. It’s working is similar to while loop with one difference. The loop will be entered atleast once before the while(True) check is carried out.

static void Main(string[] args)
{
int x = 0;
do
{
Console.WriteLine(“enter the number”);
x = Convert.ToInt32(Console.ReadLine());
} while (x != 10);
}

The same code above is implemented using a for loop below:

static void Main (string [] args)
{
int x = 0;
for (;x!=10;)
{
Console.WriteLine(“enter the number”);
x = Convert.ToInt32(Console.ReadLine());
}
}

Basically a for loop will constitute of 3 parts – initialization, condition and then the increment.

Let’s write a code which will print the fibonacci series

static void Main(string[] args)
{

Console.WriteLine(“enter the number of Fibonacci numbers required”);
int x = Convert.ToInt32(Console.ReadLine());
if (x == 0) Console.WriteLine(“no numbers are printed”);
else if (x == 1) Console.WriteLine(“The number is 0”);
else if (x == 2) Console.WriteLine(“The numbers are 0 1 “);
else if (x == 3) Console.Write(“The numbers are 0 1 1 “);
else
{
int y = 1;
int z = 1;
int sum = 0;
Console.Write(“The numbers are 0 1 1 “);
for (; x > 3; x -= 1)
{
sum = y + z;
Console.Write(” {0} “, sum);
z = y;
y = sum;

}
}
}

We have skipped the initialization part in this code because we already have the value of x from the console.

We can skip initialization and the increment part but the following is an infinite loop.

static void Main(string[] args)
{
for (;;);
}

Learning to Drive A Car – Day 5 (flopped)

Today was great, really great. Reached the car that was parked nearby. The driver was outside the car then and the car was locked. He handed over me the key, and asked to open, felt good and I did. Tried to reach out for the seat belt but it was broken, I smiled. And then started the car with the clutch and brake and inched a little only to be caught up in a signal for 5 minutes.

After the signal that seemed to take years, turned the vehicle to the left with the indicator on, the vehicle stopped. Tried to start again. Not starting. He told to release clutch, release brake, press accelerator, nothing happened. Car not moving at all. Finally after 2-3 minutes he told “petrol kaali, tomorrow class”. I felt like laughing inside. I said okay and got out of the car.

Anyway today’s class is not counted as an official class, so renaming it as Day 5 flopped.

I’m Learning C# – Part 3

Arithmetic operations – +, , *, /, %

Following is a code which will read in 2 numbers from the console and perform subtraction operation between them. Similar concept can be expanded to other operators.

int x;
int y;
Console.WriteLine(“Enter the numbers”);
x = Convert.ToInt16(Console.ReadLine());
y = Convert.ToInt16(Console.ReadLine());
Console.WriteLine(“difference = {0}”, x-y);

Main thing to remember is that ReadLine takes in only string. So it is required to convert it into the required data type by using the Convert.To<>.

If there are multiple operators in an equation, follow BODMAS.

x +=5; //This is same as x=x+5   This is a form of shorthand.

x++; //Same as x=x+1

++x and x++ are 2 different things. The first one increases the value of x then and there, the other one only in the next iteration.

int x=4; y= x++; //y =4, x= 5

int x=4; y=++x; //y = 5, x=5

Learning to Drive A Car – Day 4

Now it’s more practical driving than theoretical driving. Hence there is very little to explain and much to experience. Had a first hand experience of driving on a main road with fear gripping the mind that if the driver doesn’t look at what you are doing, will you go on to hit someone terribly bad, even to cause irreversible injury? It’s all too risky. You have so much control over whether to kill or let them live.

Had a new route today and it was pretty smooth throughout. Wasn’t allowed to hit the accelerator at all for the maximum span of time. Just the clutch brake mechanism. After a while it gets boring because it’s too slow that I decide to hit the accelerator and get this dialogue from the driver, ” bad road, only clutch brake control, no accelerator”. Could go up to only first and second gear. Multiple stops and starts resulting from pressing the brake too much happened. It takes quite sometime to make the theory practical, to think quickly involuntarily and I am yet to reach that stage where I can take decisions before I think about it.

The pleasure of driving is amazing, the initial stage is the toughest.

I’m Learning C# – Part 2

Hope the visual studio community edition is installed and running.

Let’s create our first program using it.

capture

Once the code is written, you will build it and then play it. The green colour on the side of the code indicates the code is saved or build. Otherwise it will be yellow in colour.

This is the basic Hello World Program. Main points to note are every code you write should have a main () function, that’s where the execution begins.

Noticed a WriteLine – This will send the cursor to the next line after “Hello World” is displayed. You can use just Write if you don’t want a newline.

Console is the output window where the string will be displayed to the user.

Now that we know how to display something to the user, we need to learn how to get some user input.

ReadLine is the command to read something from the console window.  Try the following code snippet inside the main function.

String message;
Console.WriteLine(“Enter your name”);
message = Console.ReadLine();
Console.WriteLine(“Hello {0}”, message);

To make the code meaningful we use comments. eg: int age; // declaring age

We can have comments extending over multiple lines by /* …….. */

Learning to Drive A Car – Day 3

Today was different, it was a totally new experience. ABC – accelerator, brake, clutch. Then gears 1 and 2. So you start vehicle in first gear, that time, you should have the clutch full down.Then you release slowly and the vehicle will start to move. Accelerate as required. Brake to slow.

It seems easy but no. There is various permutations and combinations of these 3 pedals at various times and scenarios which is pretty confusing. For eg: in a muddy road, only clutch and brake, don’t touch accelerator, vehicle will jump. In junctions, half clutch and slight brake to slow down. At this time if you apply more brake, vehicle will stop 😦

For right turn and left turn, same as above. It’s the quantity of clutch and brake that determines your driving efficiency. It takes time, a lot of time, to master it. When the speed increases, the vehicle vibrates, you shift to next gear.

The main thing what happened was just after I shift to second gear, it would be some crossing or left turn or right turn and since I wasn’t able to control the quantity of my braking, the vehicle stopped often. Again first gear and on and on.

It really felt good, better than the previous days because you feel you are at the least handling all the controls of the vehicle even though not entirely. Each of the pedals are still controlled by the driver so you don’t actually get an idea whether your efforts are enough or not. Hope that my efforts constitute the major share of what moves the car forward 🙂