Write a program that reads the dimension N of an integer array T (maximum dimension: 50 components), fills the array with values entered on the keyboard and displays the array.

Then re-arrange the values as follows: Odd integers first, then even integers. Displays the array again.

Do not use any auxiliary arrays.


Difficulty level
This exercise is mostly suitable for students
#include<stdio.h>
#include<math.h>
#include<conio.h>
#define SIZE 50
void main()
{
	int N; // nb of elements
	int T[SIZE]; // array of maximum size 50
	int i, j, k, temp;
	do {
		printf("Enter N: ");
		scanf("%d", &N);
	} while (N<0 || N>SIZE);

	for (i = 0; i < N; i++)
	{
		printf("Enter T[%d]: ", i);
		scanf("%d", &T[i]);
	}

	printf("\n\nPrinting the elements\n");
	for (i = 0; i < N; i++)
	{
		printf("T[%d]=%d\n", i, T[i]);
	}

	for(i=0; i<N-1 ; i++)
	{
		if(T[i]%2==0)
		{
			for(j=i+1; j<N; j++)
				if(T[j]%2==1)
					break;
			if(j==N) // no odd number remaining
				break;
			else
			{
				temp=T[i];
				T[i]=T[j];
				//shift elements 1 position forward from i+1 to j-1
				for(k=j; k>i+1;k--)
					T[k]=T[k-1];
				T[i+1]=temp;
			}		
		}
	}

	printf("\n\nPrinting the elements\n");
	for (i = 0; i < N; i++)
	{
		printf("T[%d]=%d\n", i, T[i]);
	}

	getch();
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
UVA 10009 - All Roads Lead Where