Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| Message-ID | <4E2C14D8.4C3@mindspring.com> (permalink) |
|---|---|
| Date | 2011-07-24 08:49 -0400 |
| From | pete <pfiland@mindspring.com> |
| Organization | PF |
| Newsgroups | comp.lang.c |
| Subject | Re: Solution ? |
| References | <eb2c9e14-4d82-43f6-9859-bd9961df7c52@t38g2000prj.googlegroups.com> |
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
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Solution ? HumbleWorker <amardeep.developer@gmail.com> - 2011-07-24 00:34 -0700
Re: Solution ? Ian Collins <ian-news@hotmail.com> - 2011-07-24 19:58 +1200
Re: Solution ? "Kleuskes & Moos" <kleuske@xs4all.nl> - 2011-07-24 00:58 -0700
Re: Solution ? pete <pfiland@mindspring.com> - 2011-07-24 08:49 -0400
Re: Solution ? HumbleWorker <amardeep.developer@gmail.com> - 2011-07-24 10:34 -0700
Re: Solution ? HumbleWorker <amardeep.developer@gmail.com> - 2011-07-24 10:38 -0700
Re: Solution ? pete <pfiland@mindspring.com> - 2011-07-27 13:28 -0400
Re: Solution ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-07-24 20:23 +0100
Re: Solution ? Fonz <fonz@bogus.from.address.nl> - 2011-07-24 17:32 +0000
Re: Solution ? Seebs <usenet-nospam@seebs.net> - 2011-07-24 22:09 +0000
csiph-web