Thursday, October 27, 2011

Arrays [II] [Basic Array Based Program]



Some more source code with arrays –


Finding the Second largest Number in an Array.


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

//Second Largest No.

void main()
      {
           int Arr[20],i,n,max,smax;
           clrscr();
           printf("\n\tEnter Array Size below 20 : ");
           scanf("%d",&n);
           for(i=0;i<n;i++)
              {
                   printf("\n\tEnter Array Element at [%d] : ",i);
                   scanf("%d",&Arr[i]);
              }
           smax=max=Arr[0];
           printf("\n\tNumbers in Array : \n");
           for(i=0;i<n;i++)
              {
                   printf("\n\t\t%d",Arr[i]);
                   if(Arr[i]>max)
                        {
                            max=Arr[i];
                        }
                   if(Arr[i]<smax)
                        {
                            smax=Arr[i];
                        }
              }
           for(i=0;i<n;i++)
              {
                   if(Arr[i]>smax && Arr[i]<max)
                        {
                           smax=Arr[i];
                        }
              }

           printf("\n\tSecond Largest No. is : %d",smax);
           getch();
      }

Remove Duplicate values from Array.



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

//Remove Duplicate Element

void main()
      {
           int Arr[20],n,i,j,k,flag;
           clrscr();
           printf("\n\tEnter Size (below 20) : ");
           scanf("%d",&n);
           for(i=0;i<n;i++)
              {
                   printf("\tEnter element at [%d] : ",i);
                   scanf("%d",&Arr[i]);
              }
           printf("\n\tElements in Array : ");
           for(i=0;i<n;i++)
              {
                   printf("%d ",Arr[i]);
              }
           for(i=0;i<n;i++)
              {
                   flag=0;
                   for(j=i+1;j<n;j++)
                        {
                            if(Arr[i]==Arr[j])
                                {
                                      flag=1;
                                      break;
                                }
                        }
                   if(flag==1)
                        {
                            for(k=j;k<n-1;k++)
                                {
                                      Arr[k]=Arr[k+1];
                                }
                            n--;
                        }
              }
           printf("\n\tAfter Removing Duplicate : ");
           for(i=0;i<n;i++)
              {
                   printf("%d ",Arr[i]);
              }

           getch();
      }

Array Operation : Add, Remove, Append, Sort and Search in an Array


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

//Add, Remove, Append, Sort and Search in an Array

int arr[20],c;
void view(int);
int append(int);
int del(int);
int inst(int);
void sort(int);
void ser(int);
  int main()
      {
           int ch=0,n,i=0;char x;
           c=0;
           printf("\n\tHow many no. (Below 20)? : ");
           scanf("%d",&n);
           for(i=0;i<n;i++)
              {
                   printf("\tEnter a no at [%d]:",i);
                   scanf("%d",&arr[i]);
              }
           c=n;
           while(ch<6)
              {
                   printf("\n\t1. Append");
                   printf("\n\t2. Delete");
                   printf("\n\t3. Insret");
                   printf("\n\t4. View");
                   printf("\n\t5. Sort");
                   printf("\n\t6. Search");
                   printf("\n\t7. Exit\n");
                   printf("\n\tEnter Your Choice : ");
                   scanf("%d",&ch);
                   switch(ch)
                        {
                            case 1:  c=append(c);break;
                            case 2:  c=del(c);break;
                            case 3:  c=inst(c);break;
                            case 4:  view(c);break;
                            case 5:  sort(c);break;
                            case 6:  ser(c);break;
                            case 7:  printf("\n\n\tQuit...");exit(0);
                            default: printf("\n\tInvalid Option ");
                        }
           }
              getch();
              return 0;
      }

  void view(int c)
      {
           int i=0;
           printf("\n\tNos. is in Array : ");
           for(i=0;i<c;i++)
              {
                   printf("%d ",arr[i]);
              }
           getch();
      }
  int append(int c)
      {
           if(c>=20)
              {
                   printf("\n\t\tArray is Full ");
              }
           else
              {
                   printf("\n\tEnter No. to Add at the End : ");
                   scanf("%d",&arr[c]);
                   c++;
              }
           return c;
      }
  int del(int c)
      {
           int p,i;
           printf("\n\tEnter Position : ");
           scanf("%d",&p);
           for(i=p;i<c;i++)
              {
                   arr[i]=arr[i+1];
              }
           c--;
           return c;
      }

  int inst(int c)
      {
           int p,i;
           if(c>=20)
              {
                   printf("\n\tArray is Full ");
              }
           else
              {
                   printf("\n\t Enter Position : ");
                   scanf("%d",&p);
                   for(i=c;i>=p;i--)
                        {
                            arr[i+1]=arr[i];
                        }
                   printf("\n\tEnter the no: ");
                   scanf("%d",&arr[p]);
                   c++;
              }
           return c;
      }

  void sort(int c)
      {
           int i,j,temp;
          for(i=0;i<c-1;i++)
              {
                   for(j=i+1;j<c;j++)
                        {
                            if(arr[i]>arr[j])
                                {
                                      temp=arr[i];
                                      arr[i]=arr[j];
                                      arr[j]=temp;
                                }
                        }
              }
           getch();
      }

  void ser(int c)
      {
           int i,val,f,p;
           printf("\n\tEnter the value to be searched : ");
           scanf("%d",&val);
           sort(c);
           f=0;
           for(i=0;i<c;i++)
              {
                   if(arr[i]==val)
                        {
                            p=i+1;
                            f++;
                        }
              }
           if(f==0)
              {
                   printf("\n\t %d is Not Found!",val);
              }
           else
              {
                   printf("\n\t%d  is Found! it is in %d Position  ",val,p);
              }
           getch();
      }

LCM of given numbers from an array


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

//LCM of Array Elements

void main()
      {
           int Nm,i,flag=0,j,n;
           long int prod=1;
           int Num[20];
           clrscr();
           printf("\n\tEnter Size of Array : ");
           scanf("%d",&n);
           for(i=0;i<n;i++)
              {
                   printf("\n\tEnter Element at [%d] :",i);
                   scanf("%d",&Num[i]);
              }
           Nm=Num[0];
           printf("\n\tNumbers in Array :\n\t");
           for(i=0;i<n;i++)
              {
                   printf("%d ",Num[i]);
                   if(Num[i]>Nm)
                        {
                            Nm=Num[i];
                        }
                   prod=prod*Num[i];
              }
           for(i=Nm;i<=prod;i++)
              {
                   flag=0;
                   for(j=0;j<n;j++)
                        {
                            if(i%Num[j]!=0)
                                {
                                      flag=1;
                                      break;
                                }
                        }
                   if(flag==0)
                        {
                            break;
                        }
              }
           printf("\n\tLCM = %d",i);
           getch();


No comments: