Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Sun, 24 Jul 2011 07:49:29 -0500 Message-ID: <4E2C14D8.4C3@mindspring.com> Date: Sun, 24 Jul 2011 08:49:28 -0400 From: pete Reply-To: pfiland@mindspring.com Organization: PF X-Mailer: Mozilla 3.04Gold (WinNT; I) MIME-Version: 1.0 Newsgroups: comp.lang.c Subject: Re: Solution ? References: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 155 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.156.228.187 X-Trace: sv3-OQSolFbdxO3y4EGlLORNSXqRs5urUiCHXeDqQFB8rgYGcQmAYguUGYKJQth1gN5S+xrfDx/IY1aHMVJ!SaJ5AnoUJiTSBHOZc8Xr2scPVsibj5GXLm4CU6g8o0wsKawFLJPlH83prV1rgBEj0Lv9XlKJSWx6!GMaMWHQhaQ== X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 5511 Xref: x330-a1.tempe.blueboxinc.net comp.lang.c:8662 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 #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