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();


Thursday, October 20, 2011

Arrays [I] [Introduction & Small Programs]



Array - We already know that a variable cannot store more than one data at a time. An array is a group of same typed variables that are referenced by a common name. Arrays can be any typed and one or more dimensions. A particular element can be addressed by array name and index of the array enclosed in third bracket ([]). Array can be declared with its initial values or can be defined with the size and can input the values latter. An array index start from 0 by default.
int a[4]={1,2,3,4};
int a[10];

What are Arrays?
To store similar types of data under a common variable name, instead of declaring N number of variables for the N number of data, we use an array. An array is a collection of similar type of data under a common variable. Thus we can have an Array of integers, floats, characters (which are known as strings), but remember that all elements of any given array must be of the same type, i.e. we cannot have an array of numbers some of which are int and some float etc. The different components of an array are called the Array Elements. Arrays also known as subscripted variable
How to Declare an Array:
Like all other variables, an array needs to be declared first before it can be used. The declaration consists of stating the type of data-type used in the array, the name of the array and the number of elements in the array.
Data Type Array_Variable[ number of elements ] ;
Example :
int age[20]; float salary[20]; char name[20];
In C char array indicate a String.
Suppose we declare an array if integer with 5 elements, N[5] then
N[0]

N[1]

N[2]

N[3]

N[4]

Here N is variable and [0] to [4] is subscript. Above array also known as single dimensional array. We can initialize the array with a list of values.
Initialization can be done when we use a fixed and known numbers of data but there is no shortcut method to initialize large number of elements.

Multi dimensional Arrays:
Where Single Dimensional Arrays has one column and multiple rows, multi-dimensional arrays has multiple rows as well as multiple columns. Multi-dimensional data structure also known as matrices. A multi-dimensional array with 3 rows and 3 columns is shown. There are two array subscripts. One subscript denotes the row & the other the column.
N[0][0]
N[0][1]
N[0][2]
N[1][0]
N[1][1]
N[1][2]
N[2][0]
N[2][1]
N[2][2]

Array based Programs :

Input five numbers in an array and print the sum and average.


#include <stdio.h>
#include <conio.h>
#define MAX 5

//Sum and Average

  int main()
      {
           int i,Arr[MAX],sum=0;
           float avg;
           printf("\nEnter Five No. :\n");
           for(i=0;i<MAX;i++)
              {
                   scanf("%d",&Arr[i]);
                   sum=sum+Arr[i];
              }
           avg=sum/5.0;
           printf("\n\tNos. in Array :\n");
           for(i=0;i<MAX;i++)
              {
                   printf("\n\t\t%d",Arr[i]);
              }
           printf("\n\tSum : %d and  Average : %f",sum,avg);
           getch();
  }

Input N numbers and print the Maximum and Minimum value.

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

//Maximum and Minimum

  int main()
      {
           int Arr[20],i,n,max,min;
           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]);
              }
           max=min=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]<min)
                        {
                            min=Arr[i];
                        }
              }
           printf("\n\tMaximum : %d   Minimum : %d",max,min);
           getch();
           return 0;
      }

Input 5 numbers and reverse the values in array.


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

//Reverse
  int main()
      {
           int Arr[20],i,n,k,j,tmp;
           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]);
              }
           printf("\n\tNumbers in Array : \n");
           for(i=0;i<n;i++)
              {
                   printf("\n\t\t%d",Arr[i]);
              }
           for(i=0;i<n/2;i++)
              {
                   tmp=Arr[i];
                   Arr[i]=Arr[n-i-1];
                   Arr[n-i-1]=tmp;
              }
           printf("\n\tNumbers After Reverse : \n");
           for(i=0;i<n;i++)
              {
                   printf("\n\t\t%d",Arr[i]);
              }
           getch();
           return 0;
      }

Input N numbers in an array, while prevent any duplicate entry.

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

//Prevent Duplicate Entry

  int main()
      {
           int Arr[20],i,n,j;
           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]);
                   for(j=0;j<i;j++)
                        {
                            if(Arr[i]==Arr[j])
                                {
                                      printf("\n\t\t*Duplicate Entry **\n");
                                      i--;
                                }
                        }
              }
           printf("\n\tNumbers in Array : \n");
           for(i=0;i<n;i++)
              {
                   printf("\n\t\t%d",Arr[i]);
              }
           getch();
           return 0;
      }



Print the Lucky Numbers up to N terms


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

//Lucky Numbers

int main()
      {
           int Arr[50],N,i,j,k,L;
           printf("\nEnter Size of N : ");
           scanf("%d",&N);
           printf("\nLucky Number : \n");
           if(N%2==0)
              {
                   N=N+1;
              }
           for(i=0;i<N;i++)
              {
                   Arr[i]=i+1;
              }
           L=1;
           while(L<=N+1)
              {
                   for(i=0;i<N;i++)
                        {
                           printf("%d ",Arr[i]);
                        }
                   printf("\n");
                   for(i=L;i<N;i=i+L)
                        {
                            for(j=i;j<N;j++)
                                {
                                      Arr[j]=Arr[j+1];
                                }
                            N--;
                        }
                   L++;
              }
           getch();
           return 0;
      }


Thursday, October 6, 2011

Example (String Program [II])




Find the Frequency of each word.


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

//Word Frequency

  void main()
      {
           char sent[100],word[20][20],tmp[20];
           int L,i,j,k,flag,cnt;
           char ch;
           clrscr();
           printf("Enter a Sentence : ");
           gets(sent);
           L=strlen(sent);
           printf("\nInput ; %s\n",sent);
           sent[L]=' ';
           k=0;
           for(i=0;i<L;i++)
              {
                   for(j=0;j<20;j++)
                        {
                            tmp[j]='\0';
                        }
                   j=0;
                   while(sent[i]!=' ')
                        {
                            tmp[j++]=sent[i];
                            i++;
                        }
                   tmp[j]='\0';
                   strcpy(word[k],tmp);
                   k++;
              }
     
           for(i=0;i<k;i++)
              {
                   cnt=1;
                   for(j=i+1;j<k;j++)
                        {
                            if(strcmpi(word[i],word[j])==0)
                                {
                                      cnt++;
                                }
                        }
                   flag=0;
                   for(j=i-1;j>=0;j--)
                        {
                            if(strcmpi(word[i],word[j])==0)
                                      flag=1;
                        }
                   if(flag==0)
                            printf("\nword :  %s Repeated %d times " ,word[i],cnt);
              }
           getch();
      }

Program for Case converter


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

//Case Converter

void Ucase(char a[])
      {
           int i;
           for(i=0;a[i]!='\0';i++)
              {
                   if(a[i]>=97 && a[i]<=122)
                        {
                            a[i]=a[i]-32;
                        }
              }
           printf("\n\t\tConverted to Uppercase %s\n",a);
      }
  void Lcase(char a[])
      {
           int i;
           for(i=0;a[i]!='\0';i++)
              {
                   if(a[i]>=65 && a[i]<=90)
                        {
                            a[i]=a[i]+32;
                        }
              }
           printf("\n\t\tConverted to Lowercase %s\n",a);
      }
  void Tcase(char a[])
      {
           int i;
           char b[100]=" ";
           strcat(b,a);
           strcat(b," ");
           for(i=0;b[i]!='\0';i++)
              {
                   if(b[i]==' ' && b[i+1]!=' ')
                        {
                            if(b[i+1]>=97 && b[i+1]<=122)
                                {
                                      b[i+1]=b[i+1]-32;
                                      i++;
                                }
                             else
                                {
                                      i++;
                                }
                        }
                    else if(b[i]>=65 && b[i]<=90)
                        {
                            b[i]=b[i]+32;
                        }
              }
           printf("\n\t\tConverted to Titlecase %s\n",b);
      }
  void Tgcase(char a[])
      {
           int i;
           for(i=0;a[i]!='\0';i++)
              {
                   if(a[i]>=65 && a[i]<=90)
                        {
                            a[i]+=32;
                        }
                   else if(a[i]>=97 && a[i]<=122)
                        {
                            a[i]-=32;
                        }
              }
           printf("\n\t\tToggle Case %s ",a);
      }

  void Scase(char a[])
      {
           int i,j;
           char b[100]=" ";
           strcat(b,a);
           strcat(b," ");
           for(i=0;b[i]!='\0';i++)
              {
                   if(b[i]==' ' && b[i+1]!=' ')
                        {
                            if(b[i+1]>=97 && b[i+1]<=122)
                                {
                                      b[i+1]=b[i+1]-32;
                                      i++;
                                }
                                break;
                         }

              }
           for(j=i+1;b[j]!='\0';j++)
              {
                   if(b[j]=='.' || b[j]=='?' || b[j]=='!')
                        {
                            if(b[j+1]==' ')
                                {
                                      if(b[j+2]>=97 && b[j+2]<=122)
                                                {
                                                          b[j+2]-=32;
                                                          j=j+2;
                                                }
                                      else
                                                          j=j+2;
                                }
                        }
                   else if(b[j]>=65 && b[j]<=90)
                        {
                            b[j]+=32;
                        }
              }
           printf("\n\t\tConverted to Titlecase %s\n",b);
      }

  void main()
      {
           char a[100];
           int c=0;
           while(c!=6)
              {
                   printf("\n\t1. UpperCase.\n");
                   printf("\t2. LowerCase.\n");
                   printf("\t3. Title Case.\n");
                   printf("\t4. Toggle Case.\n");
                   printf("\t5. Sentence Case.\n");
                   printf("\t6. Exit\n");
                   printf("\tEnter Your Choice : ");
                   scanf("%d",&c);
                   if(c<6)
                        {
                            fflush(stdin);
                            printf("\n\tEnter a String  : ");
                            gets(a);
                            printf("\n\t Input : %s",a);
                        }
                   switch(c)
                        {
                            case 1: Ucase(a);
                                                break;
                            case 2:  Lcase(a);
                                                break;
                            case 3: Tcase(a);
                                                break;
                            case 4: Tgcase(a);
                                                break;
                            case 5: Scase(a);
                                                break;
                            case 6: printf("\n\t  Quit .......");
                                                delay(50);
                                                exit(0);
                            default :printf("\n\t Wrong Choice.\n ");
                        }
              }
           getch();
      }


Convert Number to word


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

//Number to word
 void chk(int n)
  {
    char unit[20][20]={" ","One","Two","Three","Four","Five","Six","Seven","Eight", "Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen" , "Seventeen","Eighteen","Nineteen"};
    char tens[15][9]={"","","Twenty","Thirty","Forty","Fifty" , "Sixty", "Seventy", "Eighty", "Ninety"};
      if(n<20)
           printf("%s",unit[n]);
      else
           printf("%s %s ",tens[n/10],unit[n%10]);
  }

 void main()
  {
    long num,n,s=0,t=0;
    int r;
    int a,b,c=0;
    clrscr();
    printf("\nEnter number up to 9999999 ");
    scanf("%ld",&num);
    if(num>9999999 || num<0)
           printf("\n *  * * * Invalid Input * * * *\n");
    else if(num==0)
           printf("\nZero");
    else
      {
           printf("\n\n\t%ld  =  ",num);
           if(num>99999)
              {  n=num/100000;
                   chk(n);
                   printf(" Lakhs");
                   num=num%100000;
              }
           if(num>999)
              {  n=num/1000;
                   chk(n);
                   printf(" Thousands");
                   num=num%1000;
              }
           if(num>99)
              {  n=num/100;
                   chk(n);
                   printf(" Hundred");
                   num=num%100;
              }
           if(num>0)
              {
                   chk(num);
              }
       }
   getch();
  }

Same program can be done this way too but it is more complicated and suggest to use the above one.


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

//Number to word

void main()
  {
     long num,n,s=0,t=0;
     int r;
     char unit[20][20]={" ","One","Two","Three","four","five","six","seven","eight", "nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen " ,"seventeen","eighteen","nineteen"};
     char tens[15][9]={"","ten","twenty","thirty","forty","fifty" , "sixty", "seventy", "eighty", "ninety"};
    int a,b,c=0;
    clrscr();
    printf("\nEnter number up to 9999999 ");
    scanf("%ld",&num);
    if(num>9999999)
    printf("\n *  * * * Invalid Input * * * *\n");
    else
       {
    printf("\n\n%ld = ",num);
    n=num;
    while(n>0)
       {
            s=s*10+n%10;
            n/=10;
            c++;
       }
    if(c==7)
       {
            r=s%100;
            while(r>0)
       {
            t=t*10+r%10;
            r/=10;
        }
    if(t<20)
            printf("%s Lakhs ",unit[t]);
    else
         printf("%s %s Lakhs ",tens[t/10],unit[t%10]);
         s=s/100;
         c-=2;
      }

   if(c==6)
      {
         r=s%10;
         printf("%s Lakhs ",unit[r]);
         s=s/10;
         c--;
      }
  if(c==5)
     {
        t=0;
        r=s%100;
        while(r>0)
       {
          t=t*10+r%10;
          r/=10;
       }
        printf("%s %s thousands ",tens[t/10],unit[t%10]);
        s=s/100;
        c-=2;
     }
   if(c==4)
     {
        r=s%10;
        printf("%s thousands ",unit[r]);
        s=s/10;
        c-=1;
     }
        if(c==3)
      {
         r=s%10;
         printf("%s Hundred ",unit[r]);
         s=s/10;
         c-=1;
      }
        t=0;
        while(s>0)
      {
         t=t*10+s%10;
         s=s/10;
      }
        s=t;
        if(s<=20)
         printf("%s ",unit[s]);
        else
         printf("%s %s ",tens[s/10],unit[s%10]);
       }
    getch();
  }