Tuesday 22 January 2013

Simple Sorting Algorithm Using DMA

This post provides the source code for simple and naive integer sorting algorithm by exploiting the dynamic memory allocation feature of C programming.

#include <stdio.h>
#include <stdlib.h>

int main()
{
 int *arr, i, j, n;
 
 printf("Enter the number of items: ");
 scanf("%d", &n);
 
 arr = malloc(n * sizeof(int));
 
 for (i = 0; i < n; i++)
 {
  printf("Enter the %dth item: ", i + 1);
  scanf("%d", &arr[i]);
 }
 
 for (i = 0; i < n - 1; i++)
 {
  for (j = 0; j < n - 1; j++)
  {
   int temp;
   
   if (arr[j] > arr[j + 1])
   {
    temp = arr[j];
    arr[j] = arr[j + 1];
    arr[j + 1] = temp; 
   }
   
  }
 }
 
 printf("The sorted array is:\n");
 
 for (i = 0; i < n; i++)
 {
  printf("%d\n", arr[i]);
 }
 return 0;
}
samar@samar-Techgaun:~$ gcc -Wall -o sort sort.c 
samar@samar-Techgaun:~$ ./sort 
Enter the number of items: 5
Enter the 1th item: 2
Enter the 2th item: 4
Enter the 3th item: 1
Enter the 4th item: 92
Enter the 5th item: 45
The sorted array is:
1
2
4
45
92




Read more...

Matrix Multiplication Using DMA [C Source Code]

This post provides a source code for matrix multiplication by dynamically allocating memory for matrices to be multiplied and multiplication of those matrices.

#include <stdio.h>
#include <stdlib.h>

int main()
{
 int **A, **B, **C, m, n, p, q, i, j, k;
 printf("Enter the size of matrix A: ");
 scanf("%d %d", &m, &n);
 
 printf("Enter the size of matrix B: ");
 scanf("%d %d", &p, &q);
 
 if (n == p)
 {
  A = malloc(m * sizeof(int));
  B = malloc(p * sizeof(int));
  C = malloc(m * sizeof(int));
  
  for (i = 0; i < m; i++)
  {
   A[i] = malloc(n * sizeof(int));
   C[i] = malloc(q * sizeof(int));
  }
  
  for (i = 0; i < p; i++)
  {
   B[i] = malloc(q * sizeof(int));
  }
   
  printf("Enter the matrix A:\n\n");
  for (i = 0; i < m; i++)
  {
   for (j = 0; j < n; j++)
   {
    scanf("%d", &A[i][j]);
   }
  }
  
  printf("Enter the matrix B:\n\n");
  for (i = 0; i < p; i++)
  {
   for (j = 0; j < q; j++)
   {
    scanf("%d", &B[i][j]);
   }
  }
  
  for (i = 0; i < m; i++)
  {
   for (j = 0; j < q; j++)
   {
    C[i][j] = 0;
    for (k = 0; k < n; k++)
    {
     C[i][j] = C[i][j] + (A[i][k] * B[k][j]);
    }
   }
  }
  
  printf("Multiplication of given matrices is: \n\n");
  
  for (i = 0; i < m; i++)
  {
   for (j = 0; j < q; j++)
   {
    printf("%d ", C[i][j]);
   }
   printf("\n");
  }
 
  for (i = 0; i < m; i++)
  {
   free(A[i]);
   free(C[i]);
  }
  for (i = 0; i < p; i++)
  {
   free(B[i]);
  }
  free(A);
  free(B);
  free(C);
 }
 else
 {
  printf("Matrix multiplication is not possible for given size\n\n");
 }
 return 0;
}
samar@samar-Techgaun:~$ gcc -Wall -o matrix_mul matrix_mul.c
samar@samar-Techgaun:~$ ./matrix_mul 
Enter the size of matrix A: 3 2
Enter the size of matrix B: 2 3
Enter the matrix A:

1 2
3 4
5 6
Enter the matrix B:

1 2 3
4 5 6
Multiplication of given matrices is: 

9 12 15 
19 26 33 
29 40 51 



Read more...

Thursday 17 January 2013

Addition Of Two Matrices Using DMA [C Source Code]

Here is the source code in C that makes use of DMA function malloc() to dynamically allocate the memory for matrices and find their sum.
#include <stdio.h>
#include <stdlib.h>

int main()
{
 int **A, **B, **C, m, n, p, q, i, j;
 printf("Enter the size of matrix A: ");
 scanf("%d %d", &m, &n);
 
 printf("Enter the size of matrix B: ");
 scanf("%d %d", &p, &q);
 
 if (m == p && n == q)
 {
  A = malloc(m * sizeof(int));
  B = malloc(m * sizeof(int));
  C = malloc(m * sizeof(int));
  
  for (i = 0; i < m; i++)
  {
   A[i] = malloc(n * sizeof(int));
   B[i] = malloc(n * sizeof(int));
   C[i] = malloc(n * sizeof(int));
  }
   
  printf("Enter the matrix A:\n\n");
  for (i = 0; i < m; i++)
  {
   for (j = 0; j < n; j++)
   {
    scanf("%d", &A[i][j]);
   }
  }
  
  printf("Enter the matrix B:\n\n");
  for (i = 0; i < m; i++)
  {
   for (j = 0; j < n; j++)
   {
    scanf("%d", &B[i][j]);
   }
  }
  
  for (i = 0; i < m; i++)
  {
   for (j = 0; j < n; j++)
   {
    C[i][j] = A[i][j] + B[i][j];
   }
  }
  
  printf("The addition of two matrices is: \n\n");
  
  for (i = 0; i < n; i++)
  {
   for (j = 0; j < m; j++)
   {
    printf("%d ", C[i][j]);
   }
   printf("\n");
  }
 
  for (i = 0; i < m; i++)
  {
   free(A[i]);
   free(B[i]);
   free(C[i]);
  }
  free(A);
  free(B);
  free(C);
 }
 else
 {
  printf("Matrix addition is not possible for given size\n\n");
 }

 return 0;
}


Below is a sample run along with the compilation step.

samar@samar-Techgaun:~$ gcc -Wall -o matrix_addn matrix_addn.c
samar@samar-Techgaun:~$ ./matrix_addn 
Enter the size of matrix A: 2 2
Enter the size of matrix B: 2 2
Enter the matrix A:

1 2
3 4
Enter the matrix B:

4 3
2 1
The addition of two matrices is: 

5 5 
5 5 



Read more...

Tuesday 15 January 2013

Transpose Of Matrix Using DMA [C Source Code]

This snippet utilizes the dynamic memory allocation function, malloc() and finds the transpose of the user provided matrix.

Below is the source code:

#include <stdio.h>
#include <stdlib.h>

int main()
{
 int **matrix, **transpose, m, n, i, j;
 printf("Enter the size of matrix: ");
 scanf("%d %d", &m, &n);
  
 matrix = malloc(m * sizeof(int));
 transpose = malloc(n * sizeof(int));
 
 for (i = 0; i < m; i++)
 {
  matrix[i] = malloc(n * sizeof(int));
 }
 
 for (i = 0; i < n; i++)
 {
  transpose[i] = malloc(m * sizeof(int));
 }
  
 printf("Enter the matrix:nn");
 for (i = 0; i < m; i++)
 {
  for (j = 0; j < n; j++)
  {
   scanf("%d", &matrix[i][j]);
  }
 }
 
 for (i = 0; i < m; i++)
 {
  for (j = 0; j < n; j++)
  {
   transpose[j][i] = matrix[i][j];
  }
 }
 
 printf("The transpose of given matrix is: nn");
 
 for (i = 0; i < n; i++)
 {
  for (j = 0; j < m; j++)
  {
   printf("%d ", transpose[i][j]);
  }
  printf("n");
 }

 for (i = 0; i < m; i++)
 {
  free(matrix[i]);
 }
for (i = 0; i < n; i++)
 {
  free(transpose[i]);
 }
 free(matrix);
 free(transpose);
 return 0;
}


Below is the sample run:

samar@Techgaun:~$ gcc -Wall -o transpose transpose.c 
samar@Techgaun:~$ ./transpose 
Enter the size of matrix: 2 3
Enter the matrix:

1 2 3
4 5 6
The transpose of given matrix is: 

1 4 
2 5 
3 6



Read more...

Friday 11 January 2013

Java 0-day In The Wild - Disable Java Plugin NOW

Security researchers have discovered yet another critical Java 0-day exploit being used by majority of the browser exploit packs such as Blackhole and Cool. Users are urged to DISABLE the Java plugin RIGHT NOW.

French researcher Kaffeine discovered that the java 0-day has been spotted to be making big hits daily, in a blog post. This particular exploit has proven to be very serious threat for the users. The folks at AlienVault Lab have also reproduced the exploit and it seems to work over all the java versions across all platforms which is a very serious threat.

As for now, the users are highly urged to disable their JAVA plugins right now so that they do not get hit by these 0-days.

So what are you waiting for?

Important links:


How To Disable Java Plugin

Kaffeine's blog post

Alien Vault Labs Post


Read more...

Thursday 10 January 2013

Create Backup Of List Of Apps Installed In Ubuntu

When my laptop's HDD was near to death, I had created backup of the list of all applications and packages I'd installed in my Ubuntu so that I would be able to install them easily in my new system in new HDD. I had forgotten about it but today suddenly remembered and am sharing this simple technique. Fire up the terminal and type the following:

samar@Techgaun:~$ sudo dpkg --get-selections > installed_apps




Now the list of packages will be saved in the installed_apps file and you can use it for future reference. I hope this is useful ;)


Read more...

Thursday 27 December 2012

The Scale Of Universe [A Must Watch]

Well this is so absofuckingly awesome that I decided it is worth making post about this particular site. Without further delay, I present you this awesome link

Scale of Universe


I hope you loved the site like I did :)




Read more...

Monday 24 December 2012

Wishing You A Merry Christmas

Techgaun wishes you all Merry Christmas and Happy Holidays. May this christmas bring happiness and joy in your life. May this Christmas be the special one and may Santa be more gracious to you and your family.

Time for enjoying the christmas to its fullest so guess what we are doing?

Listening to this great song:



And, few movies on the line :)

List of Christmas Movies



The Christmas List


A Bride For Christmas


Christmas Cupid


Christmas Song


Naughty or Nice


All I Want For Christmas


Now that we love the geeky stuffs, lets see some examples of Christmas trees drawn using TeX. FYI, TikZ is build on top of PGF and allows you to create sophisticated graphics in a rather intuitive and easy manner. And, Tikz and PGF are TeX packages used to create graphics programmatically.

Christmas Tree Using Tikz

Wish you and your family a Merry Christmas!!!



And, special mention to Brisha Pote and Movie111Me @ Youtube


Read more...