Array C programming

  • Write a program to search for an element from an array input from the user.

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

    void main() 
    {
    clrscr();
    int num[5]={2,4,6,8,10},i,a;
    printf(“Enter an element:”);
    scanf(“%d”,&a);
    for(i=0;i<5;i++)
    {
    if(num[i]==a)
    {
    printf(“\nFound”);
    break;
    }
    }
    if(i==5)
    printf(“\nNot Found”);
    getch();
    }

  • Inserting an element into a position of an array.The element and the insertion point are inputs form the user.

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

    void main() 
    {
    clrscr();
    int a[6]={10,2,8,5,9},p,e,i,j;
    printf(“Enter the inserting point?\n”);
    scanf(“%d”,&p);
    printf(“Enter the element?\n”);
    scanf(“%d”,&e);

    for(i=5;i>=p;i–) 
    {
    j=i-1;
    a[i]=a[j];
    }
    a[p]=e;

    printf(“The array is:\n”); 
    for(i=0;i<6;i++)
    printf(” %d,”,a[i]);

    getch(); 
    }

  • Inserting a number /character into the proper position of a array which is sorted in ascending /descending order.
    Ascending
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int a[5]={1,2,3,4,5},i,j,temp;
    for(i=0;i<5;i++)

    printf(“%d”,a[i]);

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

    if(a[i]<a[j])
    {    temp=a[i];
    a[i]=a[j];
    a[j]=temp;
    }
    for(i=0;i<5;i++)
    printf(“\n%d”,a[i]);

    getch();
    }

    Descending

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int a[5]={1,2,3,4,5},i,j,temp;
    for(i=0;i<5;i++)

    printf(“%d”,a[i]);

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

    if(a[i]>a[j])
    {    temp=a[i];
    a[i]=a[j];
    a[j]=temp;
    }

    for(i=0;i<5;i++)
    printf(“\n%d”,a[i]);

    getch();
    }

  • Deleting an element from an array.

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int a[7]={2,4,8,10,12,15},i,j;
    printf(“Deleting 8 from this array\n\n”);
    for(i=2;i<6;i++)
    {
    j=i+1;
    a[i]=a[j];
    }

    printf(“The array is “);
    for(i=0;i<5;i++)
    printf(“%d,”,a[i]);
    getch();
    }

  • Write a program to find out the maximum, minimum and mode of an array of numbers.

    Maximum & Minimum

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

    void printarray(int ar[]);

    void main()
    {

    clrscr();

    int num[10]={23,33,6,4,54,28,24,6,34,2};

    int i,k,temp,a;

    printarray(num);

    a=10/2;
    a=(num[a]+num[a-1])/2;
    printf(“\nMEDIAN of this array is %d\n”,a);

    for(i=0;i<9;i++)
    for(k=i+1;k<10;k++)
    {
    if(num[i]<num[k])
    {
    temp=num[i];
    num[i]=num[k];
    num[k]=temp;
    }

    }

    printf(“\nMaximum number is %d\n”,num[0]);
    printf(“\nMinimum number is %d”,num[9]);

    getch();
    }

    void printarray(int ar[])
    {

    int i;
    for(i=0;i<10;i++)
    printf(“%d “,ar[i]);

    printf(“\n\n”);

    }

    Mode
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int a[6]={2,2,3,5,5,6},i,j;
    printf(“Mode of this array is\n”);
    for(i=0;i<6;i++)
    for(j=i+1;j<6;j++)
    {
    if(a[i]==a[j])
    {
    printf(” %d,”,a[i]);

    }
    }

    getch();
    }

  • Find k-th maximum and k-th minimum from an array.

    #include<stdio.h>
    #include<conio.h>
    void printarray(int ar[]);
    void main()
    {

    clrscr();

    int num[10]={23,10,3,4,54,28,30,6,34,2};

    int i,k,n,l,temp;

    printarray(num);

    for(i=0;i<9;i++)
    for(k=i+1;k<10;k++)
    {
    if(num[i]<num[k])
    {
    temp=num[i];
    num[i]=num[k];
    num[k]=temp;
    }

    }
    printarray(num);

    printf(“Enter the value of K?\n”);
    scanf(“%d”,&n);
    l=n-1;
    printf(“\n%dth Maximum number is %d\n”,n,num[l]);

    printf(“\n\n”);

    for(i=0;i<9;i++)
    for(k=i+1;k<10;k++)
    {
    if(num[i]>num[k])
    {
    temp=num[i];
    num[i]=num[k];
    num[k]=temp;
    }

    }
    printarray(num);

    printf(“\n%dth Minimum number is %d”,n,num[l]);

    getch();
    }

    void printarray(int ar[])
    {

    int i;
    for(i=0;i<10;i++)
    printf(“%d “,ar[i]);

    printf(“\n\n”);
    }

  • Write  a program to delete duplicate elements from an array.

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

    void main()
    {
    clrscr();
    int a[7]={2,4,6,6,8,8,10},j,i,k,l;

    printf(“\nOriginal array is\n “);
    for(i=0;i<7;i++)
    printf(” %d,”,a[i]);

    for(i=0;i<7;i++)
    for(j=i+1;j<7;j++)
    {
    if(a[i]==a[j])
    {
    a[i]=NULL;
    for(k=i;k<7;k++)
    {
    l=k+1;
    a[k]=a[l];
    }
    }
    }
    printf(“\nAfter deleting duplicate element,the array is\n “);
    for(i=0;i<5;i++)
    printf(” %d,”,a[i]);
    getch();
    }

  • Write a program to find the common characters from two arrays.

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

    void main()
    {
    clrscr();
    char str1[10],str2[10];
    int i,len,j,len1;

    gets(str1);
    gets(str2);

    len=strlen(str1);
    len1=strlen(str2);

    for(i=0;i<len;i++)
    for(j=0;j<len1;j++)
    {
    if(str1[i]==str2[j])
    {
    printf(“%c”,str1[i]);
    break;
    }
    }
    getch();
    }

  • Take a string as input and print characters in reverse order.Don’t use any built in string function.

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

    void main()
    {
    clrscr();
    int i, k , len;

    char str[100];
    char strrv[100];

    gets(str);

    len=strlen(str);

    k=0;

    for(i=len-1;i>=0;i–)
    {
    strrv[k]=str[i];
    k++;
    }

    strrv[len]=NULL;

    printf(“Original String :%s\n”,str);
    printf(“Rerverse string :%s\n”,strrv);

    getch();

    }

  • Write a program to merge two arrays removing the duplicate elements.

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

    void main()
    {
    clrscr();
    int arr1[7]={2,4,8,9},arr2[4]={4,8,10,12},i,j,l,k,a;
    for(i=0;i<4;i++)
    for(j=0;j<4;j++)
    {
    if(arr1[i]==arr2[j])
    {
    arr2[j]=NULL;
    for(k=j;k<3;k++)
    {
    l=k+1;
    arr2[k]=arr2[l];
    }
    break;
    }
    }

    a=4;
    for(i=0;i<2;i++)
    {
    arr1[a]=arr2[i];
    a=a+1;
    }
    for(i=0;i<6;i++)
    printf(” %d,”,arr1[i]);

    getch();
    }

  • Take a string as input and check whether it is  a palindrome.

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

    void main()
    {
    clrscr();
    int i,k,len;

    char arr1[100];
    char arr2[100];

    gets(arr1);

    len=strlen(arr1);

    k=0;

    for(i=len-1;i>=0;i–)
    {
    arr2[k]=arr1[i];
    k++;
    }

    arr2[len]=NULL;

    if(!strcmp(arr1,arr2))
    printf(“\n PALINDROME”);
    else
    printf(“\n NOT PALINDROME”);

    getch();

    }

  • Write  a program to merge two sorted arrays.


    #include<stdio.h>
    #include<conio.h>
    void printary(int arr[]);
    void main()
    {
    clrscr();
    int a[5]={2,5,1,7,9},b[5]={3,6,8,4,10},i,num,c,j;

    printary(a);

    printary(b);
    c=5;
    for(i=0;i<5;i++)

    {
    a[c]=b[i];
    c=c+1;
    }
    printf(“After merge the main array is:”);
    for(i=0;i<10;i++)

    printf(” %d”,a[i]);
    getch();
    }
    void printary(int arr[])
    {
    int j,i,a,temp;
    for(i=0;i<5;i++)
    for(i=0;i<5-1;i++)
    for(j=i+1;j<5;j++)

    if(arr[i]>arr[j])
    {    temp=arr[i];
    arr[i]=arr[j];
    arr[j]=temp;
    }
    for(i=0;i<5;i++)
    printf(” %d”,arr[i]);

    print f(“\n\n”);
    }

  • Write a program to count the frequencies of each character present in a text.

    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    void sort(char a[]);

    int len,k;
    void main()
    {
    clrscr();
    char a[100];
    int i,j,count=0,count1=0,count2=0;

    gets(a);

    printf(“\n”);
    len=strlen(a);
    for(i=0;i<len;i++)
    if(a[i]==’ ‘)
    {
    count++;
    k=i;
    sort(a);
    i=i-1;
    }
    if(count>0)
    printf(“space= %d\n\n”,count);

    count=0;
    for(i=0;i<len;i++)
    if(a[i]==’.’)
    {
    count++;
    k=i;
    sort(a);
    i=i-1;
    }

    if(count>0)
    printf(“.= %d\n\n”,count);

    for(i=0;i<len;i++)
    if(a[i]==’    ‘)
    {
    count2++;
    k=i;
    sort(a);
    i=i-1;
    }

    if(count2>0)
    printf(“tab= %d\n\n”,count2);

    for(i=0;i<len;i++)
    if(a[i]==’,’)
    {
    count1++;
    k=i;
    sort(a);
    i=i-1;
    }

    if(count1>0)
    printf(“,= %d\n\n”,count1);

    for(i=0;i<len;i++)
    {
    count=1;
    for(j=i+1;j<len;j++)
    if(a[i]==a[j])
    {
    count++;
    k=j;
    sort(a);
    len=strlen(a);
    j=j-1;
    }

    if(count>1)
    printf(“%c= %d\n”,a[i],count);

    }

    getch();
    }
    void sort(char a[])
    {
    int i,j;
    for(i=k;i<len;i++)
    {
    j=i+1;
    a[i]=a[j];
    }
    len=strlen(a);
    }

  • Write a program to count the number of letters and words within a text.

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

    void main()
    {
    clrscr();
    char str[100];
    int i,len,count=0,s;
    gets(str);
    len=strlen(str);
    for(i=len-1;i>=0;i–)
    if(str[i]==’ ‘)
    count++;
    s=count+1;
    printf(“The number of words is:%d”,s);
    printf(“\nThe number of letters is:%d”,len-count);
    getch();
    }

  • Write a program which will search for a substring withing a string.

    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    void main()
    {
    clrscr();
    char str[100],substr[100];
    int i,k,len,len1,count=0;
    gets(str);
    printf(“\n”);
    gets(substr);
    len=strlen(str);
    len1=strlen(substr);
    for(i=0;i<len;i++)
    for(k=0;k<len1;k++)
    {
    if(str[i]==substr[k])
    count++;
    }
    printf(“\n”);
    if(count==len1)
    printf(“Found”);
    else
    printf(“Not Found”);

    getch();
    }

  • Take a  numbers as input from the user .Find out their GCD and LCM.
    GCD

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int a[100],i,rem,n,k,j;
    printf(“How many number of GCD:”);
    scanf(“%d”,&n);
    printf(“Print %d numbers:\n”,n);
    for(k=0;k<n;k++)
    scanf(“%d”,&a[k]);
    {
    for(i=0;i<n;i++)

    while(a[i+1]>0)
    {
    rem=a[0]%a[i+1];
    a[0]=a[i+1];
    a[i+1]=rem;
    }

    }
    printf(“The GCD is:%d”,a[0]);
    getch();
    }

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

    void main()
    {
    clrscr();
    int arr[100],a,b,n,i,rem;
    printf(“How many number of LCM:”);
    scanf(“%d”,&n);
    printf(“Enter %d numbers:\n”,n);
    for(i=0;i<n;i++)
    scanf(“%d”,&arr[i]);
    for(i=0;i<n-1;i++)
    {
    a=arr[i];
    b=arr[i+1];
    while(b>0)
    {
    rem=a%b;
    a=b;
    b=rem;
    }
    arr[i+1]=a*arr[i]/a*arr[i+1]/a;
    }
    printf(“The LCM is :”);
    printf(“%d”,arr[n-1]);
    getch();
    }

  • Write a program to Add/Subtract two different matrices, input from the user.

    ADD

    #include<stdio.h>
    #include<conio.h>
    void mat(int arr[3][3]);
    void main()
    {
    clrscr();
    int m1[3][3],m2[3][3],m3[3][3],i,j;
    printf(“Enter Matrix A:\n”);
    mat(m1);
    printf(“Enter Matrix B:\n”);
    mat(m2);
    {
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)

    m3[i][j]=m1[i][j]+m2[i][j];
    }
    }
    printf(“After adding two matrix(A+B):\n\n”);

    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)

    printf(”  %d”,m3[i][j]);
    printf(” \n”);
    }
    getch();
    }
    void mat(int arr[3][3])
    {
    int r,c,a;
    for(r=0;r<3;r++)
    {
    for(c=0;c<3;c++)
    scanf(“%d”,&arr[r][c]);
    }
    for(r=0;r<3;r++)
    {
    for(c=0;c<3;c++)
    printf(”  %d”,arr[r][c]);
    printf(“\n\n”);
    }
    }


    Subtract

    #include<stdio.h>
    #include<conio.h>
    void mat(int arr[3][3]);
    void main()
    {
    clrscr();
    int m1[3][3],m2[3][3],m3[3][3],i,j;
    printf(“Enter Matrix A:\n”);
    mat(m1);
    printf(“Enter Matrix B:\n”);
    mat(m2);
    {
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)

    m3[i][j]=m1[i][j]-m2[i][j];
    }
    }
    printf(“After subtracting two matrix(A-B):\n\n”);

    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)

    printf(”  %d”,m3[i][j]);
    printf(” \n”);
    }
    getch();
    }
    void mat(int arr[3][3])
    {
    int r,c,a;
    for(r=0;r<3;r++)
    {
    for(c=0;c<3;c++)
    scanf(“%d”,&arr[r][c]);
    }
    for(r=0;r<3;r++)
    {
    for(c=0;c<3;c++)
    printf(”  %d”,arr[r][c]);
    printf(“\n\n”);
    }
    }

  • Write a program to Multiply two matrices.

    #include<stdio.h>
    #include<conio.h>
    void mat(int arr[3][3]);
    void main()
    {
    clrscr();
    int m1[3][3],m2[3][3],m3[3][3],i,j,n,p;
    printf(“Enter Matrix A:\n”);
    mat(m1);
    printf(“Enter Matrix B:\n”);
    mat(m2);
    printf(“After multiplying two matrix(A*B):\n\n”);

    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    {
    n=0;
    for(p=0;p<3;p++)
    n=n+m1[i][p]*m2[p][j];
    m3[i][j]=n;

    printf(”  %d”,m3[i][j]);
    }
    printf(” \n”);
    }
    getch();
    }
    void mat(int arr[3][3])
    {
    int r,c,a;
    for(r=0;r<3;r++)
    {
    for(c=0;c<3;c++)
    scanf(“%d”,&arr[r][c]);
    }
    for(r=0;r<3;r++)
    {
    for(c=0;c<3;c++)
    printf(”  %d”,arr[r][c]);
    printf(“\n\n”);
    }
    }

  • Write a program to find out the Transpose of a Matrix.


    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int m[3][3],i,j;
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    scanf(“%d”,&m[i][j]);
    }

    printf(“The original matrix is:\n”);

    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    printf(”  %d”,m[i][j]);
    printf(” \n\n”);
    }
    printf(“After transpose, the matrix is:\n”);

    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    printf(”  %d”,m[j][i]);
    printf(” \n\n”);
    }

    getch();
    }

  • Write a program to construct a  nxn magic square.

    #include<stdio.h>
    #include<conio.h>
    int matrix[100][100];
    void main()
    {
    clrscr();
    int r,c,start,num,n;
    printf(“Please Enter the number of order:”);
    scanf(“%d”,&n);
    start=n/2;
    num=2;
    matrix[0][start]=1;
    r=0;
    c=start;
    while(1)
    {
    r=r-1;
    c=c+1;
    if(r<0&&c<n)
    {
    r=n-1;
    matrix[r][c]=num;
    }
    else if(r>=0&&r<n&&c<n&&matrix[r][c]==0)
    {
    matrix[r][c]=num;
    }
    else if(r>=0&&r<n&&c>=n)
    {
    c=0;
    matrix[r][c]=num;
    }
    else if(r<0&&c>=n)
    {
    r=r+2;
    c=c-1;
    matrix[r][c]=num;
    }
    else if(r>=0&&r<n&&c>=0&&c<n&&matrix[r][c]!=0)
    {
    r=r+2;
    c=c-1;
    matrix[r][c]=num;
    }
    if(n*n==num)
    break;
    num++;
    }
    for(r=0;r<n;++r)
    {
    for(c=0;c<n;++c)
    {
    printf(“%d  “,matrix[r][c]);
    }
    printf(“\n\n”);
    }
    getch();
    }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s