Welcome!

I am Md Shadab Ansari a Student of Computer Science a Computer Programmer a Blogger a Coder

View Work Contact!

About Me

Programming in C
Data Structure
Who am i

Md Shadab Ansari.

Student of Computer Application

The main objective behind the creation of this blog is to share my little knowledge of Computer Science. I have completed my graduation in Bachelor of Science with honors in Computer Science and currently I'm doing Master of Computer Application.

I am very much into programming and like to learn new things. Through this blog, I will try to share my academics courses which includes Programming, Data Structures, Operating System, Networking, Mathematics, many more which might help everyone. .

What I do

Coding

I just love Programing and writing algorithmic codes. I know C/C++, Java and SQL which were taught to me in my high school and college.

Web Design

I am very creative and love creative things, I like beautiful webpages, and it inspires me to make my own blog creative and wonderful.

Mathematics

I love analysing, calculating and solving mathematics problems.

Photography

It's one of my favorite hobby, shooting beautiful landscapes and architechures through my camera lens is just wonderful.

Our Blog

Rotate an Array 'k' times


Rotation of an array is either left shifting or right shifting the elements of an array. Rotation in an array is performed by overwriting one element with another. To perform rotation 'k' times, we actually shift the elements of an array 'k' times in a specified direction. 

A left rotation of an array shift the array elements to left by one position whereas a right rotation shift the array elements to the right by one postilion.

Suppose, an array arr[]={1,2,3,4,5}, A left rotation on arr will result in arr[]={2,3,4,5,1}, whereas a right rotation will result in arr[]={5,1,2,3,4}

'k' rotations on an array will shift the elements 'k' times. Suppose k=3 left rotations are performed on arr[]={1,2,3,4,5}then the rotations will be performed as: 
arr[]={1,2,3,4,5} -> {2,3,4,5,1} -> {3,4,5,1,2} -> {4,5,1,2,3}

Similarly, k=3 right rotations on the arr[]={1,2,3,4,5} will result in

arr[]={1,2,3,4,5} -> {5,1,2,3,4} -> {4,5,1,2,3} -> {3,4,5,1,2}

Source Code


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdio.h>
#define SIZE 10000 //Macro to define maximum Array size

void leftrotation(int arr[], int n, int k) //Function to perform Left rotation
{
 int i, j;
 int temp[k]; //Temporary array
 for (i = 0; i < k; i++)
 {
  temp[i] = arr[i];
 }
 for (i = 0; i < n - k; i++)
 {
  arr[i] = arr[i + k];
 }
 for (i = n - k; i < n; i++)
 {
  arr[i] = temp[i - (n - k)];
 }
}

void rightrotation(int arr[], int n, int k) //Function to perfrom Right rotation
{
 int i, j;
 int temp[k];
 for (i = n - k; i < n; i++)
 {
  temp[i - (n - k)] = arr[i];
 }
 for (i = n - 1; i >= k; i--)
 {
  arr[i] = arr[i - k];
 }
 for (i = 0; i < k; i++)
 {
  arr[i] = temp[i];
 }
}

void display(int arr[], int n) //Display Array Elements
{
 int i;
 for (i = 0; i < n; i++)
  printf("%d ", arr[i]);
 printf("\n");
}

int main(int argc, char const *argv[])
{
 int arr[SIZE];
 int i, n, k;
 char ch;
 printf("Enter SIZE of array : ");
 scanf("%d", &n);
 printf("Enter Array elements\n");
 for (i = 0; i < n; i++)
 {
  scanf("%d", &arr[i]);
 }
 printf("Enter Rotation Value 'k': \n");
 scanf("%d", &k);
 printf("Enter Direction of Rotation (Left/Right : l/r) : ");
 scanf("%c", &ch);
 scanf("%c", &ch);
 switch (ch)
 {
 case 'l':
 case 'L':
  printf("Before Rotation : ");
  display(arr, n);
  leftrotation(arr, n, k);
  printf("After '%d' Left Rotation : ", k);
  display(arr, n);
  break;
 case 'r':
 case 'R':
  printf("Before Rotation : ");
  display(arr, n);
  rightrotation(arr, n, k);
  printf("After '%d' Right Rotation : ", k);
  display(arr, n);
  break;
 default:
  printf("Wrong Choice\n");
 }
}

Output
Enter SIZE of array : 5
Enter Array elements
1 2 3 4 5
Enter Rotation Value 'k': 
3
Enter Direction of Rotation (Left/Right : l/r) : r
Before Rotation : 1 2 3 4 5 
After '3' Right Rotation : 3 4 5 1 2 

Enter SIZE of array : 5
Enter Array elements
10 20 30 40 50
Enter Rotation Value 'k': 
2
Enter Direction of Rotation (Left/Right : l/r) : l
Before Rotation : 10 20 30 40 50 
After '2' Left Rotation : 30 40 50 10 20 

Complexity : O(n)

Eigen Value and Eigen Vector Using Power Method


Given a matrix A such that the relation AX=ƛX holds, then X is called the Eigen-vector or characteristic vector and ƛ is called the Eigen-value or characteristic value of the matrix A.
The matrix equation can be rewritten as: (A-ƛI)X=0 , where I is the identity matrix of same order of matrix A.
There are some methods to find the Eigen value Æ› as well as Eigen vector of a matrix A such as the Iterative method, Power method, Jacobi method, many more. Here, we will limit ourselves to the Power Method of finding the Eigen Value and the Eigen Vector. 


Power Method Algorithm

1. Read size of square matrix A, n
2. Read the matrix
3. Read the error limit ϵ
4. Initialize initial Eigen Vector X[n] (say with 1) and max=X[0]
5. Do
                i. Tmax=max
               ii. Calculate: Xi[n]=A[n][n]*Xi-1[n]
               iii. Extract the maximum ‘max’ from Xi[n]
               iv. Divide all element of Xi[n] with max
6. While(fabs(Tmax-max)>ϵ) 
7. Eigen Value Æ›=max
8. Eigen Vector = X[n]
9. Print Æ› and X[n]
10.   Stop

Source Code


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <math.h>

#define SIZE 3

float max(float x[])
{
    float m = x[0];
    int i;
    for (i = 0; i < SIZE; i++)
    {
        if (x[i] > m)
            m = x[i];
    }
    return m;
}

void multiply(float a[SIZE][SIZE], float x[SIZE])
{
    int i, j, k;
    float sum;
    float temp[SIZE];
    for (i = 0; i < SIZE; i++)
    {
        sum = 0;
        for (j = 0; j < SIZE; j++)
        {
            sum += a[i][j] * x[j];
        }
        temp[i] = sum;
    }
    for (i = 0; i < SIZE; i++)
        x[i] = temp[i];
}

void print(float X[SIZE])
{
    int i;
    printf("Eigen Vector : \n");
    for (i = 0; i < SIZE; i++)
    {
        printf("%f  ", X[i]);
    }
    printf("\n");
}
int main()
{
    float arr[SIZE][SIZE], X[SIZE];
    int i, j, k = 0;
    float tmax, m, e;
    printf("Enter Element into %d X %d Matrix\n", SIZE, SIZE);
    for (i = 0; i < SIZE; i++)
    {
        for (j = 0; j < SIZE; j++)
        {
            printf("Enter Matrix[%d][%d] Element : ", i, j);
            scanf("%f", &arr[i][j]);
        }
    }
    printf("The Matrix Entered : \n");
    for (i = 0; i < SIZE; i++)
    {
        for (j = 0; j < SIZE; j++)
        {
            printf("%f\t", arr[i][j]);
        }
        printf("\n");
    }
    for (i = 0; i < SIZE; i++)
    {
        X[i] = 1;
    }
    printf("Enter Error Limit (Decimal Places) : ");
    scanf("%f", &e);
    e = pow(10, -e);
    m = 0;
    do
    {
        tmax = m;
        multiply(arr, X);
        m = max(X);
        for (i = 0; i < SIZE; i++)
        {
            X[i] /= m;
        }
        k++;
    } while (fabs(tmax - m) > e);
    printf("Eigen Value : %f\nNo. of Iterations : %d\n", m, k);
    print(X);
}

Output

Enter Element into 3 X 3 Matrix
Enter Matrix[0][0] Element : 2
Enter Matrix[0][1] Element : 4
Enter Matrix[0][2] Element : 6
Enter Matrix[1][0] Element : 3
Enter Matrix[1][1] Element : 9
Enter Matrix[1][2] Element : 15
Enter Matrix[2][0] Element : 4
Enter Matrix[2][1] Element : 16
Enter Matrix[2][2] Element : 36
The Matrix Entered : 
2.000000 4.000000 6.000000
3.000000 9.000000 15.000000
4.000000 16.000000 36.000000
Enter Error Limit (Decimal Places) : 5
Eigen Value : 43.879990
No. of Iterations : 7
Eigen Vector : 
0.185868  0.446032  1.000000 

Cycle/Loop Detection in Linked List





A cycle/loop in a single linked list can be detected easily if there's a node which points to some other previous node in the list or to itself. The program below detects the cycle in the single linked list with 'head' as root node pointer passed to the function cycledetection(), and will return the Boolean true if a cycle is detected or else false.


Contact Us

Phone :

000 000 0000

Address :

Street Name
State,Country

Email :

email_support@youradress.com