-->

Wednesday, February 26, 2014

How to find absolute number of a given number in c# programming

In c# programming we easily find absolute number of a given number. We can use Abs function, which is include in Math class. Lets take an simple example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            sbyte sortbyte1 = -12, sortbyte2 = 12;
            short shnumber1 = -13, shnumber2 = 13;
            int integer1 = -14, integer2 = 14;
            long long1 = -15, long2 = 15;
            float float1 = -16.0f, float2 = 16.0f;
            double double1 = -17.1, double2 = 17.1;
            Decimal decimal1 = -18.0m, decimal2 = 18.0m;

            Console.WriteLine();
            Console.WriteLine("SortByte:   1) {0,-2} {1,-2}", Math.Abs(sortbyte1), Math.Abs(sortbyte2));

            Console.WriteLine("Integer 16 bit:   1) {0,-2} 2) {1,-2}", Math.Abs(shnumber1), Math.Abs(shnumber2));

            Console.WriteLine("Integer 32 bit:   1) {0,-2} 2) {1,-2}", Math.Abs(integer1), Math.Abs(integer2));

            Console.WriteLine("Integer 64 bit:   1) {0,-2} 2) {1,-2}", Math.Abs(long1), Math.Abs(long2));

            Console.WriteLine("Single:  1) {0,-2} 2) {1,-2}", Math.Abs(float1), Math.Abs(float2));

            Console.WriteLine("Double:  1) {0,-2} 2) {1,-2}", Math.Abs(double1), Math.Abs(double2));

            Console.WriteLine("Decimal: 1) {0,-2} 2) {1,-2}", Math.Abs(decimal1), Math.Abs(decimal2));

            Console.ReadKey();

        }
    }
}
Here Math class provide Abs static method for returning absolute value of a given number. Abs function is overloaded by some data types like double, decimal, Int16, Int32 and SByte.
Code generate the following output
How to find absolute number of a given number in c# programming

Memory representation of Linked List Data Structures in C Language

                                 Memory representation of Linked List

             In memory the linked list is stored in scattered cells (locations).The memory for each node is allocated dynamically means as and when required. So the Linked List can increase as per the user wish and the size is not fixed, it can vary.

               Suppose first node of linked list is allocated with an address 1008. Its graphical representation looks like the figure shown below:


      Suppose next node is allocated at an address 506, so the list becomes,



  Suppose next node is allocated with an address with an address 10,s the list become,


The other way to represent the linked list is as shown below:




 In the above representation the data stored in the linked list is “INDIA”, the information part of each node contains one character. The external pointer root points to first node’s address 1005. The link part of the node containing information I contains 1007, the address of next node. The last node of the list contains an address 0, the invalid address or NULL address.

Example:

The data stored in the linked list is: “The Is Another:
 Example Of Linked List”

Q. What is stored in the linked list shown below?



Q. What is stored in the Linked List shown below?



Tuesday, February 25, 2014

About the Default Action Methods in MVC Controller

Earlier article was about to add a controller (Student) and create its default views (having the same name as default actions). In this article we will discuss about the Index action which is used to show the list of particular table in database or any list container.

The index action by default have following code:
private StudentContext db = new StudentContext();
public ActionResult Index()
{
return View(db.Students.ToList());
}

In this code the first line is used to create the object of Student context class, which is used further to perform CRUD actions with the database. This action is simply a method having return type ActionResult which is basically encapsulates the result of an action method and used to perform a framework-level operation on behalf of the action method.

Just get in to the inner code of this action, having a single line of code, which is returning the view with the list of students stored in the database (from the db variable initialized in the first line).

Open the Index view in the Views>> Students folder having something like the below code:

@model IEnumerable<MvcApplication1.Models.Student>

<h2>Index</h2>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th></th>
    </tr>
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id })
        </td>
    </tr>
}
</table>

The first line contains the name of model specifying that this view is strongly typed view of the named mode. The total body of this view is in table written above. Table headers are bind with the model fields, it may be a simple string.

After the headers a loop is there for all the items contained in the model. The DisplayFor() method is used to show the value of name of item passed through the model and more fields have more headers as well as other fields.

GET and POST Create Action in MVC

C Program to implement doubly linked list for Data Structrue in 'C'

C Program to implement doubly linked list:

#include<stdio.h>
#include<alloc.h>
#include<stdlib.h>
#include<conio.h>
struct dlnode
{
Int info;
struct dlnode *link;
struct dlnode *prev;
}; typedef struct dlnode dln;
main( )
{
dln *root, *temp, *new, *ptr; char choice=’y’;
root=NULL;
while(choice= =’y’)
{
new=(dln*)malloc(sizeof(dln);
if(new= =NULL)
{
printf(“Memory allocation error…”); exit(0);
}
new->link=NULL; new->prev=NULL;
printf(“\nEnter node information :”);
scanf(“%d”,&new->info);
if(root= =NULL)
{
root=new;    temp=root;
}
else
{
new->prev=temp;   temp->link=new;
temp=new;
}
printf(“Do you want to continue…(y/n)”);
choice=getche();
}
printf(“\nDoubly linked list is:\n\n”);
/* Doubly linked list normal singly linked list if it is traversed using link field of the node*/
ptr=root;
while(ptr=NULL)
{
printf(“%d”,ptr->info);
ptr=ptr->link;
}
printf(“\n\nDoubly linked list(reverse order):\n\n”);
/*The pointer temp is pointing to the last node. So, we can use it traverse in reverse order*/
while(temp!=NULL)
{
printf(“%d”,temp->info);
temp=temp->prev;
}
getch();
}

Calculate duration between two dates in c# programming

Introduction

Design simple age calculator , calculate remaining days from two dates. Suppose your Date of Birth is 19/mar/1987 and you want to calculate total days on till date. Here we take an simple demonstration of this type of application.

Design

Step-1 : Add two DateTimerPicker and one button control on windows form.
Step-2 : Take Two Datetime class instance, first for starting date and second for ending date.
Step-3 :  Calulate difference between two dates using Timespan structure.
Step-4 :  Using TotalDays property of TimeSpan structure you can calculate days.

Calculate duration between two dates in c# programming

Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace datecalulator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DateTime dtstart = dateTimePicker1.Value;
            DateTime enddate = dateTimePicker2.Value;
            TimeSpan totaldays = enddate - dtstart;
            int calculate =Convert .ToInt32 (totaldays.TotalDays);
            MessageBox.Show(calculate.ToString()+" Days");

        }
    }
}

 Code generate the following output

Calculate duration between two dates in c# programming

Monday, February 24, 2014

Understanding creation of doubly linked list for Data Structrue in 'C'

Understanding creation of doubly linked list:
                                                                                                  
One node with information ‘6’ is added to the doubly linked list. Then the list become,
                                                                                                                                                                                                                                                         



Another node with information ‘16’ is added to the circular linked list. Then the list become,


Similarly when two more nodes are added with information ‘1’ and ’61’, the list become,



Traversing the Doubly Linked List

       Set a pointer variable PTR with ROOT. Process PTR-->INFO and update PTR by LINK of PTR i.e. PTR<--PTR-->LINK. Repeat till the PTR is NULL. The algorithm is as follows:

      TRAVERSDLL (ROOT)
      PTR<--ROOT
      Repeat While PTR< >NULL
      Apply process to PTR-->INFO
      PTR<--PTR-->LINK
      [End of while]
      Exit

Algorithm to print the contents of a linked list:

      TRAVERSEDLL (ROOT)
      PTR<--ROOT
      Repeat While PTR< >NULL
      Write: PTR-->INFO;  PTR<--PTR-->LINK
      [End of while]
      Exit.

        From the just created doubly list in the example shown it is possible to traverse the doubly linked list in reverse order starting from the last node. Address of the last node is available in external pointer TEMP. Set a pointer variable PTR with TEMP. Process PTR-->INFO and update PTR by storing the address of previous node given by the pointer part PREV of PTR i.e. PTR<--PTR-->PREV. Repeat till the PTR is NULL.
The algorithm is as follows:

       TRAVERSEDLLRO
       PTR<--TEMP
       Repeat While PTR< >NULL
       Apply process to PTR-->INFO; PTR<--PTR-->PREV
       [End of while]
       Exit.
  Rests of the operations are similar to singly linked list and are left out as exercises.

How to Create User-Defined Database in SQL Programming

In addition to system databases, the SQL Server also contains user-defined databases where the users store and manages their information. When the users create a database, it is stored as a set of files on the hard disk of the computer.

To create a user-defined database, you can use the CREATE DATABASE statement. The syntax of the CREATE DATABASE statement is:

CREATE DATABASE database_name
[ON [ PRIMARY ] [ < filespec >]]
[LOG ON [ < filespec >}}
< filespec > : : =
( [ NAME = logical_file_name , ]
FILENAME = ‘os_file_name’
[ , SIZE = size]
[ , MAXSIZE = { max_size | UNLIMITED } ]
[ , FILEGROWTH = growth_increment ] ) [ ,…n ]

Where
  • Database_name is the name of the new database.
  • ON specifies the disk files used to store the data portion of the database (data files).
  • PRIMARY specifies the associated <filespec> list that defines file in the primary filegroup.
  • LOG ON specifies the disk files used to store the log files.
  • NAME=logical_file_name specifies the logical name for the file.
  • FILENAME=os_file_name specifies the operating-system file name for the file.
  • SIZE=size specifies the initial size of the file defined in the <filespec> list.
  • MAXSIZE=max_size specifies the maximum size to which the file defined in the <filespec> list can grow.
  • FILEGROWTH=growth_increment specifies the growth increment of the file defined in the <filespec> list. The FILEGROWTH setting for a file cannot exceed the MAXSIZE setting.

To create a database, you must be a member of the dbcreator server role. In addition, you must have the CREATE DATABASE, CREATE ANY DATABASE, or ALTER ANY DATABASE permissions.

The following SQL query creates a database named Personnel to store the data related to all the employees:
CREATE DATABASE Personnel
The preceding statement creates a database named Personnel in the C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data folder. The data file name of the database is Personnel.mdf and the log file name is Personnel_Log.ldf.
You can also create a database in the Object Explorer window by right-clicking the Databases folder and selecting the New Database option from the shortcut menu. When the database is created, the user, who creates the database, automatically becomes the owner of the database. The owner of the database is called dbo.
After a database has been created, you may also need to see the details of the database. For this purpose, you can use he sp_helpdb command. The synbtax of the sp_helpdb command is:
sp_helpdb [database name]

Store information in database
Rename or Drop Database
© Copyright 2013 Computer Programming | All Right Reserved