Sunday, 14 October 2012

WAP to enter an array and Sort it.

  •  WAP to enter an array and Sort it as Following:
 input:1 2 3 4 5 6 7
output: 6 4 2 1 3 5 7

#include <stdio.h>
#include <conio.h>
void main()
{
    int a[15],b[15];
    int goleft=0,bind=0,lind=14;
    //input of array
    printf("Enter the array");
    for(int i=0;i<15;i++)
    scanf("%d",&a[i]);
    //Sorting of array(Bubble Sort)
    for(i=0;i<15;i++)
    {
        for(int j=i+1;j<15;j++)
        {
         if(a[i]>a[j])
         {
            int t=a[i];
            a[i]=a[j];
            t=a[j];
            }
         }
        }

    for(i=14;i>=0;i--)
    {
        if(goleft==0)
          {
                b[lind]=a[i];
                lind--;goleft=1;
          }
          else
          {
            b[bind]=a[i];
            bind++;goleft=0;
            }
          }
          for(i=0;i<15;i++)
          printf("%d",b[i]);
        }

WAP to Find the Sum of Right Diagonal And Sum of Left Diagonal of a 3*3 array..

  •  WAP to Find the Sum of Right Diagonal And Sum of Left Diagonal.
#include <stdio.h>
#include <conio.h>
void main()
{
    int i,j,rd=0,ld=0;
    int a[3][3];
    printf("Enter the array");
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
     scanf("%d",&a[i][j]);
     }
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    {
        if(i==j)
        rd=rd+a[i][j];
        if(i+j==2)
        ld=ld+a[i][j];
        }
        }
        printf("\n left diagianl sum is %d",rd);
        printf("Right diagianl sum is %d",ld);
        }