pete
7/24/2011 12:49:00 PM
HumbleWorker wrote:
>
> Write a C program to create an array of 10 integer elements.
The type of the integer elements was not specified,
so I'm going to make it (unsigned).
> Receive 5
> even numbers and 5 odd numbers from the user as input at the keyboard.
> While receiving input from the user, following point is to be kept in
> mind - If the user enters more than 5 EVEN or ODD numbers in the
> array, discard that input, display a warning message on the console
> and ask for a fresh input from the user. In other words, maximum limit
> for entering EVEN and ODD numbers in the array is 5. Store all the ODD
> numbers in the FIRST HALF locations of the array and store EVEN
> numbers in the second half locations of the array. Finally, display
> the contents of the array.
/* BEGIN new.c */
#include <stdio.h>
#define NMEMB 10
enum {Start = 0, Odd_full, Even_full};
void bisort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
int comp(const void *a, const void *b);
int
main(void)
{
int rc;
unsigned integer;
unsigned array[NMEMB];
size_t n;
size_t even = 0;
size_t odd = 0;
int status = Start;
const size_t even_max = sizeof array / sizeof *array / 2;
const size_t odd_max = sizeof array / sizeof *array - even_max;
puts("\n/* BEGIN new.c output */\n");
for (n = 0; n != sizeof array / sizeof *array; ++n) {
switch (status) {
case Start:
puts("Enter an integer.");
break;
case Odd_full:
puts("Enter an even integer.");
break;
default:
puts("Enter an odd integer.");
break;
}
rc = scanf("%u", &integer);
if (rc != 1) {
puts("\nThat's not an integer. Try again.\n");
--n;
while (!feof(stdin)) {
if (getc(stdin) == '\n') {
break;
}
}
continue;
}
if (integer % 2 == 0) {
if (status == Even_full) {
puts("\nThat's not an odd integer. Try again.\n");
--n;
continue;
}
if (++even == even_max) {
status = Even_full;
}
} else {
if (status == Odd_full) {
puts("\nThat's not an even integer. Try again.\n");
--n;
continue;
}
if (++odd == odd_max) {
status = Odd_full;
}
}
array[n] = integer;
}
bisort(array,
sizeof array / sizeof *array,
sizeof *array,
comp);
puts("\n\nThe contents of the array:");
for (n = 0; n != sizeof array / sizeof *array; ++n) {
printf("%u\n", array[n]);
}
puts("\n/* END new.c output */");
return 0;
}
int
comp(const void *a, const void *b)
{
const unsigned *ua = a;
const unsigned *ub = b;
return (*ua & 1) > (*ub & 1) ? -1 : (*ua & 1) != (*ub & 1);
}
void
bisort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
if (nmemb-- > 1) {
size_t low, middle, high, n;
size_t bytes = 0;
const size_t odd_size = size ^ size - 1;
unsigned char *const array = base;
unsigned char *key = array;
do {
low = 0;
key += size;
bytes += size;
high = bytes;
do {
n = high - low;
middle = ((n & odd_size ? n - size : n) >> 1) + low;
if (0 > compar(key, middle + array)) {
high = middle;
} else {
low = middle;
}
} while (n != size);
base = array + high;
if (base != key) {
unsigned char *end = key;
unsigned char *after = end + size;
do {
const unsigned char swap = *--end;
*end = *--after;
*after = swap;
} while (end != base);
}
} while (--nmemb != 0);
}
}
/* END new.c */
--
pete