Assignment 6 :     Arrays                                                   Date : 26th  Feb 2007               


Tutorial 6A: Write a C program to compute any Arithmetic series using an array

                                     #include<stdio.h>

                                     #define N 100

                                     int main()

                                     {

                                       int i,f,d,n;

                                      int a[N];

                                       printf("Enter the no. of terms to be printed\n");

                                      scanf("%d",&n);

                                       printf("Enter the first term of the series\n");

                                       scanf("%d",&f);

                                       printf("Enter the difference between any two terms\n");

                                       scanf("%d",&d);

                                       a[0]=f;

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

                                       a[i]=a[i-1]+d;

                                        printf("The series is \n");

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

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

                                        }  

 

Input: n=5, f=1, d=3

Output: 1,4,7,10,13



Assign. 6A:

Write a C program to compute the Fibonacci series up to n terms using an array.

               The Fibonacci series is defined as follows:

                                         0                      if      n = 0         

           F(n)  =                  1                       if      n = 1

                                         F(n-1)+F(n-2)     other wise

 

 

Input: 7      Output: 0,1,1,2,3,5,8



Tutorial 6B:

Write a C program to perform set difference operation on two given sets using arrays.

 Hint: set difference of two sets A and B is the set of elements that are present only in A but not in B.

 

                        #include<stdio.h>

                        #define N 100

                        int main()

                        {

                          int set1[N],set2[N],i,j,s1,s2,c;

                          printf("Enter the size of first array");

                          scanf("%d",&s1);

                          printf("Enter the elements of first array");

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

                          {

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

                          }

                          printf("Enter the size of second array");

                          scanf("%d",&s2);

                          printf("Enter the elements of second array");

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

                          {

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

                          }

                          printf("{ ");

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

                               {

                                      for(j=0,c=0;j<s2;j++)

                                           if(set1[i]!=set2[j])

                                                c++;

                                       if(c==s2)

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

                               }

                           printf("}");

                           }    

Input:      A={1,2,3}

              B={1,3}

Output: {2}



Assignment 6B:

Write a C program to perform union, intersection, cartesian product of two given sets using arrays.

Input : {1,2,3}, {3,4}

Output:

Union : {1,2,3,4}

Intersection : {3}

Cartisian product :{ {1,3},{1,4},{2,3},{2,4},{3,3},{3,4} }

                           



Tutorial 6C:

Write a C program to eliminate duplicates from an array of real numbers.

                        #include<stdio.h>

                        int main()

                        {

                         int a[100],i,j,k,n,flag=1;

                         printf("Enter the length of array ");

                         scanf("%d",&n);

                         printf("Enter the array ");

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

                         {

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

                         }

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

                         {

                              for(j=i+1;j<n;j++)

                            {

                                     if(a[i]==a[j])

                                     {

                                         while(flag==1)

                                                  {

                                              for(k=j;k<n;k++)

                                                        {

                                                              a[k]=a[k+1];

                                                      }

                                               n=n-1;

                                               flag=0;

                                               if(a[i]==a[j])

                                               {

                                                  flag=1;

                                               }

                                               }

 

                                     }

                              }

                         }

                         printf("The array without duplicates is:");

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

                         {

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

                         }

                           }

Input:  3,5,4,3,6,4                        Output: 3,5,4,6

Input: 4,4,4                                    Output: 4

     



Assign 6C:

Write a C program to store “n” real numbers in an array and find the highest frequent element(s) from that array

 

Input: 2,4,6,1,4,5,3,4,2           Output: 4

Input: 3,4,2,3,5,2                   Output: 2,3

 



Assign. 6D:

Write a C program to find the number of times a substring appears in a given string.

                               (String handling library functions can be used)

Input:                                                             

Given string:  mportporg

Sub string   :  por               

Output        : 2