Thursday, 20 March 2014

Program to print Elements of a Matrix in Spiral Order in c

Hi friends,
               Today I'm here with a new program that is print elements of a matrix in spiral order. We have to start from the element in the 0th row and 0th column in the matrix and proceed in a spiral order as shown below.

                                          Input matrix 4x4

                                            1    2    3    4
                                            5    6    7    8
                                           9   10   11  12
                                           13 14   15  16

and the output of the program is ,
                                           1 → 2  → 3 → 4
                                                                    ↓
                                           5 → 6  → 7      8
                                           ↑               ↓      ↓
                                           9     10 ←11   12
                                           ↑                       ↓
                                          13←14←15←16

Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10

/*
   Print Matrix in Spiral Order
   Created by: Pirate
*/

#include<stdio.h>
#include<conio.h>
int main()
{
int a[100][100],i=0,j,k,r,c,i1=0,j1=1,p,count=0;
printf("Enter the size of matrix\t");
scanf("%d%d",&r,&c);
printf("Enter the %dX%d matrix",r,c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
}
p=c*r-1;
j=0;
while(1)
{
for(i=i1;i<c;i++)
{
printf("%d ",a[j][i]);
count++;
}
for(j=j1;j<r;j++)
{
printf("%d ",a[j][i-1]);
count++;
}
for(i=r-2;i>=i1;i--)
{
printf("%d ",a[j-1][i]);
count++;
}
if(count>p-1)
break;
for(i=j-2;i>i1;i--)
{
printf("%d ",a[i][i1]);
count++;
}
i1++;
j1++;
c--;
r--;
p--;
j=i1;
}
        getch();
        return 0;
}

Output:


Enjoy :)



           

Thursday, 6 March 2014

Different ways to swap values

Hi friends,
                Today I'm going to talk on swapping concept. You all know how to swap but which is the fastest way to swap two variables values. What is swapping? It's mean to exchange values of one variable to another variable. I'm going to describe here three method to swap two variable. In first method we'll use a temporary variable that is used to store intermediate result. In second method we'll use addition and subtraction but no other variable and in the last method we use bitwise XOR operator. Lets take two variable named A,B.

A=10
B=20

after swapping

A=20
B=10

Method:1 (Swapping using temporary variable)
Here we have to take another variable temp. It is the simplest method to swapping values between variables.

temp=A;
A=B;
B=temp;

Method:2 (Swapping with Addition and Subtraction)
In this method we don't use another temporary variable. It can only swap numeric variable values and if the value stored in the variable is little less from its range then it will not work properly and will give incorrect answer. 

A=A+B;
B=A-B;
A=A-B;

Method 3 (Swapping using XOR bitwise operator)
It is the fastest method to swap two variables but it's not working on floating type variables and gives correct output on character and integer type variables.

A=A^B;
B=A^B;
A=A^B;

Here is the full program,

#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,temp;
clrscr();
printf("Enter two no");
scanf("%d %d",&a,&b);
printf("Swapping using temporary variable\n");
temp=a;
a=b;
b=temp;
printf("A=%d\t%d",a,b);
printf("\nSwapping with Addition and Substraction\n");
a=a+b;
b=a-b;
a=a-b;
printf("A=%d\tB=%d",a,b);
printf("\n Swapping using XOR bitwise operator\n");
a=a^b;
b=a^b;
a=a^b;
printf("A=%d\tB=%d",a,b);
getch();
        return 0;
}

Output:


Enjoy :)