-->

Monday, January 27, 2014

Introducation to Pointer DataStructure, C Programming

 Pointer is a special type of variable that is used to store the memory address of another variable of free memory requested by the programmer or user during the execution of  the program. To understand dynamic data structures understanding pointer is definitely must. In simple words pointer is a variable that stores memory address whereas simple variables store data.

              For example, when variable is declared the memory is allocated to that variable by the compiler during the compilation of the program. Once memory is allocated the data can be stored in that memory location. The data is referred by the name of the variable. If the address of that variable is to be stored then the pointer variable is necessary. Consider the declaration statement of “C” language: int age;

            Assuming that during compilation memory address allocated variable age is 1000, and the address 1000 is to be stored in another variable that variable should be of the type pointer.
           In case of ‘C’ language the pointer variables are simply declared by means of prefixing an asterisk (*) to the variable name in a normal declaration statement. For example, int *page; declares a pointer variable page of the type int. Now page can be used to store the address of a simple integer variable. i.e. page = &age.
            When a pointer variable is declared, the memory is not allocated to such variable during the compilation. So it can point to any already allocated address of variable or an address of requested free memory. The type of free memory or variable should match with the type of pointer variable. Pointer variables are advantageous when they are used for dynamic memory allocation and function call by reference.

Introduction of Array in DataStructure, C programming

ARRAY is an ordered collection of similar data elements, so it is called as Linear Data Structure. The elements are arranged in Linear order i.e. one after the other, at consecutive memory locations. Each element of the array is naturally bound together by means of consecutive memory allocation to each respective element.
  ARRAY may be either one-dimensional or two –dimensional or multidimensional depending on the characteristics that are explained by the elements. In one-dimensional the elements simply explain

One Dimensional Arrays
       Array is a Linear Data Structure in which homogeneous (similar)
element are stored. Array is used to represent a real life List. The elements stored explain one and only one characteristic. For example you may find an array of makes of students, height of students, salary of employees, employee structures etc.

Sequential Allocation   
    One-dimensional array is stored consecutively or sequentially in memory. Contiguous memory is allocated for an array to store the elements. The address of an element of an array is the location in memory where that element is stored. The address of first element is the starting address of the array and it is called ‘Base Address’. Total memory needed for storage of an array depends on the number of element (size of array) in it and the size of each element. The size of element is termed as ‘Word Size’.

WS – size of element stored in bytes (basic memory unit)                                               
a1, a2,… a10 are the address of each element                                                          
a1 First element’s address – The Base Address                                                                         a2=a1+word size.                                                                                                                  
a3=a2+word size OR a1+(word size) * 2                                                                                   
a4=a1+(word size) * 3                                                                                           
…                                                                                                                                   
an=a1+(word size) * (n-1)                                                                                                
IF a1=1000, so Base Address=1000 and WS=4 bytes, then                                        
a2=BA+WS *(2-1)                                            a5=BA+WS *(5-1)                                 
=1000+4 * 1                                                        =1000+4 * 4                                       
=1004                                                                  =1016
In case of “C” language one-dimensional array is declared in the declaration part of the program part of the program as follows:
    data _type name _of _the _array[size];
where data _type may be any of the standard data types available in C like int, float, long, char, double etc. 
name _ of _ the _ array is an identifier or user defined variable name to represent  array name. [size] is an integer constant that specifies the size of the array and it represents number element that can be stored in one-dimensional array.
        For example, int marks [5] is an array of size 5 and of the type integer.
In “C” language the lower bound of the array is fixed to 0(Zero) and the upper bound of is size-1. User only has to take care of the array boundary. So in the above example, makes [0] represents the first element whereas marks [4] represents the last element of the array.

Two Dimensional Arrays
       Two-dimensional array are called as Tables in case of business and Matrix in case of Mathematics. The elements stored in a two-dimensional array explain two characteristics. Row and Columns are used to explain the two dimensions of two-dimensional array. Two-dimensional is a collection of multiple columns. Row index and column indexes are used to access an individual element stored in two-dimensional array. 


Row size of the above two-dimensional array is 3 and column size is also 3. So it is called a 3*3 matrix or two-dimensional array.  Two-dimensional array is represented as
         NameOfTheArray (RLB: RUB, CLB:CUB) OR
         NameOfTheArray (M, N)
         NameOfTheArray(M X N)
Where RLB- Lower bound of Row, RUB-Upper bound of Row .
CLB-Lower bound of column, CUB-Upper bound of Column.
M- Row size, N- Column size. (In this case 1 is lower bound, for both row and column).
     When row size M and column size N are not given they are calculated as M=RUB-RLB+1 and N=CUB-CLB+1.
M*N elements are stored in two-dimensional array(TDA).

Memory Representation or Sequential allocation of TDA
  
                   As there are two dimensions of two-dimensional array, it is stored in memory in sequential M*N cells where M and N are row and column size of TDA respectively. The sequential storage may be by ROW major or COLUMN major depending on the compiler.
                  In ROW major all the ROW element are stored sequentially, means first row elements are stored first then second rowed elements and so on. In COLUMN major all the COLUMN elements are stored sequentially, means first the column elements are stored first then second column elements and so on.
                                                   For example consider a matrix MAT (3*5), the element of which are as shown below:  
                              4   5   6   8   9
                              7   9   1   5   3
                              6   5   4   3   2
In case of Row major the stored can be represented as follows:              


11 mean first row’s first column element, similarly 12 and 13 represent second and third column elements first row respectively, so on the elements can be represented.     
        So, in case of row major all the 5.elements of first row are stored first, then second row’s 5 elements and then third row’s 5 elements are stored sequentially in memory. So depending on the word size M*N storage locations are required to store the TDA elements. Here in this example 3*5=15, fifteen storage (memory) locations are needed to store the TDA. As the word size is 2 (assuming integer) bytes, the total memory required is 30 bytes. 
In case of column major the storage can be represented as follows:  


11 means first row’s first column element, similarly 12 , 13 and so on.
     So, in case of column major all the 3 elements of first column are stored first, then second column’s 3 elements, then third column’s 3elements, then fourth column’s 3 elements and fifth column’s 3 elements are stored sequentially in memory. So, depending on the word size M*N storage locations are required to store the TDA elements.
Here in this example 3*5=15, fifteen storage (memory) locations are needed to store the TDA. As the word size is 2 (assuming integer) bytes, the total memory required is 30 bytes (similar to row major).

Address Calculation
              In case of two-dimensional array the address of the individual element is calculated depending on the storage major either row major or column major.  The first element of the TDA is stored at location called Base Address and the location of the next coming elements depends on the word size, similar to one-dimensional arrays.

Row Major      
                          In case of Row major as all the row elements are stored sequentially, by knowing the Base Address and Word Size, it is possible to calculate the location of any element. To reach to the element i.e. to find the location of the element, number of rows elements are skipped to reach to the element in the respective row and number of column elements are skipped to reach to the column element  of that row.
                          For example in a TDA, MAT (5*6), in order to reach the fifth element of fourth row, three row are skipped and each row contains 6 elements plus four elements of fourth are also skipped. So total elements to skip is equal to 3*6+4=22, If word size is 4 then 22*4=88 plus Base Address is the address or location of fifth element of fourth row i.e. MAT(4,5).
      For example consider a TDA, MAT (5 * 6), the element of which are as shown below:
      The elements to be skipped are shown in bold face in order to reach to element 2.
Total number of elements skipped are 3*6+4=22.
If they are stored in memory starting from location 100, then the memory representation of the above TDA is as follows (assuming the word size 2 because of integer data):


So, the formula to find the address is as follows if TDA is given as MAT(M X N) (in which both RLB and CLB are 1):
Where
BA - Base Address,          WS – Word Size
I – Row Number,              J – column Number
N – Column Size

If TDA is given as MAT(RLB : RUB, CLB : CUB), then the formula changes to

Where
                BA – Base Address,        WS – Word Size,   I – Row Number,
                J – Column Number,      CUB – Column Upper Bound, CLB – Column Lower Bound
       In case of “C” language the two-dimensional array is declared by adding another size to one-dimensional array.
The syntax of the declaration of two-dimensional array looks like:  data-type name-of_the_array[rsize][csize];
Where data_type may be any of the standard data types available in C like int, float, char, double etc. name_of_the_array is an identifier or user defined variable name to represent array name. [rsize] is an integer constant that specifies the row size of the array and it represents number rows that can be stored I two-dimensional array and [csize] is also integer constant that shows number elements for each row. For example, int matrix[5][5] is two-dimensional array of size 5*5 and of the type integer. Here matrix whereas matrix [4][4] represents last row’s last column element.

Sunday, January 26, 2014

Example of Inline coding model in ASP.NET

Code render blocks define the inline code or inline expressions that are executed when a web page is rendered. The inline code is then executed by the server and the output is displayed on the client browser that requested for the page. The code render blocks are capable of displaying information on the client browser without customarily calling the write method. The code render blocks are represented on a page by the <% %> symbols. The code present inside these symbols is executed in the top-down manner. Now, let's put the concept of code render bock into use by creating an application, CodeRenderBlock. This application use a code render block to implement a loop used for changing the text size.

Lets take a simple example, change font size in asp.net using loop

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Change Font size inline code model</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <% for(int i=0;i<6;i++)
           
       { %>
       <font size="<%=i %>" >Hello World ! </font><br />
       <% } %>

    </div>
    </form>
</body>
</html>

Code generate following output

Computer Programming : code render block

About the Folders in MVC Web Application in Visual Studio: Part 4

In MVC application, Views folder represents the pages shown to the user in the unique folder, named according to controller. Means there is one folder for each controller as shown in the following image of solution explorer.

As in image, account folder have some razor pages like login.cshtml, register .cshtml, manage.cshtml and more. Every page have its own action specified in the respective controller discussed in the earlier article.

Views folder MVC Web Application in Visual Studio 2013


These page can be of any type e.g. plain html, classic asp, razor etc. Home view folder contains Index, about and contact pages. Run the application and there are some links like Home, about, contact us. When user click on any of them, the respective action is called and the related view is shown to user.

Further article will show, how to create such type of folders for Model, controller as well as views.

About the Folders in MVC Web Application in Visual Studio: Part 3

Controller folder represents the controller for the MVC application created, which have two controller by default i.e. AccountController and HomeController. The functions/methods defined in these classes are called Action results used to specify some rules for the view related to that action.

Every action have its related view responsible for handling user input. Every controller in MVC application have a suffix Controller as the name shown in the default controller folder. Programmer can also create its own controller by adding new from the right click on this folder.

The following action shows two type of login actions, first is for Get action and second is for Post action. When user request for login, get action requests, after submitting with required credentials it requests for post (HttpPost) action (the second one).

        [AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
            {
                return RedirectToLocal(returnUrl);
            }

            // If we got this far, something failed, redisplay form
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
            return View(model);
        }

There should be a view named Login to make these actions effective. Like this action the AccountController also have Register action, logout action etc.

Example of CausesValidation property in ASP.NET

CausesValidation: Whether Validation is performed or not , you can check using CausesValidation property,after button click. Bydefault page validation is performed with all control, which is associated with validation control. If CausesValidation is true, page validate all control , which is associated with validation control. If CausesValidation is false , manually validate the control by the user.

Lets Take an Simple Example

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Enter Your Name : 
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:RequiredFieldValidator
            ID="RequiredFieldValidator1" EnableClientScript ="false" ControlToValidate ="TextBox1" runat="server" ErrorMessage="Validate your name"></asp:RequiredFieldValidator><br />
        <asp:Button ID="Button1" runat="server" Text="Button" CausesValidation="False" 
            onclick="Button1_Click" />


    </div>
    <asp:Label ID="Label1" runat="server"></asp:Label>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        RequiredFieldValidator1.Validate();
        if (RequiredFieldValidator1 .IsValid)
        {
            Label1.Text = "Entered Name is " + TextBox1.Text;
 
        }
    }
}

Code Generate the following output

Example of CausesValidation property in ASP.NET

Example of CausesValidation property in ASP.NET

Label control and their properties in ASP.NET, AssociatedControlID example

Introduction

The label control is used to display the text that the user control edit. The Label control exists within the System.Web.UI.WebControls namespace. Here is the class hierarchy of the Label class:
System.Object
    System.Web.UI.Control
       System.Web.UI. WebControls.WebControl
           System.Web.UI.WebControls.Label

Public Properties of the Label Class

AssociatedControlID : Obtains or sets the identifier for a server control the Label control is associated with.
Text : Obtains or sets the text content of the Label control.

Lets take an simple example

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected void Button1_Click(object sender, EventArgs e)
    {
        Label2.Text = "Associated Control Id Example";
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
     <div>
        <asp:Label ID="Label1" runat="server" Text="<u>U</u>serName" AccessKey="U" 
             AssociatedControlID="TextBox1"></asp:Label>
    &nbsp;<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Check Data" 
             onclick="Button1_Click" />
        <br />
        <asp:Label ID="Label2" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>

Code Generating the following Output

Label control and their properties in ASP.NET, AssociatedControlID example

© Copyright 2013 Computer Programming | All Right Reserved