Saturday, January 31, 2015

Pattern in c

A tricky pattern in cprogram

given
 n=4
values:
1
2
3
4
k=8

output :
11112222
11112222
11112222
11112222
33334444
33334444
33334444
33334444


given

n=9

values:
1
2
3
4
5
6'
7
8
9

k=9

output

111222333
111222333
111222333
444555666
444555666
444555666
777888999
777888999
777888999



given
n=16
values


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

k=1

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



program:




#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,i,m,jj,times,j,k,l,a[100];
clrscr();
printf("Enter value of n:");
scanf("%d",&n);
printf("enter the array elements:"):
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter the value of k:");
scanf("%d",&k);
printf("\n\n\n\nThe pattern is\n");
times=sqrt((k*k)/n);
jj=times;
if(times!=1)
{
for(i=0;i<n;i+=(k/times))
{
for(m=0;m<times;m++)
{
for(j=0,jj=times,l=0;j<k;j++)
{
printf("%d ",a[i+l]);
if(j==(jj-1))
{
l++;
jj+=times;
}
}
printf("\n");
}
}
}

else
{
l=k;
for(i=0;i<n;i++)
{
printf("%d   ",a[i]);
if(i==(k-1))
{
printf("\n");
k+=l;
}
}
}
getch();

}

Saturday, January 3, 2015

Quick Sort in c programming........

Quick Sort :

#include<stdio.h>
#include<conio.h>
int no;
void quicksort(int low,int high,int a[])
{
int i,down=low,up=high,pivot;
pivot=a[down];
down++;
if(low<high)
{
while(down<=up)
{
while(pivot>a[down])
   down++;
while(pivot<a[up])
   up--;
if(down<up)
{
   a[down]=a[down]+a[up];
   a[up]=a[down]-a[up];
   a[down]=a[down]-a[up];
}
}
a[low]=a[up];
a[up]=pivot;
printf("\nArray Status:");
for(i=0;i<no;printf("-->%d ",*(a+i)),i++);
quicksort(low,up-1,a);
quicksort(up+1,high,a);
}
else
return;
}
void main()
{
int *a;
int n,i;
printf("Enter the size of the array:");
scanf("%d",&n);
no=n;
a=(int *)malloc(sizeof(int)*n);
for(i=0;i<n;scanf("%d",(a+i)),i++);
quicksort(0,n-1,a);
printf("\nSorted List:");
for(i=0;i<n;printf(" %d",*(a+i)),i++);
getch();
}



Copy and paste it in a notepad...then save it in "tc\bin" as "name.c" and open the program in turbo c compiler "by file->open->name.c"