Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Wednesday, 29 April 2009

Exchanging two variables value without using temporary storage in C language

You can exchange values in C language without the need of the temporary storage location using three bitwise Exclusive-OR operator, look at code below:



#include <stdio.h>

int main(int argc, char *argv[])
{
int A;
int B;

A = 16;
B = 30;

printf("A=%d B=%d\n",A,B);
A ^= B;
B ^= A;
A ^= B;
printf("A=%d B=%d\n",A,B);

return 0;
}





The code after compiled will exchange value of variabel A with B, where A = 16 and B = 30 become A = 30 and B = 16.

How it works?
Value of A is XOR-ed with B value, the result then stored in A, then B XOR-ed with the altered A , this will produce the original value of A, this value then stored in B. Last, altered A value is XOR-ed with the altered B, this will produce the original B.


Here the schematically detail of its process:

A = 16 (decimal) --> 10 (hexadecimal) --> 0001 0000 (binary)
B = 30 (decimal) --> 1E (hexadecimal) --> 0001 1110 (binary)

on A ^= B statement, A being XOR-ed with B:

0001 0000 --> A
0001 1110 --> B
---------- XOR
0000 1110 --> A

the result 0000 1110 then stored in A,

on B ^= A statement, B being XOR-ed with A:

0001 1110 --> B
0000 1110 --> A
---------- XOR
0001 0000 --> B

the result 0001 0000 (16 decimal) stored in B,

on A ^= B statement, B XOR-ed with A:

0000 1110 --> A
0001 0000 --> B
---------- XOR
0001 1110 --> A

the result 0001 1110 (30 decimal) stored in A.

So the final result is:
A = 30 dan B = 16.



Have a nice try :).

Monday, 20 April 2009

Simple GUI with MATLAB® pragmatically programming

You must now what is a GUI before trying this stuff. A graphical user interface (GUI) is a graphical display that contains devices, or components, that enable a user to perform interactive tasks. To perform these tasks, the user of the GUI does not have to create a script or type commands at the command line. Often, the user does not have to know the details of the task at hand.

The GUI components can be menus, toolbars, push buttons, radio buttons, list boxes, and sliders, just to name a few. In MATLAB® software, a GUI can also display data in tabular form or as plots, and can group related components.

The following figure illustrates a simple GUI.















The GUI contains
-A push button component
-An edit button component

When you click a push button, A string "Hello World" will be showed in edit box.


How Does a GUI Work?
Each component, and the GUI itself, is associated with one or more user-written routines known as callbacks. The execution of each callback is triggered by a particular user action such as a button push, mouse click, selection of a menu item, or the cursor passing over a component. You, as the creator of the GUI, provide these callbacks.

So what Is a Callback?
A callback is a function that you write and associate with a specific component in the GUI or with the GUI figure itself. The callbacks control GUI or component behavior by performing some action in response to an event for its component. The event can be a mouse click on a push button, menu selection, key press, etc.

Okay, too much talking (too long so boring, I too exhausted to write this hahaha :) ). Here the source code listing:



function simple_gui
% Simple GUI with MATLAB to show how it works

% Initialize and hide the GUI as it is being constructed
fh = figure('Visible', 'off', 'Position', [360, 400, 300, 180]);

%Construct the components
% Push Button
hButton = uicontrol(fh, 'Style', 'pushbutton',...
'String', 'Push Button',...
'Position', [20, 120, 70, 25],...
'Callback',{@hButton_Callback});
% Edit Button
hEdit = uicontrol(fh, 'Style', 'edit',...
'String', '',...
'Position', [100 115 130 35]);

% Make the GUI visible.
set(fh,'Visible','on');

% Callback for hButton to show string 'Hello World' in edit Button
function hButton_Callback(source, eventdata)
set(hEdit, 'String', 'Hello World');
end

end







Well, i will explain it shortly,


function simple_gui
.
.
.
end
Every MATLAB® script with callback should be started by 'function' and ended with 'end' because this script is written using nested functions.

% blablabla
Every statement starting with '%' is a comment and will not executed by MATLAB®.


fh = figure('Visible', 'off', 'Position', [360, 400, 300, 180]);
This statement ini create figure object called fh, not visible to user. A GUI is a figure object. The Position property is a four-element vector that specifies the location of the GUI on the screen and its size: [distance from left, distance from bottom, width, height]. Default units are pixels.


hButton = uicontrol(fh, 'Style', 'pushbutton',...
'String', 'Push Button',...
'Position', [20, 120, 70, 25],...
'Callback',{@hButton_Callback});
This statement create a push button user interface control objects hButton, With fh as its GUI parent, has a String 'Push Button' on it, locate in a position 20 from left of screen, 120 from bottom screen, 70 width, 25 height, also has a callback function 'hButton_Callback' in response with event on this object.


hEdit = uicontrol(fh, 'Style', 'edit',...
'String', '',...
'Position', [100 115 130 35]);
Same explanation with hButton, but the object is an edit box.

set(fh,'Visible','on');
Setting property fh so it will be shown in screen with its components (a push button and edit box).


function hButton_Callback(source, eventdata)
set(hEdit, 'String', 'Hello World');
end
This part is a callback to hButton object. When the push button is pressed it will trigger edit box to show 'Hello World' on it.


Save the source code in a text file with extension M or m, then run it from MATLAB® command line.















Reference:
1. MATLAB® R2008A help file.
2. My fresh brain :).



Have a nice Try,


Juan Rio Sipayung

Tuesday, 31 March 2009

How to extracting many zip file at once

Extracting 1 to 5 zip files isn't a hard job, we just choose the files then right-click on the files then choose extract.

How about extracting many zip files, for example 100 files or maybe 1000 files. Well, this job really become tedious and frustrating (honestly, i ever do this hard job, its torture me, believe me....). so, what is the solution?

I finishing him (i mean the problem), with making a bash shell script (i got this idea from book "Mastering Unix Shell Scripting" by Randal K. Michael published by wiley). here the script (i hope you already knew what kind of creature is shell script he he he..):





#!/bin/sh

# extract.sh, created at 15 February 2009
# (c) Juan Rio Sipayung aka Joielechong, GPL
# This Script will extracting all file in this script current directory
# to '/destination', you can change '/destination' to whatever destination
# folder you want e.g '/home/user' or '/tmp', of course
# you must have the write permission.


# This variable use to store the numbers of zip file being extracted
count=0

# Do extracting each zip file until no more
for file in *.zip
do
# unzipping file, -d option allows extraction in an arbitrary directory
# here we extracting to '/destination' as example, change '/destination'
#to your destination folder
unzip $file -d /destination
# Count each zip file being extracted
count=$(($count+1))
done

# This part telling user that no zip file in the current directory
# Still not working i don't know why, if you know tell me :).......
#if [ "$count" = 0 ]; then
# echo "No zip file is extracted"
#fi

# show how many the zip file being extracted
if [ "$count" != 0 ]; then
echo "$count zip file extracted"
echo "Extracting complete"
fi

exit 0





Save this source code and name it extract.sh, and put it on the folder where your zip files are, then run it from terminal and wait for the result :).....


notes:
1. Every single step was done on system with Slackware 12.2 with KDE desktop manager.
2. This script can be use to extracting rar file, tar.gz file or other compression file type, of course you must do a couple change to this script.




Happy Hacking,


Juan Rio Sipayung