|
Last updated on:
10 November 2008 06:01:33 PM
This page contains miscellaneous information
that can't be fitted to any other categories. It is mostly related to
programming language tips & tricks. And this may contain junks also...
(things that are not related with programming)
Let's say "Hello World"
"Hello World" is a well known programming term, widely used when teaching
programming lessons to a novice. It is a name of your first program created
with a programming language that is new to you. Hello World programs
simply print out the sentence "Hello World" to the screen/stdout,
and here are some of them:
In C:
#include <stdio.h>
void main() {
prinft("Hello World\n");
}
In C++:
#include <iostream.h>
void main() {
cout << "Hello World" << endl;
}
In Pascal:
program HelloWorld;
begin
writeln('Hello World');
end.
In Java:
class HelloWorld {
public static void main(String[] args) throws IOException
{
System.out.println("Hello World");
}
}
In Visual Basic:
Sub Form_Load()
msgbox "Hello World"
End Sub
In Scheme:
(write-line "Hello World")
Approximation
This is the way computer calculate complicated mathematical functions such
as Sqrt, Sine, Cosine, Tangent, etc.
Approximation will cause a loss in precision and this can cause some
problem.
Example:
if (sqrt(4) == 2) break; /* what happens if sqrt(4) returns 2.00000001 ???
*/
However, you can fix this problem using epsilon
Example:
if (sqrt(4)-2 < 0.001) break;
C
Language Shortcuts
These shortcuts are mainly available for C/C++ language.
i = i+1;
is equivalent to
i++; /* other example of this shortcut: i--; for i=i-1; */
i = i+2;
is equivalent to
i += 2; /* other example of this shortcut: -=, *=, /=, %=, etc */
index++;
if (index>=n) index=0;
is equivalent to
index=(index+1) mod n;
index--;
if (index<0) index=n-1;
is equivalent to
index=(index+n-1) mod n;
if (a<b) min = a; else min = b;
is equivalent to
min = a<b ? a : b; /* the_statement ? if_true : if_false, is the shortcut
for single if-else */
#include <math.h>
int a=7;
int a_div_two_round_up = (int) ceil(a/2); /* result: 4 */
is equivalent to
int a=7;
int a_div_two_round_up = (a+1)/2; /* result: 4, you don't have to use
floating point */
#include <math.h>
int a=7;
int a_div_two_round_down = (int) floor(a/2); /* result: 3 */
is equivalent to
int a=7;
int a_div_two_round_down = a/2; /* result: 3, operator / by default is an
integer division */
Swapping two integer values
Swapping two integer values is important, especially in a sorting
algorithm that involves integer numbers. There are several ways to do it.
Let's see the methods one by one starting from the most obvious one.
1. Use a
temporary variable, the most straightforward implementation.
#include <stdio.h>
void swap(int *a,int *b) {
int temp;
temp = *a; // a = 7 ; b = 49; temp = 7
*a = *b; // a = 49; b = 49; temp = 7
*b = temp; // a = 49; b = 7 ; temp = 7
}
void main() {
int num1 = 7,num2 = 49;
printf("%d %d\n",num1,num2);
swap(&num1,&num2);
printf("%d %d\n",num1,num2);
}
2. Only use
2 variable, a bit slower than first method because this method involves
computation, but use less memory.
#include <stdio.h>
void swap(int *a,int *b) {
// initially a = 7; b = 49
*a += *b; // a = 56; b = 49
*b = *a - *b; // a = 56; b = 7
*a -= *b; // a = 49; b = 7
}
void main() {
int num1 = 7,num2 = 49;
printf("%d %d\n",num1,num2);
swap(&num1,&num2);
printf("%d %d\n",num1,num2);
}
3.
Finally, we arrive at the best method so far, using XoR. This is the
most efficient because it doesn't use additional variable as temporary
storage and it is the fastest because all swaps occurs in-situ, you only
change 'bits' instead of changing the whole variable. XoR operations is
faster than additions/substractions. See the example below. Btw you know
what XoR does, doesn't you?
#include <stdio.h>
void swap(int *a,int *b) {
// initially a = 000111 ( 7) and b = 110001 (49)
*a ^= *b; // a = 110110 (54) and b = 110001 (49)
*b ^= *a; // a = 110110 (54) and b = 000111 ( 7)
*a ^= *b; // a = 110001 (49) and b = 000111 ( 7)
}
void main() {
int num1 = 7,num2 = 49;
printf("%d %d\n",num1,num2);
swap(&num1,&num2);
printf("%d %d\n",num1,num2);
}
Btw, this trick is only applicable for
integers. If you want to swap floating point numbers or strings, please
revert back to the first (direct) method.
Discrete
Mathematics
Discrete means separated from the others.
The
name Discrete Mathematics comes from the distinction between
continuous and discrete mathematical objects. Discrete Mathematics
concerns processes that consist of a sequence of individual steps.
The ideas of Discrete Mathematics underlie the science and
technology specific to the computer age.
What
you'll learn if you take Discrete Mathematics are: Proof
Technique, Sets, Relations, Functions, Graphs, Trees, Pigeon Hole
Principle, Counting & Combinatorics, Probability and many more
which will be useful for any other areas in Computer Science.
This document, 12_miscellaneous.html, has been accessed 5179 times since 26-Jul-04 20:33:15 SGT.
This is the 1st time it has been accessed today.
A total of 2919 different hosts have accessed this document in the
last 1594 days; your host, 38.103.63.56, has accessed it 2 times.
If you're interested, complete statistics for
this document are also available, including breakdowns by top-level
domain, host name, and date.
|