Tuesday, October 9, 2012

CUDA Diary Day 1

Hi !
Yesterday, installed gcc on fedora 17.
Today, up to now, installed NVIDIA device drivers.
Currently, trying to install CUDA Toolkit + SDK.
Thanks to : http://fedoraproject.org/wiki/Cuda.

Thursday, March 22, 2012

Pascal’s Triangle

In 1653 Blaise Pascal published his treatise Traité du triangle arithmétique describing his triangle computing the binomial coefficients, which are used in the study of probability; Pascal gets credit for the name even though both the coefficients and their arrangement in a triangle were known before him.

To compute the triangle, start with a row containing only 1. Then each succeeding row is built by adding the two numbers triangularly above the current number, assuming a temporary 0 at each end of the prior row.

Your task is to write a program to neatly display Pascal’s Triangle.

1:  i=1  
2: a=[]
3: b=[]
4: for j in xrange(10):
5: for k in xrange(j):
6: a.append(1)
7: if j>=3:
8: for l in xrange(1,j-1):
9: a[l]=b[l-1]+b[l]
10: m=str(a)
11: print m.center(76)
12: b=a
13: a=[]

Monday, April 11, 2011

merge sort

If you have two sorted sequences, they can be merged into a single sorted sequence in time linear to their combined length by running through them in order, at each step taking the smaller of the heads of the two sequences. Then merge sort works by recursively merging smaller sequences into larger ones, starting with trivially-sorted sequences of one element that are merged into two-element sequences, then merging pairs of two-element sequences into four-element sequences, and so on, until the entire sequence is sorted.

Your task is to write a function that sorts an array by the merge sort algorithm described above, according to the conventions of the prior exercise.
My C implementation

#include
#include
void display(int arr[],int n)
{
int i;
printf("\n");
for(i=0;i {
printf("\t%d ",arr[i]);
}
getchar();
}

void sort(int arr[],int low,int mid,int high)
{
int i,j,k,l,b[20];
l=low;
i=low;
j=mid+1;
while((l<=mid)&&(j<=high))
{
if(arr[l]<=arr[j])
{
b[i]=arr[l];
l++;
}
else
{
b[i]=arr[j];
j++;
}
i++;
}

if(l>mid)
{
for(k=j;k<=high;k++)
{
b[i]=arr[k];
i++;
}
}
else
{
for(k=l;k<=mid;k++)
{
b[i]=arr[k];
i++;
}
}

for(k=low;k<=high;k++)
{
arr[k]=b[k];
}
}

void partition(int arr[],int low,int high)
{
int mid;
if(low {
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
sort(arr,low,mid,high);
}
}


void main()
{
int arr[20],i;
int n;
printf("Enter number of data:");
scanf("%d",&n);
printf("enter the data:");
for(i=0;i {
scanf("%d",&arr[i]);
}

partition(arr,0,n-1);
display(arr,n);

}