3/19/2009

C Program To Find Mean, Median, Standard Deviation And Mode For A Given Set Of Values


/* C Program To Find Mean, Median, Standard Deviation And Mode For A Given Set Of Values */


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

void main()
{
    int a[10],b[10],i,j,k=0,temp,sum=0,n,choice,count=1,max=0,mode;

    float mean=0,median=0,sd,x,add=0,adds=0;
    printf("Enter the size of array:\n");
        scanf("%d",&n);
    printf("Enter The elements:\n");
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
      printf("Elements before sorting:\n");
         for(i=0;i<n;i++){
            printf("%d\n",a[i]);
         }

      for(i=0;i<n;i++)
      {
          for(j=0;j<n-1;j++)
          {

              if(a[j]>a[j+1])
              {
                  temp=a[j];
                  a[j]=a[j+1];
                  a[j+1]=temp;
              }
          }
      }
      printf("Sorted Elements:\n");
         for(i=0;i<n;i++)
            printf("%d\n",a[i]);


      for(;;)
      {
          printf("1.Mean\t2.Median\t3.Standard Deviation\t4.Mode\t5.Exit\n");
            scanf("%d",&choice);

           switch(choice)
           {
           case 1:
            for(i=0;i<n;i++)
            {
                sum+=a[i];
                mean=(float)sum/n;
            }
            printf("Mean= %.2f\n",mean);
            break;
           case 2:
               if(n/2==0){
                median=a[n/2];
                printf("Median= %f\n",median);
                break;
               }
               else
                median=(a[n/2]+a[n/2]+1)/2;

               printf("Median= %.2f\n",median);
               break;
            case 3:
            for(i=0;i<n;i++)
               add+=a[i];
                mean=add/n;
            for(i=0;i<n;i++)
                    adds+=adds+((a[i]-mean)*(a[i]-mean));
            x=adds/n;
            sd=sqrt(x);
            printf("Standard deviation= %.2f\n",sd);
            break;

            case 4:
                for(i=0;i<n-1;i++)
                    {
                        mode=0;
                    for(j=i+1;j<n;j++)
                    {
                        if(a[i]==a[j])
                        {
                            mode++;
                        }
                    }
                if((mode>max)&&(mode!=0))
                    {
                        k=0;
                        max=mode;
                        b[k]=a[i];
                        k++;
                    }
                else if(mode==max)
                    {
                        b[k]=a[i];
                        k++;
                    }
                }
            for(i=0;i<n;i++)
                {
                    if(a[i]==b[i])
                    count++;
                }
            if(count==n)
            printf("There is no mode!\n");
            else
            {
                printf("Mode:\n");
                for(i=0;i<k;i++)
                printf("%d\n",b[i]);
            }

                break;
            case 5:
                exit(0);

            default:
                printf("Invalid Choice!\n");
                break;
           }
      }

}
Out Put:
Enter the size of array:
6
Enter The elements:
2
3
4
8
2
5
Elements before sorting:
2
3
4
8
2
5
Sorted Elements:
2
2
3
4
5
8
1.Mean  2.Median        3.Standard Deviation    4.Mode  5.Exit
1
Mean= 4.00
1.Mean  2.Median        3.Standard Deviation    4.Mode  5.Exit
2
Median= 4.00
1.Mean  2.Median        3.Standard Deviation    4.Mode  5.Exit
3
Standard deviation= 6.03
1.Mean  2.Median        3.Standard Deviation    4.Mode  5.Exit
4
Mode:
2
1.Mean  2.Median        3.Standard Deviation    4.Mode  5.Exit
5

3/15/2009

C Program To Solve Equation Using GAUSS SEIDEL METHOD


/*C Program To Solve Equation Using GAUSS SEIDEL METHOD*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define ESP 0.0001
#define X1(x2,x3) ((17 - 20*(x2) + 2*(x3))/20)
#define X2(x1,x3) ((-18 - 3*(x1) + (x3))/20)
#define X3(x1,x2) ((25 - 2*(x1) + 3*(x2))/20)


void main()
{
  double x1=0,x2=0,x3=0,y1,y2,y3;
  int i=0;
  clrscr();
  printf("\n__________________________________________\n");
  printf("\n   x1\t\t   x2\t\t   x3\n");
  printf("\n__________________________________________\n");
  printf("\n%f\t%f\t%f",x1,x2,x3);
  do
  {
   y1=X1(x2,x3);
   y2=X2(y1,x3);
   y3=X3(y1,y2);
   if(fabs(y1-x1)<ESP && fabs(y2-x2)<ESP && fabs(y3-x3)<ESP )
   {
     printf("\n__________________________________________\n");
     printf("\n\nx1 = %.3lf",y1);
     printf("\n\nx2 = %.3lf",y2);
     printf("\n\nx3 = %.3lf",y3);
     i = 1;
   }
   else
   {
     x1 = y1;
     x2 = y2;
     x3 = y3;
     printf("\n%f\t%f\t%f",x1,x2,x3);
   }
  }while(i != 1);
getch();
}
Out Put:
__________________________________________

   x1              x2              x3

__________________________________________

0.000000         0.000000       0.000000
0.850000        -1.027500       1.010875
1.978588        -1.146244       0.880205
2.084265        -1.168629       0.866279
2.105257        -1.172475       0.863603
2.108835        -1.173145       0.863145
2.109460        -1.173262       0.863065
2.109568        -1.173282       0.863051
__________________________________________


x1 = 2.110

x2 = -1.173

x3 = 0.863

3/10/2009

C Program To Find The Root Of The Equation x*x*x-18=0 Using Regula Falsi Method


/* C Program To Find The Root Of The Equation x*x*x-18=0 Using Regula Falsi Method */


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

float f(float);
int main()
{
    float x0,x1,x2,y0,y1,y2,e;
    int i,n;

      printf("Enter the initial guess\n");
          scanf("%f%f",&x0,&x1);
       y0=f(x0);
       y1=f(x1);

       if((y0*y1)>=0)
       {
           printf("Invalid guess.\n");
           exit(0);
       }
      printf("Enter the allowable error:\n");
          scanf("%f",&e);
      printf("Enter the number of iterations:\n");
          scanf("%d",&n);

      printf("f(x)=x*x*x-18\n");
      printf("-------------------------------------------------------\n");
      printf("i\tx0\tx1\ty0\ty1\tx2\ty2\n");
      printf("-------------------------------------------------------\n");
      for(i=0;i<n;i++)
      {
          x2=((x0*y1)-(x1*y0))/(y1-y0);
          y2=f(x2);

          printf("%d\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\n",i,x0,x1,y0,y1,x2,y2);

          if(fabs(y2)<=e)
          {
              printf("-------------------------------------------------------\n");
              printf("Converges to a root at %d iteration.\n",i);
              printf("Root is %.3f",x2);
              exit(0);
          }
          if(y0*y2>0)
          {
              x0=x2;
              y0=y2;
          }
          else{
            x1=x2;
            y1=y2;
          }
      }
      printf("-------------------------------------------------------\n");
      printf("Does not converges to root in %d iterations.\n",i);
      printf("Root is= %.3f\n",x2);

        return 0;
}
   float f(float x)
   {
       return(x*x*x-18);
   }
Out Put:
Enter the initial guess
2
3
Enter the allowable error:
0.001
Enter the number of iterations:
6
f(x)=x*x*x-18
-------------------------------------------------------
i       x0      x1      y0      y1      x2      y2
-------------------------------------------------------
0       2.000   3.000   -10.000 9.000   2.526   -1.876
1       2.526   3.000   -1.876  9.000   2.608   -0.261
2       2.608   3.000   -0.261  9.000   2.619   -0.035
3       2.619   3.000   -0.035  9.000   2.621   -0.005
4       2.621   3.000   -0.005  9.000   2.621   -0.001
-------------------------------------------------------
Converges to a root at 4 iteration.
Root is 2.621

C Program To Find The Root Of The Equation x*x-25 Using Bisection Method


/* C Program To Find The Root Of The Equation x*x-25 Using Bisection Method */


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

float f(float);
int main()
{
    float x0,x1,x2,y0,y1,y2,e;
    int i;

      printf("Enter the initial guess\n");
          scanf("%f%f",&x0,&x1);
       y0=f(x0);
       y1=f(x1);

       if((y0*y1)>0)
       {
           printf("Invalid guess.\n");
           exit(0);
       }
      printf("Enter the allowable error:\n");
          scanf("%f",&e);


      printf("f(x)=x*x-25\n");
      printf("-------------------------------------------------------\n");
      printf("i\tx0\tx1\ty0\ty1\tx2\ty2\n");
      printf("-------------------------------------------------------\n");
          i=0;
        while((x1-x0)/x1>e)
        {
            printf("%d\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\n",i,x0,x1,y0,y1,x2,y2);

            x2=(x0+x1)/2;
            y2=f(x2);
            if(y0*y0>0)
            {
                x0=x2;
                y0=y2;
            }
            else{
                x1=x2;
                y1=y2;
            }
            i++;

        }
        printf("-------------------------------------------------------\n");
        printf("The root is %.3f",x2);

        return 0;
}
   float f(float x)
   {
       return(x*x-25);
   }
Out Put:
Enter the initial guess
2
7
Enter the allowable error:
.01
f(x)=x*x-25
-------------------------------------------------------
i       x0      x1      y0      y1      x2      y2
-------------------------------------------------------
0       2.000   7.000   -21.000 24.000  0.000   0.000
1       4.500   7.000   -4.750  24.000  4.500   -4.750
2       5.750   7.000   8.063   24.000  5.750   8.063
3       6.375   7.000   15.641  24.000  6.375   15.641
4       6.688   7.000   19.723  24.000  6.688   19.723
5       6.844   7.000   21.837  24.000  6.844   21.837
6       6.922   7.000   22.912  24.000  6.922   22.912
-------------------------------------------------------
The root is 6.961