-->

Friday, January 24, 2014

Computer Programming : Classification or Type of Data structures, C Programming

Data structures can be classified, keeping in view the way the elements are connected to each other, the way when they are stored in memory or the way further division of a data structure.

Linear and Non-Linear

This type of classification is on the basis of element's connection and placement. When the elements of a data structures are placed in order, means if the second element is placed after the first one, third one after the second one and so on, are called Linear Data Structures otherwise Non-Linear Data Structures. Examples of Non-linear Data Structures: Trees, Graphs etc.

Classification of Data Structure in Computer programming

ARRAY is an ordered collection of similar data elements. So, it is called as Linear Data Structure. The element are arranged in Linear order i.e. one after the other at consecutive memory locations.Similarly LINKED LIST is also a Linear Data Structure Where the elements of List are stored in Linear Order but at different memory locations, the Linear order is maintained by one of the fields of Linked List, called Link field(pointer to next node element).

On the other hand, TREE is a non-linear data structure in which the elements are stored in hierarchical order instead of linear order. In hierarchical order top most node is called root node and it may be connected to zero, one or more than one nodes. So from one element elements of the data structure are placed such that they are adjacent (connected) to more than one element, then the resulting data structure, Which is non-linear one, is called as GRAPH. Graph is a general non-linear data structure and Tree is a restricted Graph.

STACK and QUEUE are specialized data structures where insertion and deletion are restricted. STACK and QUEUE can be implemented either using one-dimensional array or linked list.

Linear data structure are also called as contiguous data structures and non-linear data structures are called as non-contiguous data structure.

Static and Dynamic

This type of classification is on the basis of Memory Allocation. The data structure modeled must be present in memory to use it i.e. memory should be allocated to the data structure to store the data in it. If the memory is allocated to the data structures at the time of compilation of programs where the data structures are used, then such type of data structures are called as static data structures. Compilation of program is translation of source program into an object program. The linear data structure ARRAY is static data structure for which memory is allocated at the time of  compilation.

On the other hand if the memory is allocated at the time of execution of program (While running) for the data structures used in the program are called Dynamic data structures. The linear data structure LINKED LIST is a dynamic data structure for which memory is allocated at the time of execution, means the memory for data structure is allocated as per the user wish at the time of executing the program.

Primitive and Non-Primitive

This type of classification is on the basis of further division. If the data structure is not further divisible to another data structure, then it is called as Primitive data structure and if it is further divisible then it is called as Non-Primitive data structure.

For example the standard data types of C like char, int, float are fall under the category of Primitive data structures where as user defined or derived data types like array, structure are further divisible so they are called Non-Primitive data structure. 

Monday, December 16, 2013

Computer Programming : Nullable types, Null coalescing operator in c# Programming

In Computer Programming, C# Data types are divided into two broad categories such as Value types and Reference type. In Value types, some inbuilt types such as int, float, double, structs, enum etc. These all are non-nullable types, it means these types don't hold null value. In other hand Reference types such as string, Interface, Class, delegates, arrays etc. can hold null value.
By default value types are non nullable. To make them nullable use, programmer have to use ?
  • int a=0   a is non nullable, so a cannot be set to null, a=null will generate compiler error
  • int ?b=0  b is nullable int, so b=null is legal

Operator Need

If programmer want to assign null value to non-nullable data types then there are some operators explained with an example. 

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)
        {
            int a;
            int? b = 100;
            a = b ?? 0;
            System.Console.WriteLine(a);
            Console.ReadKey();
        }
    }
}
Computer Programming : Nullable types, Null coalescing operator in c# Programming

Output show that if b hold some value like 100 then Null coalescing operator assign value to variable (a). If b hold null value then Null coalescing operator assign default value, which is zero to variable a. 

Replace the declaration of variable b with the following line:
            int? b = Null;
This will outputs zero (default value).

Friday, December 13, 2013

Computer Programming: Operators in c# console application

Introduction

It is a symbol, which is performed operation on operands, operation can be mathematical or logical. In c#, operators are categories in three basic parts first one is unary operator and second one is binary operator and last one is ternary operator. In unary operator we use single operand like.
++a // here a is a "operand"
In Binary operator we use two operand. Like
a+b;
In ternary operator we use three operand like
a>b?a:b

Assignment Operator (=)

if we create a variable of type integer also we want to initialize it with some value then we should use assignment operator like.

int a=10;

Here, variable a is initialized with value 10. It means you can say the right hand side value is copy to left hand side variable using assignment operator.

Arithmetic Operators ( +,-,*,/,%)

Basically arithmetic operators are used for doing mathematical calculation like addition of two or more numbers , multiplication, division, subtraction, and remainder. lets take a simple example to understand arithmetic operators. If the total collection by five students is five thousand then find the average value?.
int collected_money=5000;
int total_student =5;
int avg=collected_money/total_student;


Ternary operator (?, : )

In ternary operator we have three operands, in which first operand is used for check the condition and other two operands is used for giving results. Let’s take a simple example to understand the ternary operator.

Int firstnumber=10;
Bool result;
Result = firstnumber>10? True: false;

In the preceding example, if condition becomes true or you can say if firstnumber is greater than to 10 then ternary operator gives  ‘true’ in results otherwise gives ‘false’.
 

Comparison Operators (< , > ,<=,>=,!=,==)

using comparison operator you can perform Boolean operation between operands known as comparison operator. Lets take an simple to understand comparison operator
 
 class Program
    {
        static void Main(string[] args)
        {
            int a = 10;
            if (a==10)
            {
                Console.WriteLine(string.Format("The value of variable ={0}", a));
  
            }
            Console .ReadKey (false);
        }
    }
 
Computer Programming : Operators in c# console application Here, condition if check equality operation between operands, a and constant value 10. If condition becomes true, output comes "The value of variable=10".


 

  

  Logical operator (&&,|| )

Perform logical operation between Boolean operands known as logical operator. Lets take a simple example
 
  class Program
    {
        static void Main(string[] args)
        {
            bool a = true;
            bool b = true;
            if (a&&b)
            {
                Console.WriteLine("condition becomes true");
  
            }
            Console .ReadKey (false);
        }
    }
 

Sunday, December 8, 2013

Computer programming : Print string in double quote escape sequences, Verbatim literal

In c#, String is enclosed in double quote, suppose we want to store name then we should use string type. For example
String name="Computer Programming";

Starting double quote define the beginning of the string and ending double quote define the end of the string. Now if we want to print this string then in console application, we have to write:

System.Console.writeline (name);

If we want to print words Computer Programming in double quote or you can say output window display Computer Programming in double quote like "Computer Programming"

Note: If we use double quote beginning of the string like " "Computer Programming" then you get error in the string.

Solution: Use Escape sequence character (\) for keep double quote in the string.
For example :  String name = "\"Computer Programming\" ";
There are different types of escape sequences available in msdn library.

Lets take another example, if we want to print path of the file like C:\csharp\tutorial\beginning, then we  should use escape sequence character before every back slash.
Solution : C:\\csharp\\tutorial\\beginning.
Now this type of solution is too typical and unreadable string. So use verbatim literal for this
Solution : @” C:\csharp\tutorial\beginning”

Here @ symbol is a verbatim literal for removing escape sequences characters in the string.

Computer programming : Print string in double quote escape sequences, Verbatim literal

Here's the full program in c# language
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string name = "\"Computer Programming\"";
            Console.WriteLine(name);
            String path = @"C:\csharp\tutorial\beginning";
            Console.WriteLine(path);
            Console.ReadKey(false);
        }
    }
}

Write above lines of code in c# console application and run the project, string with quote and path with quote will display on the screen.



© Copyright 2013 Computer Programming | All Right Reserved