Answer to Question #215980 in C for Hemambar

Question #215980
give a one dimensional array with positive integer values rearrange the array such that the odd numbers precede the even numbers
Sample input:
Numbers [10]={5,7,9,98,3,65,54,2,13,15}
Sample output:
Rearranged array={5,7,9,3,13,65,15,98,54,2}
1
Expert's answer
2021-07-11T14:38:23-0400
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#define MX 655
int main()
{
	int n;
	printf("Please eneter size array:");
	scanf("%i",&n);
	int ar[MX];
	//Input numbers
	printf("Please enter numbers:\n");
	for (int i = 0; i < n; i++)
		scanf("%i", &ar[i]);
	for (int i = 0; i < n-1; i++)
	{
		for (int j = i+1; j < n; j++)
		{
			if (ar[i] % 2 == 0 && ar[j] % 2)
			{
				int tm = ar[i];
				ar[i] = ar[j];
				ar[j] = tm;
			}
		}
	}
	//Out numbers
	for (int i = 0; i < n; i++)
		printf("%i ", ar[i]);
	//O n^2
	return 0;
}
//Input
//Numbers [10]={5,7,9,98,3,65,54,2,13,15}
//There are several ways 
//for example 
//5 7 9 3 65 13 15 2 98 54
//5 7 9 3 13 65 15 98 54 2
//.............

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS