Tuesday, October 4, 2011

Data Structure Lab Programs - Write a program to sort an array using the quick sort

Practical # 5

 

 

/*Write a program to sort an array using the quick  sort */

 

#include<stdio.h>

#include<conio.h>

void quick_sort(int a[],int first,int last);

void main()

{

int a[10],n,i;

clrscr();

printf("Enter total no's of element=");

scanf("%d",&n);

if(n>10)

{

printf("OVERFLOW");

}

else

{

printf("The unsorted elements are=");

for(i=0;i<n;i++)

{

scanf("%d",&a[i]);

}

}

quick_sort(a,0,n-1);

printf("The sorted elements are=");

for(i=0;i<n;i++)

{

printf("%d",a[i]);

}

getch();

}

void quick_sort(int a[],int first,int last)

{

int i,j,k,t,temp;

if(first<last)

{

i=first;

j=last+1;

k=first;

 

do

{

do

{

i++;

}

while(a[i]<a[k]);

do

{

j--;

}

while(a[j]>a[k]);

 

if(i<j)

{

t=a[i];

a[i]=a[j];

a[j]=t;

}

}while(i<j);

 

temp=a[k];

a[k]=a[j];

a[j]=temp;

 

quick_sort(a,first,j-1);

quick_sort(a,j+1,last);

}

}

 

 

 

Output:-

 

Enter total no's of element=5

The unsorted elements are=5

4

3

2

1

The sorted elements are=12345

 

Data Structure Lab Programs - Write a program following operation on tables using functions only a)Addition b)Subtration c)Multiplication d)Transpose

Practical # 4

 

/* Write a program following operation on tables using functions only  a)Addition b)Subtration c)Multiplication d)Transpose  */

 

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#include<ctype.h>

void main()

{

int mat1[3][3],mat2[3][3],m,n,i,j,ch;

int mat_add(int mat1[3][3],int mat2[3][3]);

int mat_sub(int mat1[3][3],int mat2[3][3]);

int mat_multi(int mat1[3][3],int mat2[3][3]);

int mat_trans(int mat1[3][3],int mat2[3][3]);

char ch1;

clrscr();

printf("\n Enter the elements of the matrix1:\n");

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

scanf("%d",&mat1[i][j]);

}

}

printf("\n Enter the elements of the matrix2:\n");

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

scanf("%d",&mat2[i][j]);

}

}

printf("\nThe entered matrix1 is \n");

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

printf("%d  ",mat1[i][j]);

}

printf("\n");

}

printf("\nThe entered matrix2 is \n");

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

printf("%d  ",mat2[i][j]);

}

printf("\n");

}

getch();

do

{

clrscr();

printf("\n\n\tMENU");

printf("\n1. Addition");

printf("\n2. Subtraction");

printf("\n3. multiplication");

printf("\n4. Transpose");

printf("\n0. Exit\n");

printf("Enter your choice");

fflush(stdin);

scanf("%d",&ch);

switch(ch)

{

case 1:

mat_add(mat1,mat2);

break;

case 2:

mat_sub(mat1,mat2);

break;

case 3:

mat_multi(mat1,mat2);

break;

case 4:

mat_trans(mat1,mat2);

break;

default:

exit(1);

}

printf("Do you want to continue(y/n)?");

fflush(stdin);

scanf("%c",&ch1);

}while(toupper(ch1)=='Y');

}

int mat_add(int mat1[3][3],int mat2[3][3])

{

int sum_mat[3][3],i,j;

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

sum_mat[i][j]=mat1[i][j]+mat2[i][j];

}

}

printf("\n\n\n Addition of two matrix= \n");

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

printf("%d  ",sum_mat[i][j]);

}

printf("\n");

}

return 0;

}

int mat_sub(int mat1[3][3],int mat2[3][3])

{

int sub_mat[3][3],i,j;

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

sub_mat[i][j]=mat1[i][j]-mat2[i][j];

}

}

printf("\n\n\n Subtraction of two matrix= \n");

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

printf("%d  ",sub_mat[i][j]);

}

printf("\n");

}

return 0;

}

int mat_multi(int mat1[3][3],int mat2[3][3])

{

int prod_mat[3][3],i,j,k;

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

prod_mat[i][j]=0;

}

}

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

for(k=0;k<3;k++)

{

prod_mat[i][j]=prod_mat[i][j]+(mat1[j][k]*mat2[k][j]);

}

}}

printf("\n\n multiplicaton of matrixes is:\n");

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

printf("%d  ",prod_mat[i][j]);

}

printf("\n");

}

return 0;

}

 

int mat_trans(int mat1[3][3],int mat2[3][3])

{

int trans_mat1[3][3],trans_mat2[3][3],i,j;

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

trans_mat1[i][j]=mat1[j][i];

trans_mat2[i][j]=mat2[j][i];

}

}

printf("\n\n\n The transpord matrix1 is = \n");

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

printf("%d  ",trans_mat1[i][j]);

}

printf("\n");

}

printf("\n\n\n The transpord matrix2 is = \n");

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

printf("%d  ",trans_mat2[i][j]);

}

printf("\n");

}

return 0;

}

 

Output:-

 

 Enter the elements of the matrix1:

9 8 7

6 5 4

3 2 1

 

 Enter the elements of the matrix2:

1 2 3

4 5 6

7 8 9

 

The entered matrix1 is

9  8  7

6  5  4

3  2  1

 

The entered matrix2 is

1  2  3

4  5  6

7  8  9

 

        MENU

1. Addition

2. Subtraction

3. multiplication

4. Transpose

0. Exit

Enter your choice1

Addition of two matrix=

10  10  10

10  10  10

10  10  10

Do you want to continue(y/n)?y

 

 

 

Enter your choice2

Subtraction of two matrix=

  8     6     4

  2     0    -2

 -4    -6   -8

 

 

Do you want to continue(y/n)?y

Enter your choice4

 The transpord matrix1 is =

9  6  3

8  5  2

7  4  1

The transpord matrix2 is =

1  4  7

2  5  8

3  6  9

 

FW: Data Structure Lab Programs - Using iteration concept write program for finding the element in the array using Binary Search Method.

Practical # 3

 

/* Using iteration concept write program for finding the element in the array using Binary Search Method. */

 

#include<stdio.h>

#include<conio.h>

void bsearch(int x[],int item,int n)

{

int start,end,mid,f=0;

end=n-1;

start=0;

 

while(end>=start)

                {

                mid=(start+end)/2;

                if(item==x[mid])

                                {

                                printf("Item Found At Possition %d",mid+1);

                                f=1;

                                break;

                                }

                else if(item>x[mid])

                                {

                                start=mid+1;

                                }

                else

                                {

                                end=mid-1;

                                }

                }

if(f==0)

                {

                printf("Item Not Found");

                }

}

void main()

{

int x[10],i,n,item;

clrscr();

printf("Enter No. Of Element : ");

scanf("%d",&n);

printf("Enter Sorted Element :..\n");

for(i=0;i<n;i++)

                {

                scanf("%d",&x[i]);

                }

printf("Enter Item To Search : ");

scanf("%d",&item);

bsearch(x,item,n);

getch();

}

 

OUTPUT :-

 

Enter No. Of Element : 6

Enter Sorted Element :..

3

6

8

9

23

34

Enter Item To Search : 9

Item Found At Possition 4

 

Data Structure Lab Programs - recursion concept write program for finding the element in the array using Binary Search Method.

Practical # 2

 

/* Using recursion concept write program for finding the element in the array using Binary Search Method. */

 

#include<stdio.h>

#include<conio.h>

void main()

{

int x[10],n,i,e,found;

clrscr();

printf("Enter total no's of element=");

scanf("%d",&n);

if(n>10)

{

printf("OVERFLOW");

}

else

{

printf("The elements are=");

for(i=0;i<n;i++)

{

scanf("%d",&x[i]);

}

}

printf("Enter item to be searched=");

scanf("%d",&e);

found=binary_search(x,e,0,n-1);

if(found==-1)

{

printf("Item not found");

}

else

{

printf("Item found at location=%d",found+1);

}

getch();

}

binary_search(int x[],int e,int begin,int end)

{

int mid;

if(begin>end)

return -1;

mid=(begin+end)/2;

if(x[mid]==e)

return mid;

else if(e>x[mid])

binary_search(x,e,mid+1,end);

else

binary_search(x,e,begin,mid-1);

}

 

Output :-

 

Enter total no's of element=6

The elements are=2

5

7

9

12

34

Enter item to be searched=12

Item found at location=5

 

 

 

 

Data Structure Lab Programs - Write a program to search an element in a two dimensional array using linear search

Practical # 1

 

/* Write a program to search an element in a two dimensional array using linear search .*/

 

#include<stdio.h>

#include<conio.h>

void main()

{

int arr[10][10],n,m,i,j,search;

clrscr();

printf("\n\n\t\tLinear Search");

printf("\n\n\nNo. of rows of an array:");

scanf("%d",&n);

printf("\nNo. of columns of an array:");

scanf("%d",&m);

printf("\nEnter the elements of an array:\n");

for(i=0;i<n;i++)

{

for(j=0;j<m;j++)

scanf("%d",&arr[i][j]);

}

printf("\nThe matrix entered is\n");

for(i=0;i<n;i++)

{

for(j=0;j<m;j++)

{

printf("%d ",arr[i][j]);

}

printf("\n");

}

printf("\nEnter the element to be searched:");

scanf("%d",&search);

int pos=0,pos1=0;

for(i=0;i<n;i++)

{

for(j=0;j<m;j++)

{

if(arr[i][j]==search)

{

pos=i+1;

pos1=j+1;

}

}

}

if(pos>0 || pos1>0)

{

printf("\n\nThe position of the searched element is[%d][%d]",pos,pos1);

printf("\nwhere %d is row",pos);

printf("\nwhere %d is column",pos1);

}

else

printf("\n\nThe element does not exist in the matrix");

getch();

}

 

 

 

 

Output :-

 

Linear Search

No. of rows of an array:2

No. of columns of an array:2

 

Enter the elements of an array:

1 2

3 4

 

The matrix entered is

1 2

3 4

 

Enter the element to be searched:3

 

 

The position of the searched element is[2][1]

where 2 is row

where 1 is column

 

 

 

Data Structure Lab Programs - Index


Index



S.No.           Name of Practical                                                                   Signature
 
  1.   Program to search an element in a two dimensional array using linear search.
  2. Using recursion concept program for finding the element in the array using
    Binary Search Method.
  3. To Using iteration concept program for finding the element in  he array using Binary      Search Method.
  4. Write a program following operation on tables using functions only 
     a)Addition  b)Subtration c)Multiplication d)Transpose 
  5.  Write a program to sort an array using the quick  sort.
  6. Write a Program to implement the various operation on string such as  
     a)String Length   b)String Concatenate  c)String Reverse  d)String Copy
  7. Write a program for swapping of two numbers using call by value
    and call by reference strategies.
  8.   Linked list implementation.
  9.  Program to reverse a linked list.
  10.  To save data on disk & read data from disk.

Sunday, October 2, 2011

The query operator 'ElementAt' is not supported.

Scenario:
I have products list sorted according to their work priority and now I want to fetch
the next product  to number of processed products as:
int noOfProducts = context.ProcessedProducts.Count(product => product.ClientId.Equals(clientID));
    if (noOfProducts > 0)

    {

      nextProductID = context.Products.OrderBy(prd => prd.Priority).Where(prod => 
                               prod
.Equals(clientID)).ElementAt(noOfProducts +1).Id; //error

    }

Here I got an exception of
NotSupportedException: The query operator 'ElementAt' is not supported.

Reason:
The exception is telling you what you need to know.  LINQ to SQL cannot translate
ElementAt into SQL.  It does not support translating any of the query operators that
have an index argument.

Solutions:
Instead you can used the First, FirstOrDefault, Single or SingleOrDefault operators. 

int x = resultToAdd.First().ID;

or
use skip and take statement to improve processing

query.Skip(2).Take(1).Single();

or
This solution is little inefficient, because the whole query is executed and converted
to a List.
query.Take(2).ToList().ElementAt(1);

and I have I implemented my solution as:
nextProductID = context.Products.OrderBy(prd => prd.Priority).Where(prod => 
                               prod.Equals(clientID)).Skip(noOfProducts).Take(1).Single().Id;