Showing posts with label c simplified programs. Show all posts
Showing posts with label c simplified programs. Show all posts

Monday, 17 September 2012

Print Even position character


*c program for display character only that are stored in even position*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int i;
 char str[30];
 printf("Enter any string: ");
 gets(str);
 for(i=0; str[i]!='\0'; i++)
 {
   if(i%2==0)
      printf("%c",str[i]);
 }
 getch();
 return 0;
}

Saturday, 15 September 2012

Write a c program for selectioon sort


 Description:
                       This is the simplest method of sorting. In this method, to sort the data in ascending order, the 0th element is compared with all other eements. If the 0th element is found to be greater than the compared element then they are interchanged.

        Algorithm:
1)      Start
2)      Initiliaze the variables I,j,temp and arr[]
3)      Read the loop and check the condition. If the condition is true print the array elements and increment the I value. Else goto step 4
4)      Read the loop and check the condition. If the condition true then goto next loop.
5)      Read the loop and check the condition. If the condition true then goto if condition
6)      If the condition if(arr[i]>arr[j]) is true then do the following steps
i)                    temp=arr[i]
ii)                  arr[i]=arr[j]
iii)                arr[j]=temp
7)      increment the j value
8)      perform the loop operation for the displaying the sorted  
elements.
9)      print the sorted elements
10)  stop

 Program:
#incude<stdio.h>
#incude<conio.h>
Void main()
{
       Int arr[5]={25,17,31,13,2};
Int I,j,temp;
Clrscr();
Printf(“selection sort\n”);
Printf(“\n array before sorting:\n”);
For(i=0;i<=3;i++)
Printf(“%d\t,arr[i]”);
For(i=0;i<=3;i++)
{
      For(j=j+1;j<=4;j++)
{
             If(arr[i]>arr[j])
                {
                      Temp=arr[i];
                      Arr[i]=arr[j];
                     Arr[j]=temp;
               }
        }
}
Printf(“\n\n array after sortong:\n”);
For(i=0;i<=4;i++)
Printf(“%d\t”,arr[i]);
Getch();
}
Sampe input & output:
1)    Section sort
Array before sorting:
25   17  31        13     2
Array after sorting:
2    13    17    25    31
2)   section sort
Array before sort
25   31  30 12   1
Array after sort
1   12     25  30   31
Concusion: this program is error free
VIVA QUESATIONS
1)  The complexity of the section  sort algorithm ?
Ans:  O(n2)
2) 1) Drawback of the binary tree ?
Ans: Additional space is required for building the tree
3) The complexity of the heap sort algorithm ?
Ans: O(n og n)

Write a c program for heap sort


 Description:
                        In this method, a tree structure caed heap is used. A heap is type of a binary tree. An ordered baanced binary tree is caed a min-heap where the vaue at the roo of any sub tree is ess than or equa to the vaue of either of its chidern. Heap sort is basically an improvement over the binary tree sort.

        Algorithm:

              Heap sort
           SWAP FUNCTION

1. start

2. assign *a to temp

3. assign *b to *a

4. assign  temp to *b

5. stop


           HEAP SORT

1. start

2. assign n to i and a[n] to item

3. if i > 1 and a[i/2]< item repeat through step 4 other wise goto  
     step 5
            begin

4.          assign a[i/2] to a[i] and i/2 to i
            end if

5. assign item to a[i]

6. stop

  Program:
#include<stdio.h>
int a[20];
main()
{
int n,i;
clrscr();
printf("Enter number of elements: ");
scanf("%d",&n);
printf("Enter %d elements: ",n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
heapsort(n);
printf("Sorted elements are: \n");
for(i=1;i<=n;i++)
printf("%3d",a[i]);
getch();
}
heapsort(int n)
{
int t;
while(n>1)
{
         maxheap(n);
         t=a[1];
         a[1]=a[n];
         a[n]=t;
         n=n-1;
}
}

maxheap(int n)
{
int i,t,j;
for(i=2;i<=n;i++)
{
         t=a[i];
         j=i;
         while(a[j/2]<t&&j>1)
         {
         a[j]=a[j/2];
         j=j/2;
         }
         a[j]=t;
}
}

Input/Output:

Enter number of elements: 4
Enter 4 elements: 23
4
12
8
Sorted elements are:
  4  8 12 23


Enter number of elements: 6
Enter 6 elements: 67
23
6
45
99
78
Sorted elements are:
  6 23 45 67 78 99
Conclusion: 
    
                         The program is error free

VIVA QUESATIONS
1) Drawback of the binary tree ?
Ans: Additional space is required for building the tree

2) The complexity of the heap sort algorithm ?
Ans: O(n og n)


Program to Implement Traezodial and simpson methods.


Traezodial method:

Algorithm:

Step 1. Read x1, x2, e { x1 and x2 are the two end points of  the 
            internal the allowed error in integral is e}

Step 2. h=x2-x1

Step 3. SI = (f(x1) + f(x2))/2;

Step 4. I = h-si

Step 5. i=1 Repeat

Step 6. x=x1 + h/2

Step 7. for J= 1 to I do

Step 8. SI= SI + f(x)

Step 9. x=x+h
Endfor

Step 10. i=21

Step 11. h=h/2 { Note that the internal has been halved above and 
             the number of points where the function has to be computed     
             is doubled}

Step 12.i0=i1

Step 13. i1 = h.si

Step 14. until / I1-i0 / <=c./i1/

Step 15. Write I1,h,i

Step 16. Stop

 Program:


#include<stdio.h>
#include<math.h>
main()
{
float h,a,b,n,x[20],y[20],sum=0,integral;
int i;
clrscr();
printf("enter the value ofa,b,n:");
scanf("%f %f %f",&a,&b,&n);
printf("enter the values of x:");
for(i=0;i<=(n-1);i++)
{
scanf("%f",&x[i]);
}
printf("\n enter the values of y:");
for(i=0;i<=(n-1);i++)
{
scanf("%f",&y[i]);
}
h=(b-a)/n;
x[0]=a;
for(i=1;i<=n-1;i++)
{
x[i]=x[i-1]+h;
sum=sum+2*y[i];
}
sum=sum+y[b];
integral=sum*(h/2);
printf("approximate integral value is: %f",integral);
getch();
}












Input/Output:

Enter the values of a,b,n
1
2
3
Enter the values of x:
1
2
3
Enter the values of y:
1
2
3
Approximate integral value is 2.166667
Conclusion: The program is error free






























           Simpsons Method:

   Algorithm:

Step 1. Read x1,x2,e

Step 2. h=(x2-x1)/2

Step 3. i=2

Step 4. si=f(x1) + f(x2)

Step 5. s2=0

Step 6. s4=f(x1+h)

Step 7. I0=0

Step 8.  In =(s+4s4). (h/3)
             Repeat

Step 9. s2=s2+s4 {s2 stores already computed functional value and s4 the value computed in   the new    nitration }

Step 10. s4=0

Step 11. x=x1+h/2

Step 12. for j=1 to I do

Step 13. s4=s4+f(x)

Step 14. x=x+h

Step 15. h=h/2

Step 16. i=2i

Step 17. io=in

Step 18. in= (s1+2s2+4s4) . (h/3)
Step 19. until |In-Io|≤e. /in

Step 20. Write In,h,i

Step 21. STOP

                Program:

#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
float h,a,b,n,x[20],y[20],sum=0,itgl;
int i;
clrscr();
printf("enter the values of a,b,n");
scanf("%f%f%f",&a,&b,&n);
printf("enter the values of x");
for(i=0;i<=n;i++)
{
scanf("%f",&x[i]);
}
printf("\n enter the values of y");
for(i=0;i<=n;i++)
{
scanf("%f",&y[i]);
}
h=(b-a)/n;
a=x[0];
b=x[n];
for(i=0;i<=(n-2);i++)
{
x[i]=x[i]+h;
if(i%2==0)
{
sum=sum+4*y[i];
}
else
{
sum=sum+2*y[i];
}
}
itgl=sum*(h/3);
printf("integral value%f",itgl);
getch();
}





Input/Output:

Enter the values of a,b,n
1
2
3
Enter the value of x
4
5
6
7
Enter the values of y
8
9
1
2
Integral value is 5.555556


Conclusion: The program is error free

VIVA QUESATIONS
1)  Define Binary search ?
Ans: Binary search is a vast improvement over the sequential search. For binary search to work, the item in the list  must be in assorted order. The approach employed in the binary search is divid and conquer. If the list to be sorted for a specific item is not sorted, binary search fails.



Implement in ‘C’ the linear regression and polynomial regression algorithms


 Linear regression

Algorithm:

Step 1. Read n

Step 2. Sumx=0

Step 3. Sumxsq=0

Step 4. Sumy=0

Step 5. Sumxy=0

Step 6. fori=1 to n do

Step 7. Read x,y

Step 8. Sumx=sumx+x

Step 9.Sumxsq=Sumxsq+x2

Step 10.Sumy=Sumy+y

Step 11.Sumxy=sumxy+x x y end for

Step 12. denom=n x sumxsq – sumx x sumx

Step 13. a0=(sumy x  sumxsq – sumx x sumxy) / denom

Step 14. a1=(n x sumxy-sumx x sumy)/ denonm

Step 15. Write a1,a0

Step 16. STOP
 Program:

            #include<stdio.h>
            #include<math.h>
            Main()
            {
                        Int n,I;
                        Float sumx, sumxsq, sumy, sumxy, x, y, a0, a1, denom;
                        Printf(“enter the n value”);
                        Scanf(“%d”,&n);
                        Sumx=0;
                        Sumsq=0;
                        Sumy=0;
                        Sumxy=0;
                        For(i=0;i<n;i++)
                        {
                                    Scanf(“%f %f”,&x, &y);
                                    Sumx +=x;
                                    Sumsq += pow(x, 2);
                                    Sumy +=y;
                                    Sumxy +=x * y;
                        }
                        Denom = n * sumxsq – pow(sumx, 2);
                        A0 = (sumy * sumxsq –sumx *sumxy)/denom;
                        A1 = (n * sumxy –sumx *sumy)/denom;
                        Printf(“y= %fx + %f”,a1,a0);
                   }
Input/Output:
Enter the n value 7
1                2
2                5
4                7
5                10
6                12
8                15
9                19
Y = 1.980769x + 0.096154
Conclusion: The program is error free
VIVA QUESATIONS
1) What is the use of goto statement ?
Ans: The goto statement is used to alter the normal sequence of the program execution by unconditionally transferring control to some other part of the program.
 2) What is the use of continue statement ?
Ans: The continue statement is used to bypass the remainder of the current pass through a loop

Polynomial regression

Algorithm:

Sep 1: Strart

Step 2: Read n

Step 3: Initialize sumx = 0, sumxsq = 0, sumy = 0, sumxy = 0, sumx3 = 0, sumx4 = 0, sumxsq =0

Step 4: Intialize i=0

Step 5: Repeat steps 5 to 7 until i<n

Step 6: Read x,y

Step 7: Sumx = sumx + x
            Sumxsq =sumxsq + pow(x,2)
            Sumx3 = sumx3 + pow(x,3)
            Sumx4 = sumx4 + pow(x,4)
            Sumy = sumy + y
            Sumxy = Sumxy + x*y
            Sumxsqy = Sumxsqy + pow(x,2) *y

Step 8: Increment  I by 1

Step 9: Assign
         a[0][0] = n
         a[0][1] = n
a[0][2] = n
a[0][3] = n
a[1][0] = n
a[1][1] = n
a[1][2] = n
a[1][3] = n
a[2][0] = n
a[2][1] = n
a[2][2] = n
a[2][3] = n
                          
                           Step 10: Intialize i=0

Step 11: Repeat steps 11 to 15 until i<3

Step 12: Intialize j=0

Step 13: Repeat step 13 to 14 until j<=3

Step 14: Write a[i][j]

Step 15: Increment j by 1

Step 16: Increment I by 1

Step 17: Initialize k =0

Step 18: Repeat steps  18 to 27 until k<=2

Step 19: Intialize i=0

Step 20: Repeat step 20 to 26 until i<=2

Step 21: If  I not equal to k

Step 22: Asign u=a[i][k]/a[k][k]

Step 23: Intialize j=k

Step 24: Repeat steps 24 and 25 until j<=3

Step 25: Assign a[i][j] = a[i][j] – u *a[k][j]

Step 26: Increment j by 1

Step 27: Increment i by 1

Step 28: Increment k by 1

Step 29: Initialize I =0

Step 30: Repeat steps 31 to 33  until i<3

Step 31: Assign b[i] = a[i][3]/a[i][i]

Step 32: Write I, b[i]

Step 33: Increment I by 1

Step 34: Write b[2],b[i],b[0]
Step 35: Stop
Program:
           

            #include<stdio.h>
            #include<math.h>
main()
{
               Int n, I, j, k;
               Float sumx, sumxsq, sumy, sumxy, x, y;
               Float sumx3, sumx4, sumxsqy, a[20][20], u=0.0, b[20];
               Printf(“\n Enter the n value”);
               Scanf(“%d”, &n);
               Sumx = 0;
               Sumxsq = 0;
               Sumy = 0;
               Sumxy = 0;
               Sumx3 = 0;
               Sumx4 = 0;
               Sumxsqy = 0;
               For(i=0;  i<n; i++)
               {
                        Scanf(“%f %f”, &x, &y);
                        Sumx +=x;
                        Sumxsq += pow(x,2);
                        Sumx3 += pow(x,3);
                        Sumx4 += pow(x,4);
                        Sumy +=y;
                        Sumxy += x * y;
                        Sumxsqy += pow(x,2) *y;
               }
               A[0][0] = n;
               A[0][1] = sumx;
               A[0][2] = sumxsq;
               A[0][3] = sumy;
               A[1][0] = sumx;
               A[1][1] = sumxsq;
               A[1][2] = sumx3;
               A[1][3] = sumxy;
               A[2][0] = sumxsq;
               A[2][1] = sumx3;
               A[2][2] = sumx4;
               A[2][3] = sumxsqy;
for(i=0;  i<3; i++)
  for(j=0;  j<=3; j++)
               Printf(“%10.2f”,a[i][j]);
               Printf(“\n”);
            }
          For(k=0;  k<=2; k++)
          {
               For(i=0;i<=2;i++)
               {
                        If(i!=k)
                           U=a[i][k]/a[k][k];
                        For(j = k; j<=3; j++)
                                    A[i][j]=a[i][j] – u * a[k][j];
                }
        }

     For(i=0;i<3;i++)
     {
            B[i] = a[i][3]/a[i][i];
            Printf(“\nx[%d] = %f”, I, b[i]);
     }
  Printf(“\n”);
 Printf(“y= %10.4fx +10.4 fx +%10.4f”,b[2],b[i],b[0]);
}


Input/Output:

Enter the n value 10

-4                     21
-3                     12
-2                     4
-1                     1
 0                     2
 1                     7
 2                     15
 3                     30
 4                     45
 5                     67
               10.00            5.00                 85.00                  204.00
                 5.00            85.00               125.00                513.00
               85.00            125.00             1333.00              3193.00
X[0] = 2.030303
X[1] = 2.996970
X[2] = 1.984848
Y =         1.9848xsq +  2.9979x  + 2.0303
Conclusion: The program is error free

VIVA QUESATIONS
1)  Define insertion sort ?
Ans: Insertion sort is similar to playing cards. To sort the cards in yourhand you extrat a card shift the remaining cards and then insert the extracted card in its correct place.

2) Efficiency of the insertion sort ?
Ans: The efficiency of insertion sort is O(n2).





To implements the Lagrange interpolation and Newton Gregory forward interpolation



       Algorithm:

Step 1. Read x,n

Step2. for i=1 to (n+1) is steps of 1 do Read xi,fi end for {the above 
           statements reads x,s and the corresponding values of  f is }

Step 3. Sum=0

Step 4. for i=1 to (n+1) in steps of 1 do

Step 5. Profvnc=1

Step 6. for J=1 to (n+1) in steps of 1 do

Step 7. If (j≠i) then prodfunc=prodfunc X(x-xj) / (xi-xj) endfor

Step 8. Sum=Sum+fi x Prodfunc {sum is the value of f at x} end for

Step 9. Write x, sum

Step 10. STOP
Program:

#include<stdio.h>
#include<math.h>
Main()
{
  Float y, x[20],f[20],sum,pf;
  Int I,j,n;
  Printf(“enter the value of n”);
  Scanf(“%d”,&n);
  Printf(“enter the value to be found”);
  Scanf(“%f”,&y);
  Printf(“enter the values of xi’s & fi’s”);
  For(i=0;i<n;i++)
   {
        Pf=1;
         For(j=0;j<n;j++)
         {
                     If(j!=i)
                                 Pf  *= (y-x[j])/(x[i] – x[j]);
         }
         Sum += f[i] *pf;
 }
Printf(“\nx = %f ”,y);
Printf(“\n sum =%f ”,sum);
}

Input/Output:

Enter the value of n 4
Enter the value to be found 2.5
Enter the values for xi’s & fi’s
1                    1
2                    8
3                    27
4                    64
         X = 2.500000
         Sum = 15.625000
Conclusion: The program is error free

VIVA QUESATIONS
1) Define storage class ?
Ans: Storage class specifiers inform the complier how to store the variable; the storage clas specifiers in the c language are : auto, register, static,extern, typedef
           Newton  gregory  forward interpolation.

          Algorithm:

Step1: START

Step2: Read n

Step3: for i=0 to (n-1) do read xi,yi

Step4: read x

Step5: h←xi-x0

Step6: p←(x-xo)/n

Step7: for j=0 to n-2 do
   ∆1yj←yj+1-∆i-1

Step8: k←n-2

Step9: for i=2 to (n-1)do
    Step9.1: k←k-1
    Step9.2:for j=0 to  k do
      ∆iyj←∆i-1 yj+1-∆i-1yj

Step10:   Sumy←y0

Step11: Pvalue←1

Step12: Fact value←1

Step13: for l=1 to (n-1) do
   Step13.1: Pvalue←pvalue x (p-(l-1))
Step13.2: factvalue←factvaluex1
Step13.3: term←(pvalue x ∆ly) / factvalue
Step13.4: Sumy←Sumy+term

Step14: Print x,SUMY

Step15: STOP
 Program:

#include<stdio.h>
#include<math.h>
Main()
{
    Int i, j, n, k, l;
    Float sumy, h, term, p, z, pvalue;
    Float x[25], y[25], d[25][25], factvalue;
    Printf(“enter the value of n”);
    Scanf(“%d”,&n);
    Printf(“enter %d values for x, y \n”,n);
    For(i=0;i<n;i++)
     Scanf(“%f %f”, &x[i], &y[i]);
     Printf(“\n enter z”);
     Scanf(“%f”,&z);
     h = x[1] – x[0];
     p = (z - x[0] )/ h;
     for(j=0; j<n-2; j++)
         d[i][j] =y[j+1] – y[j];
         k=n-2;
    for(i=2; i<n; i++)
    {
         k++;
         for(j=0; j<=k; j++)
           d[i][j] =d[i-1][j+1] – d[i-1][j];
   }
   For(l=1; l<n; l++)
   {
         Pvalue *= (p-(l  - 1));
         Factvalue *= 1;
         Term = pvalue* d[l][0] / factvalue;
         Sumy += term;
 }
 Printf(“\n y value at z = %f is %f”, z, sumy);
}










Input/Output:

Enter n 7
Enter 7 data values for x, y

                  1921                35
1931                42
1941                58
1951                84
1961               120

1971              165
1981              220

Enter z 1925
Y value at z = 1925.000000 is 36.756710
Conclusion: The program is error free




VIVA QUESATIONS
1) What is the use of goto statement ?
Ans: The goto statement is used to alter the normal sequence of the program execution by unconditionally transferring control to some other part of the program.
 2) What is the use of continue statement ?
Ans: The continue statement is used to bypass the remainder of the current pass through a loop















Program to implement the merge sort method


Description:
The merge sort splits the list to be sorted into two equal halves, and places them in separate arrays. Each array is recursively sorted, and then merged back together to form the final sorted list. Like most recursive sorts, the merge sort has an algorithmic complexity of O(n log n).
Algorithm: main program
Step1: Start
Step2: declare the merge sort function
Step3: Declare the array and their size and initailaze the j=0
Step4: read the array elements and then sort these elements.
Step5: read the array elements before the merge sort and then display the   
           elements.
Step6: call the merge sort function
Step7: display the array elements after merge sort by using the following stament.
            for( j=0;j<Max_ary;j++)
Step8: Stop
Subprogram
Step1:initialize the array excuting[MAX_ARY] and 
           j=0,mid=0,mrg1=0,mrg2=0,size=start-end+1
Step2: check the condition if(end==start) then return
Step3: calculate the mid value
             Mid=(end+start)/2
Step4: call themerge_sort(x,end,mid)
Step5:merge_sort(x,mid+1,start)
Step6: performing the looping operation
             For(j=0;j<SIZE;j++) then its true
             Executing[j]=x[end+1]
Mrg1=0;
Step7: calculate the mrg2=mid-end+1
Step8: performing the looping operation
             For(j=0;j<SIZE;j++) then its true then goto step9
Step9: check the condition
i)                    if(mrg2<=start-end) is true goto ii). If not goto Step12.
ii)                  If(mrg1<=mid-end) is true goto iii). If not goto step11
iii)                If(executing[mrg1]>executing[mrg2]) is true then follows. If not goto step10.
X[j+end]= executing[mrg2++]
Step10: x[j+end]=executing[mrg1++]. If not goto Step11
Step11: x[j+end]= executing[mrg2++]
Step12: x[j+end]=executing[mrg1++]
Step13: return to main program
Program:
#include <stdio.h>
#include <stdlib.h>

#define MAX_ARY 10

void merge_sort(int x[], int end, int start);

int main(void) {
 int ary[MAX_ARY];
 int j = 0;

 printf("\n\nEnter the elements to be sorted: \n");
   for(j=0;j<MAX_ARY;j++)
       scanf("%d",&ary[j]);

 /* array before mergesort */
 printf("Before    :");
 for(j = 0; j < MAX_ARY; j++)
  printf(" %d", ary[j]);

 printf("\n");

 merge_sort(ary, 0, MAX_ARY - 1);

 /* array after mergesort */
 printf("After Merge Sort :");
 for(j = 0; j < MAX_ARY; j++)
  printf(" %d", ary[j]);

 printf("\n");
 getch();
}

/* Method to implement Merge Sort*/
void merge_sort(int x[], int end, int start) {
int j = 0;
 const int size = start - end + 1;
 int mid  = 0;
 int mrg1 = 0;
 int mrg2 = 0;
 int executing[MAX_ARY];

 if(end == start)
  return;

 mid  = (end + start) / 2;

 merge_sort(x, end, mid);
 merge_sort(x, mid + 1, start);

 for(j = 0; j < size; j++)
  executing[j] = x[end + j];

 mrg1 = 0;
 mrg2 = mid - end + 1;

 for(j = 0; j < size; j++) {
  if(mrg2 <= start - end)
   if(mrg1 <= mid - end)
    if(executing[mrg1] > executing[mrg2])
     x[j + end] = executing[mrg2++];
    else
     x[j + end] = executing[mrg1++];
   else
    x[j + end] = executing[mrg2++];
  else
   x[j + end] = executing[mrg1++];
 }
}
Output:
Enter the elements to be sorted:
8 2 3 4 1 5 7 6 9 0
Before    : 8 2 3 4 1 5 7 6 9 0
After Merge Sort : 0 1 2 3 4 5 6 7 8 9

Enter the elements to be sorted:
7 6 5 4 8 4 3 2 1 3
Before    : 7 6 5 4 8 4 3 2 1 3
After Merge Sort : 1 2 3 3 4 4 5 6 7 8
Conclusion: the program is error free
VIVA QUESATIONS
1) Define merge sort ?
Ans: The merge sort splits the list to be sorted into two equal halves, and places them in separate arrays. Each array is recursively sorted, and then merged back together to form the final sorted list.
2) Efficiency of merge sort ?
Ans: O(n log n).