Argusoft 2nd round programming questions with solution


Argusoft 2nd round programming questions with solution
Program 1 :- Write a program where different number if taken as input as put them in an order like the largest element will get place in middle ,the second largest will get place  in right of middle and third largest will get place in left of middle and so on for entire number of inputted items.
Sample
Input :-  12,4,3,5,6,19,15,9,8
Output :- 3,5,8,12,19,15,9,6,4

SOLUTION
#include<stdio.h>
#define max 9 // Maximum number of items
int a[max]={12,4,3,5,6,19,15,9,8}; //explicit initialization of array
int res[max]; //This array will store the final result
void main()
{
                int i,j,mid,large_left,large_right,temp;
// large_left for third largest
//large_right for second largest
//mid is for middle which if largest one
//sort
                for(i=0;i<max;i++)
                printf("%d\t",a[i]);
                for(i=0;i<max;i++)
                {
                                for(j=i+1;j<max;j++)
                                {
                                                if(a[i]>a[j])
                                                {
                                                                temp=a[i];
                                                                a[i]=a[j];
                                                                a[j]=temp;
                                                }
                                }
                }

                mid=max/2;
                large_right=mid;
                large_left=mid;
                res[mid]=a[max-1];// This will store the largest item into middle place
//Here the logic for the rest part of the program
                for(i=max-2;i>=0;)
                {
                                res[++large_right]=a[i--];
                                res[--large_left]=a[i--];
                }
// displaying the result
                printf("\n");
                for(i=0;i<max;i++)
                printf("%d\t",res[i]);
}




Comments

Popular posts from this blog

Tips 01) self introduction in an interview

Computer Science Terminology

Why Failure Is Good for Success