-->

Sunday, October 13, 2013

Pointer constants in C language

Definition  

As we store any data (information) in our memory, the computer also stores data in its memory. The computer memory is divided into a number of location called storage cells. Each location can hold one bye of data and each location is associated with address. Let us assume size of memory is 64K where 1K=1024bytes. So , total number of memory locations =64K = 64X 1K

= 64 X 1024 bytes

=65536 locations

Logically, all 65535 locations in computer memory are numbered sequentially from 0 to 65535 but, physically they are divided into even bank and odd bank. Even bank is set of memory locations with even address and odd bank is set of memory locations with odd addresses as shown as below:

Pointer constants in C


These memory addresses are called pointer constants. We cannot change them ; but, we can only use them to store data values.
for example, in the above memory organization, the address ranging from 0 to 65535 are pointer constants.

How to Split String in Equal Parts: C#

String is a text of Unicode characters in any programming language, it can be changeable as the user want. Splitting a string means to divide a string in an array after performing some operations on it. These operation may be to remove desired character, blank spaces or find out a substring of a particular length.

A string can be split into chunks of equal size, given by the user. Create two string variable, first will contains the original string that is to be split, second string is empty and will contain the split string.

Write the following code in which for loop is used to get our desired chunk size. I am dividing the string in chunks of 4.
string stringToDivide = "abcdefghijklmnopqrstuvwxyz";
string stringAfterDivide = string.Empty;
int part = 4;
int stringLength = stringToDivide.Length;
for (int i = 0; i < stringLength; i += part)
{
if (i + part > stringLength) part = stringLength - i;
stringAfterDivide += stringToDivide.Substring(i, part) + " ";
}
MessageBox.Show(stringAfterDivide);

I am using alphabets as original string, and at the last the string will be divide in chunk of four as shown in below message box.

How to split string in equal parts in windows forms C#


The string can be changed according to the user’s requirements, or may be input by the user through the textbox. The chunk size can also be changed, or may also be input by the user. 

Saturday, October 12, 2013

Binary Search in c Language

Binary Search

To overcome the disadvantage of linear search, we generally use binary search when the elements are sorted. Now, let us see " What is binary search? What is the concept used in binary search?"

Definition : Binary search is a simple searching technique which can be applied if the items to be compared are either in ascending order or descending order.

The general idea used in binary search is similar to the way we search for the meaning of a word in dictionary. Obviously , we do do not use linear search. Instead , we open the dictionary in the middle and the word is compared with the element at the middle of the dictionary. If the word is found, the corresponding meaning is retrieved and the searching is finished. Otherwise , we search either the left part of the dictionary or the right part of the dictionary. If the word to be searched is less than the middle element, search towards left otherwise , search towards right. The procedure is repeated till key(word) is found or the key item (word) is not found.

Once we know the concept of binary search, the next question is "How to search for key in a list of elements?"

Now, let use see how to search for an item. The procedure is shown below:

Procedure: The program can be designed as follows. Here, low is considered as the position of the first element and high as the position of the last element. The position of the middle element can be obtained using the statement: mid=(low+high)/2;
the key to be achieved using the statement:
if(key==a[mid])
{
printf("Sucessful search");
exit(0);
}
After executing the statement , if(key==a[mid]), if it is true, the message "successful search" is displayed. If this condition is false, key may be in the left part of the array or in the right part of the array . If the key is less than the middle element, the array in the left part has to be compared from low to mid-1. Otherwise, the right part of the array has to be compared from mid-1 to high.
This can be achieved by using the following statement:
if(key<a[mid])
high=mid-1;
else
low=mid+1;
Note that, as all the above statement are executed repeatedly, the gap between low and high will be reduced. Finally, the value of low may exceed the value of high, in that case we say that key is not found.
Now, the C program takes the following form:

low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(item==a[mid])
{
printf("Sucessful search");
exit(0);
}
if(item<a[mid])
high=mid-1;
else
low=mid+1;
}
printf("UN-Successful search");

You are already familiar with the algorithm and flowchart of binary search. Now the complete C program is shown below:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,key,a[10],low,mid,high;
printf("Enter the value of n:\n");
scanf("%d",&n);
printf("Enter n values\n");
for(i=0;i<n;i++)
scanf("%d",,&a[i]);
printf("Enter Key to search");
scanf("%d",&key);
/* Initialization*/
low=0;
high=n-1;
while(low<=high)
{
/* Find the mid point */
mid=(low+high)/2;
/* Key found */
if(key==a[mid])
{
       printf("Sucessful");
       exit(0);
}
if(key<a[mid])
       high=mid-1;
else
       low=mid+1;
}
printf("UN-Sucessful");

}


Binary Search in c Language


Note: The necessary condition for binary search is that the list of elements should be sorted. Otherwise, binary search should not be used. If elements are not sorted and are very less, normally we go for linear search. But, if the elements are more and not sorted, it is better to sort them and use binary search.

Advantages of binary search
  • Simple Technique 
  • Very efficient searching technique.
Disadvantage:
  • The list of elements where searching takes place should be sorted.
  • It is necessary to obtained the middle element which is possible only if the elements are sorted in the array. If the elements are sorted in linked list , this method can not be used.

Check If File Exist or Not at Specified Location: C#

We can specify the extension of files, to be opened using OpenFileDIalog as I have explained in Use Filters in Open File Dialog. Let suppose user have been copied a file at a specified location and save that location for future reference. After some time, when user want to use that file with that location and, the file is not there, then it will throw an exception.


To avoid that exception, we have to check that file’s existence at the given location. The existence of file can be check through File.Exist() method, which resided in System.IO.File class in Visual Studio.

The following c# code will check the file named “ab.jpg” in our current directory which is Debug folder of the solution.
if (File.Exists(Environment.CurrentDirectory + "\\ab.jpg"))
{
//Write your code to execute
}
else
MessageBox.Show("File Not Exist");

If the file exist at the debug folder, it will execute our line of code, and if file not exist, it will show a message containing “File Not Exist”.

How to check file's existence in windows forms C#


See Also: Use Filters to Open Desired Files

Friday, October 11, 2013

How to Specify Filters in OpenFileDialog: Windows Forms

To open an existing file on the system, open file dialog box is used. Create an object of OpenFileDialog class and call the ShowMethod() to open this type of dialog box. I have explained about this dialog box in my previous article i.e. Use OpenFileDialog to open an existing file.

In many Software, we can select only the specified type of file such as excel, word or text files. In Windows forms we can also use this feature by using filters for this dialog box.

Add a windows form project and write the following code in C# language:
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "jpg files: |*.jpg";
ofd.Title = "Images";
ofd.ShowDialog();

Now in the above code, I have specified the type of file, user have to select. Using above code the user have to select only jpg files. Run this code and it will show an open file dialog look like the following:

How to use filters in open file dialog: Windows Forms C#

We can change these filter as per our requirements, even we can specify some more filters in a single file dialog. Check out the below line of code:

ofd.Filter = "jpg files: |*.jpg; *.jpeg;*.png";

The above line, when used, can select these three type of files as specified.

Previous:  Open File Dialog in Windows Forms                 Next: Check If File Exist or Not

How to Use Images in Treeview: WPF

Image control can be placed in any other control such like Treeview, Toolbar control or any other controls. The images can be stored in your solution files. To place an image in Treeview control using following simple steps.

Place a stackpanel with horizontal orientation and look like following structure:
 <StackPanel Orientation="Horizontal" Height="30">
<Label Content="first"></Label>
<Image Source="buttonImage.png"></Image>
</StackPanel>

The XAML code will place a label and an image in a stack panel. Now to use this in treeview control just place it in between the treeview item code.
<TreeView HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5">
<TreeViewItem Header="Item1">
<StackPanel Orientation="Horizontal" Height="30">
<Label Content="first"></Label>
<Image Source="buttonImage.png"></Image>
</StackPanel>
</TreeViewItem>
</TreeView>

A single treeview item will be placed in the WPF window. Now write the following code to place three treeview items each having an image:
<TreeView HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5">
<TreeViewItem Header="Item1">
<StackPanel Orientation="Horizontal" Height="30">
<Label Content="first"></Label>
<Image Source="buttonImage.png"></Image>
</StackPanel>
</TreeViewItem>
<TreeViewItem Header="Item2">
<StackPanel Orientation="Horizontal" Height="30">
<Label Content="second"></Label>
<Image Source="image1.png"></Image>
</StackPanel>
</TreeViewItem>
<TreeViewItem Header="Item3">
<StackPanel Orientation="Horizontal" Height="30">
<Label Content="third"></Label>
<Image Source="image2.png"></Image>
</StackPanel>
</TreeViewItem>
</TreeView>

Run the project and expand each of the node of treeview, each node have its label and an image as shown in the following image:

How to use images in Treeview control WPF XAML

See Also: Place an Image in Toolbar

Introduction of WPF TreeView Control

Treeview control is used to display our data in hierarchical form, where some of the nodes are parent and some of them are children of their parents. In the left side of windows explorer there is a treeview containing some libraries, or our computer shortcuts and some favourites.

The parent node can be expanded or collapsed by the user. A simple treeview containing some nodes can be designed by the following XAML code:


<TreeView HorizontalAlignment="Left" VerticalAlignment="Top">
<TreeViewItem Header="Item1"/>
<TreeViewItem Header="Item2"/>
<TreeViewItem Header="Item3"/>
<TreeViewItem Header="Item4"/>
</TreeView>

The code will place a treeview contains four items as described in above code. The code specifies that the treeview will placed in the left and top of the window having margin of 5 points as shown in the following image:

How to use Treeview and add some children in WPF XAML

Now we have to add some children for the treeview item added above. Write the following XAML code:
<TreeView HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5">
<TreeViewItem Header="Item1">
<TreeViewItem Header="Child 1"></TreeViewItem>
<TreeViewItem Header="Child 2"></TreeViewItem>
</TreeViewItem>
<TreeViewItem Header="Item2">
<TreeViewItem Header="Child 1"></TreeViewItem>
<TreeViewItem Header="Child 2"></TreeViewItem>
</TreeViewItem>
<TreeViewItem Header="Item3">
<TreeViewItem Header="Child 1"></TreeViewItem>
<TreeViewItem Header="Child 2"></TreeViewItem>
</TreeViewItem>
</TreeView>

Run the project and it will show three parent nodes, each contains two children, as shown in the following image:

How to use Treeview and add some children in WPF XAML

Like above children we can add more children to the parent node and a children can also have its children. The same process will follow up to add nested nodes.

See Also: How to use Treeview in Windows Forms
© Copyright 2013 Computer Programming | All Right Reserved