Saturday, 5 January 2013

Data Structure Using C

Practical 1

1. write a program to interchange the largest and smallest number from an array and also display its position.
#include<stdio.h>
#include<conio.h>
void swapMinMax(int arr[],int no)
{
int min = arr[0];
int max = arr[0];

int i,p_min=0,p_max=0,temp;
for(i=0;i<no;i++)
{
if(min>arr[i])
{
min=arr[i];
p_min=i;
}
if(max<arr[i])
{
max=arr[i];
p_max=i;
}
}

printf("\n\nminimum value is %d at a[%d] position",min,p_min);
printf("\n\nmaximum value is %d at a[%d] position",max,p_max);

temp = arr[p_min];
arr[p_min] = arr[p_max];
arr[p_max] = temp;
}
void main()
{
int a[50],i,no;
clrscr();

printf("\n\nEnter the size of the array: ");
scanf("%d",&no);

printf("\n\nEnter %d elements in to the array: \n\n", no);
for(i=0;i<no;i++)
{
printf("Enter value at a[%d] = ",i);
scanf("%d",&a[i]);
}
printf("\n\n= = = Before swap the value = = = ");
swapMinMax(a,no);

printf("\n\n= = = after swap the value = = = ");
swapMinMax(a,no);
getch();
}
Click Here to Download
2. write a program to find the second largest number using an array of n numbers.                   
#include<stdio.h>
#include<conio.h>
void main()
{
            int a[50],size,i,j=0,big,secondbig;
            clrscr();
            printf("\n\nEnter the size of the array: ");
            scanf("%d",&size);

            printf("\n\nEnter %d elements in to the array: \n\n", size);
            for(i=0;i<size;i++)
            {
                        printf("Enter value at a[%d] = ",i);
                        scanf("%d",&a[i]);
            }
            big=a[0];
            for(i=1;i<size;i++)
            {
                  if(big<a[i])
                  {
                          big=a[i];
                          j = i;
                  }
            }
            secondbig=a[size-j-1];
            for(i=1;i<size;i++)
            {
                        if(secondbig <a[i] && j != i)
                        secondbig =a[i];
            }
            printf("\n\nSecond biggest: %d", secondbig);
            getch();
}
3. write a program to delete a number from a given location an array.
#include<stdio.h>
#include<conio.h>
void main()
{
            int arr[10], i, j, size, no,flag;
            clrscr();
            printf("Enter the size of an array : ");
            scanf("%d",&size);

            printf("\nEnter %d elements in array \n\n", size);
            for(i=0;i<size;i++)
            {
                printf("arr[%d] = ",i);
                scanf("%d",&arr[i]);
            }
            printf("\n\nEnter the element for deletion :");
            scanf("%d",&no);

            for(i=0;i<size;i++)
            {
               if(no==arr[i])
               {
                        for(j=i;j<size;j++)
                           arr[j]=arr[j+1];
                        size--;
                        flag=0;
                        break;
               }
               else
               {
                        flag=1;
               }
            }

            if(flag==1)
               printf("\nElement not found in array ");
            else
            {
                for(i=0;i<size;i++)
                {
                        printf("\narr[%d] = %d",i,arr[i]);
                }
            }
            getch();
}
4. write a program to transpose the n x n matrix.
#include<stdio.h>
#include<conio.h>
void main()
{
   int row, column, i, j, matrix[10][10], transpose[10][10];
   clrscr();
   printf("Enter the number of rows : ");
   scanf("%d",&row);
   printf("Enter the number of columns : ");
   scanf("%d",&column);
   printf("\nEnter the elements of matrix \n");

   for(i=0 ; i<row ; i++)
   {
      for( j=0 ; j<column ; j++ )
      {
             printf("matrix[%d] = ",i);
             scanf("%d",&matrix[i][j]);
      }
   }

   printf("\n\nentered matrix is :\n\n");
   for( i=0 ; i<row ; i++ )
   {
      for( j=0 ; j<column ; j++ )
      {
             printf("%d\t",matrix[i][j]);
      }
      printf("\n");
   }

   for( i=0 ; i<row ; i++ )
   {
      for( j=0 ; j<column ; j++ )
      {
             transpose[j][i] = matrix[i][j];
      }
   }
   printf("\n\nTranspose of entered matrix :-\n");
   for( i=0 ; i<column ; i++ )
   {
      for( j=0 ; j<row ; j++ )
      {
             printf("%d\t",transpose[i][j]);
      }
      printf("\n");
   }
   getch();
}
Click Here to Download
5. write a program to find greatest three integer using pointer and user define function.
#include<stdio.h>
#include<conio.h>
void swap(int *no1,int *no2, int *no3)
{
            if( *no1>*no2 && *no1>*no3 )
                        printf("\n\n\t %d is greatest value ",*no1);
            else if(*no2>*no3)
                        printf("\n\n\t %d is greatest value ",*no2);
            else
                        printf("\n\n\t %d is greatest value ",*no3);
}
void main()
{
            int number1, number2, number3;
            clrscr();
            printf("Enter first number : ");
            scanf("%d",&number1);
            printf("Enter second number : ");
            scanf("%d",&number2);
            printf("Enter third number : ");
            scanf("%d",&number3);
            swap(&number1,&number2,&number3);
            getch();
}
6. Write a program to find sum and average of array using pointer.
#include<stdio.h>
#include<conio.h>
void main()
{
            int *p,arr[10],size,i;
            float average,sum=0;
            clrscr();
            printf("Enter size of array : ");
            scanf("%d",&size);

            for(i=0;i<size;i++)
            {
                        printf("arr[%d] : ",i);
                        scanf("%d",&arr[i]);
            }
            p = &arr[0];

            for(i=0;i<size;i++)
            {
                        sum =sum + *p;
                        p++;
            }
            printf("\n\nSum of elements : %.2f",sum);
            average = (float)(sum/size);
            printf("\n Average is = %.2f",average);
            getch();
}
Click Here to Download

No comments:

Post a Comment