Thursday, November 3, 2011

Arrays [III] [Mean-Median/Std. Dev. Frequency, Set & Pascal Trangle ]



Pascal Triangle using single dimension Array.


#include <stdio.h>
#include <conio.h>

// Pascal Triangle

int main()
  {
      int Pas[20],n,i,j,k;
      printf("\n\tEnter size below 20 : ");
      scanf("%d",&n);
      for(i=0;i<n;i++)
           {
              Pas[i]=0;
           }
      Pas[0]=1;
      for(i=1;i<n;i++)
           {
              for(j=0;j<i;j++)
                   {
                        printf("%d ",Pas[j]);
                   }
              printf("\n");
              for(k=i+1;k>=1;k--)
                   {                 
                        Pas[k]=Pas[k]+Pas[k-1];
                   }
           }      
      getch();
      return 0;
  }

Finding Mean and Median of ‘N’ Numbers


#include <stdio.h>
#include <conio.h>

//Mean & Median

  void sort(int a[],int n)
      {
           int i,j,tmp;
           for(i=0;i<n;i++)
              {
                   for(j=0;j<n-i-1;j++)
                        {
                            if(a[j]>a[j+1])
                                {
                                      tmp=a[j];
                                      a[j]=a[j+1];
                                      a[j+1]=tmp;
                                }
                        }
              }
      }

  int main()
      {
           int a[20],i,n,s=0;
           float k,m;
           printf("\nEnter the Size (below 20) : ");
           scanf("%d",&n);
           for(i=0;i<n;i++)
              {
                   printf("Enter Element at[%d] :",i);
                   scanf("%d",&a[i]);
                   s=s+a[i];
              }
           printf("\nValues  :\n");
           for(i=0;i<n;i++)
              {
                   printf("%d ",a[i]);
              }
           k=s/n;
           sort(a,n);
           printf("\nValues after Sort :\n");
           for(i=0;i<n;i++)
              {
                   printf("%d ",a[i]);
              }
           if(n%2!=0)
              {
                   m=a[n/2];
              }
           else
              {
                   m=(a[n/2]+a[(n/2)-1])/2.0;
              }
           printf("\nMean :  %8.2f  and Median  :  %8.2f",k,m);
           getch();
           return 0;
      }


Set – Union, Intersection, Minus.


#include <stdio.h>
#include <conio.h>

// Union, Intersection, Minus.

void input(int x[],int n)
    {
  int i,j;
  for(i=0;i<n;i++)
    {
       printf("\nEnter Value of a[%d] : ",i);
       scanf("%d",&x[i]);
       for(j=i-1;j>=0;j--)
      {
         if(x[i]==x[j])
            {
           printf("\n\t\t**** Duplicate Entry, Enter again ****");
            i--;
            }
       }
    }

    }


  void print(int x[],int n)
    {
  int i;
  for(i=0;i<n;i++)
    {
       printf("\t%d ",x[i]);

    }
  printf("\n");
    }

  int inter(int x[],int y[],int z[],int n)
    {
  int i,j,k=0,f;
  for(i=0;i<n;i++)
    {
       f=0;
       for(j=0;j<n;j++)
      {
         if(x[i]==y[j])
            {
            f=1;
            }
       }
        if(f==1)
       z[k++]=x[i];
    }
    return k;
    }

  int unions(int x[],int y[],int z[],int n)
    {
  int i,j,k=0,f;
  for(i=0;i<n;i++)
    {
       z[i]=x[i];
    }
  k=n;
  for(i=0;i<n;i++)
    {
       f=0;
       for(j=0;j<n;j++)
      {
         if(z[i]==y[j])
            {
            f=1;
            }
       }
           if(f==0)
       z[k++]=y[i];
    }
    return k;
   }

  int min(int x[],int y[],int z[],int n)
   {
  int i,j,k=0,f;
  for(i=0;i<n;i++)
    {
        f=0;
        for(j=0;j<n;j++)
      {
           if(x[i]==y[j])
              {
            f=1;
              }
       }
           if(f==0)
       z[k++]=x[i];
    }

    return k;
   }

  int main()
   {
       int arr[10],arr1[10],un[20],intr[10],mins[10];
       int n,c;
       printf("\n\tEnter Nos. of input (Below 10 ) ");
       scanf("%d",&n);
       printf("\n\tFirst Array \n");
       input(arr,n);
       printf("\n\tFirst Array \n");
       input(arr1,n);
       printf("\n\tFirst Array \n");
       print(arr,n);
       printf("\n\tSecond Array \n");
       print(arr1,n);
       c=unions(arr,arr1,un,n);
       printf("\n\tAfter Union \n");
       print(un,c);
       printf("\n\tAfter Intersection \n");
       c=inter(arr,arr1,intr,n);
       print(intr,c);
       printf("\n\tAfter A-B  Minus \n");
       c=min(arr,arr1,mins,n);
       print(mins,c);
       printf("\n\tAfter B-A  Minus \n");
       c=min(arr1,arr,mins,n);
       print(mins,c);

       getch();
       return 0;
   }

Print the frequency of each numbers.


#include <stdio.h>
#include <conio.h>

//Frequency of each No.

int main()
      {
           int n,i,j,c,f,k;
           int a[20];
           printf("Enter the Size (below 20) : ");
           scanf("%d",&n);
           for(i=0;i<n;i++)
              {
                   printf("Enter Element at [%d] : ",i);
                   scanf("%d",&a[i]);
              }
           printf("\n\tNos. in Array : ");
           for(i=0;i<n;i++)
              {
                   printf("%d  ",a[i]);
              }
           printf("\n\tFrequency :\n");
           for(i=0;i<n;i++)
              {
                   c=0;
                   f=0;
                   for(j=i+1;j<n;j++)
                        {
                            if(a[i]==a[j])
                            c++;
                        }
                   for(k=i-1;k>=0;k--)
                        {
                            if(a[i]==a[k])
                                {
                                      f=1;
                                      break;
                                }
                        }
                   if(f==0)
                        printf("\n\t%d repeated %d ",a[i],c+1);
              }
           getch();
           return 0;
      }

Finding Standard deviation for ‘n’ size array elements


#include <stdio.h>
#include <conio.h>
#include <math.h>

//Standard Deviation


  int main()
      {
          int a[20],i,n;
          float mean,sum=0;
          printf("\nEnter Numbers of Input [Below 20] : ");
          scanf("%d",&n);
          for(i=0;i<n;i++)
              {
                   printf("\n\tEnter the Element at [%d] :",i);
                   scanf("%d",&a[i]);
                   sum=sum+a[i];
              }
          printf("\n\tValues as Inputted : ");
          for(i=0;i<n;i++)
              {
                   printf("%d ",a[i]);
              }
          mean=sum/n;
          sum=0;
          for(i=0;i<n;i++)
              {
                   sum=sum+((a[i]-mean) * (a[i]-mean));
              }
          sum=sqrt(sum/n);
          printf("\n\tStandard Deviation of above Values are %f ",sum);
           getch();
           return 0;
      }


No comments: