Descending even and ascending odd Program

Descending even and ascending odd Program

QUESTION:

For a given array, output must be a array which contains the even elements of the array in descending order and then the odd elements in
ascending order.

NOTE:

Output is a array which need to be printed not just print it in order, Only one array must be used for both input and output for memory optimization.

Eg:
1.
INPUT: 1 2 3 4 5 6 7
OUTPUT: 6 4 2 1 3 5 7

2.
INPUT: 23 443 2334 66 32 34
OUTPUT: 2334 66 34 32 23 443

 

EXPLANATION:

1. The number of elements is got as input from user in n
Eg, Enter the no of elements:7 ( n=7)

2. Then get the array elements with for loop
Eg. Enter 7 positive elements: 1 2 3 4 5 6 7

3. Count the no of even elements in the array and store it in evenCount
Eg. Here no of even elements is 3 (evenCount=3)
Hence the even elements fill upto index 2 (0 to evenCount-1)
And odd elements fill from 3 to 6 (evenCount to length-1)

4. Search from 0 to evenCount-1 , if odd no is present Search for an even
elements from evenCount to length-1, and swap the both.
Eg. From index 0 to 2 , 1 is odd , So search index 3 to 6 for an even no
4 is even , so swap 1 and 4.
After completion of shifting array: 4 2 6 1 5 3 7

5. Sort the sub-array 0 to evenCount-1 in descending order with any of the
sorting algorithms. Here selection sort is used
Eg. After sorting sub array: 6 4 2 1 5 3 7

6. Sort the sub array from evenCount to length-1 in ascending order
Eg. After sorting sub array: 6 4 2 1 3 5 7

7. Print the array.

Thus the output is an array with even numbers 2,4,6 in descending order first
and odd numbers 1,3,5,7 in ascending order following the even numbers.
And only one array is used for both input and out put

PROGRAM:

//Program to arrange the elements ars descending even first and ascending odd next
#include<stdio.h>
#include<conio.h>
void main(){
	int arr_length,i,j,temp,trav_odd,trav_even,evencount=0,arr[100];
	clrscr();
	printf("\nEnter the no of elements:");           //Get the no of elements
	scanf("%d",&arr_length);
	printf("\nEnter %d positive elements:",arr_length);       //Get the array elements
	for(i=0;i<arr_length;i++)
		scanf("%d",&arr[i]);
	for(i=0;i<arr_length;i++)
		if(arr[i]%2==0)
			evencount++;
	trav_even=0;                        //swift even elements to left and odd elements to right
	trav_odd=evencount;
	for(;trav_even<evencount;trav_even++)
		if(arr[trav_even]%2==1)
			for(;trav_odd<arr_length;trav_odd++)
				if(arr[trav_odd]%2==0){
					temp=arr[trav_even];
					arr[trav_even]=arr[trav_odd];
					arr[trav_odd]=temp;
					trav_odd++;
					break;
				}
	for(i=0;i<evencount-1;i++)             //sort even elements in descending order
		for(j=i+1;j<evencount;j++)
			if(arr[i]<arr[j]){
				temp=arr[j];
				arr[j]=arr[i];
				arr[i]=temp;
			}
	for(i=evencount;i<arr_length-1;i++)             //sort odd elements in ascending order
		for(j=i+1;j<arr_length;j++)
			if(arr[i]>arr[j]){
				temp=arr[i];
				arr[i]=arr[j];
				arr[j]=temp;
			}
	printf("\nThe array:");           //print the array
	for(i=0;i<arr_length;i++)
		printf("%d ",arr[i]);
	getch();
}
[/code ]

HariPrasath TV