Search This Blog

Showing posts with label C programming. Show all posts
Showing posts with label C programming. Show all posts

Array Rearrangement Without Another Array - Zoho Programming Round Question


Question:

You are given two integer arrays. The first one is a set of integers which you should rearrange and produce an output array based on the second array. The second array, we call it Order array, is an array which directs the rearrangement by specifying the required position of each element in given array. You should rearrange the elements just as specified by the order array without using any additional arrays. You should write a c program to rearrange the number array but without using another array. An example is as follows:

number array: 46   80  10   8    7    2   11   16  17  43
order array   :  5     2    0   6    8    7    1     3    4   9
output          :  2    10  46  11  17  16   80    8   7    43


Problem Explanation:

order[0]=5 means the current element at index 5 in the given number array should be at index 0 in the desired array. Similarly order[1]=2 means  the current element at index 2 in the given number array (10) should come at index 1 in the desired array.

Thus, the question is to write a c program to rearrange the given array without using another array to produce the desired array as specified by the order array.

This was one of the questions asked in a programming test conducted as a part of recruitment process of Zoho corporation, an IT firm. This is one of the tough c programming questions asked in second hands on coding round.

Before answering the question, you must know that it is possible to produce the desired output array without actually performing the rearrangement. It is not so difficult.The following code fragment can do it.

for(i=0;i<10;i++)
printf("%d  ",nums[order[i]]);

As emphasized in the question, the array should be rearranged, but your are not allowed to use any additional arrays.

Answer:

Actually, this can't be done without additional allocation of memory. But, to answer this question, we should avoid explicit use of additional arrays. So, we can make use of recursion. In fact, for each call of a recursive function, memory is allocated in stack for its local variables. We make use of recursion to exploit the stack. I would first write the program, then explain it.

#include<stdio.h>
int nums[]={ 46,80,10,8,7,2,11,16,17,43};
int order[]={5,2,0,6,8,7,1,3,4,9};
int i;
void rec(int index)
{
int num;
num=nums[order[index]];
if(index<9)
rec(index+1);
nums[index]=num;
}
void main()
{
printf("rearranging\n");
i=0;
rec(i);
printf("rearranged:\n");
for(i=0;i<10;i++)
printf("%d  ",nums[i]);
}


The parameter index in each recursion stores the index (position) and local variable num stores the desired number at that position. Thus value of parameter 'index' varies from 0 to 9 (here) and corresponding number at the index is also held in variable num. Thus, after the last call of recursive function, the whole array (index and the number at that index) get stored in stack. Only after this, the num is placed at its index.

C Program to Swap Numbers

The following are C and C++ programs to swap the values of two variables using a third variable.

C Program:
#include<stdio.h>
void main()
{
int x,y,temp;
printf("Enter the value of xand y\n");
scanf("%d%d", &x, &y);
printf("Before Swapping\n x= %d\n y = %d\n",x,y);
temp = x;
x= y;
y = temp;
printf("After Swapping\n x= %d\ny= %d\n",x,y);
}


C Programming Previous Year Question Paper for Automobile, Mechanical, Production or Metallurgy

Here i have uploaded the previous year (2014 November) question paper of Programming in C for Third semester for Automobile Engineering, Mechanical Engineering, Production Engineering or Metallurgy branches under MG university BTech course.

Course : B.Tech Engineering (degree)
University: MG university (Mahatma Gandhi university) Kottayam, Kerala
Department or branch: Automobile Engineering, Mechanical Engineering, Production Engineering or Metallurgy

Semester: Third Semester (3rd or s3)
Subject: Programming in C (CP or PSCP)

You may view online or download the question paper as pdf file from following links.

View Online in Google Docs

Download question paper in PDF format



Related Posts:

C Program to Display Prime Numbers Less than Given Number
C Program to Display Armstrong Numbers
C Program to Check Armstrong Number
C Program to Reverse a Number
C Program for Quick Sort
C Program to Search an element in array (Linear Search)
Leap Year C Program
C Program to Find Sum of Digits of a Number
C Program to Check Perfect Number
C Program to Find Transpose of Matrix
C Program to Understand Bitwise Operators

C Program For Insertion And Deletion In Heap

C program code for Insertion and Deletion in a Heap.

#include <stdio.h>
#include<stdlib.h>
int arr[100],n;
void display();
void insert(int num,int loc);
void del(int num);

void main()
{
int choice,num;
n=0; //number of nodes in the heap
while(1) //loop for menu
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the number to be inserted : ");
scanf("%d",&num);
insert(num,n);
n=n+1;
break;
case 2:
printf("Enter the number to be deleted : ");
scanf("%d",&num);
del(num);
break;
case 3:
display();
break;
case 4:
exit(0);
default: printf("Wrong choice\n");
}
}
}

void display()
{
int i;
if(n==0)
{
printf("Heap is empty\n");
return;
}
for(i=0;i<n;i++)
printf("%d ",arr[i]);
printf("\n");
}

void insert(int num,int loc)
{
int par;
while(loc>0)
{
par=(loc-1)/2;
if(num<=arr[par])
{
arr[loc]=num;
return;
}
arr[loc]=arr[par];
loc=par;
}
arr[0]=num; //assign num to the root node
}

void del(int num)
{
int left,right,i,temp,par;
for(i=0;i<n;i++)
{
if(num==arr[i])
break;
}
if(num!=arr[i])
{
printf("%d not found in heap\n",num);
return;
}
arr[i]=arr[n-1];
n=n-1;
par=(i-1)/2; //find parent of node i
if(arr[i] > arr[par])
{
insert( arr[i],i);
return;
}
left=2*i+1; //left child of i
right=2*i+2; // right child of i
while(right < n)
{
if(arr[i]>=arr[left] && arr[i]>=arr[right])
return;
if(arr[right]<=arr[left])
{
temp=arr[i];
arr[i]=arr[left];
arr[left]=temp;
i=left;
}
else
{
temp=arr[i];
arr[i]=arr[right];
arr[right]=temp;
i=right;
}
left=2*i+1;
right=2*i+2;
}
if( left==n-1 && arr[i]<arr[left] ) /* right==n */
{
temp=arr[i];
arr[i]=arr[left];
arr[left]=temp;
}
}