Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #8651 > unrolled thread
| Started by | HumbleWorker <amardeep.developer@gmail.com> |
|---|---|
| First post | 2011-07-24 00:34 -0700 |
| Last post | 2011-07-24 22:09 +0000 |
| Articles | 10 — 7 participants |
Back to article view | Back to comp.lang.c
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
| From | HumbleWorker <amardeep.developer@gmail.com> |
|---|---|
| Date | 2011-07-24 00:34 -0700 |
| Subject | Solution ? |
| Message-ID | <eb2c9e14-4d82-43f6-9859-bd9961df7c52@t38g2000prj.googlegroups.com> |
Write a C program to create an array of 10 integer elements. 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.
[toc] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2011-07-24 19:58 +1200 |
| Message-ID | <9921m2FkjuU3@mid.individual.net> |
| In reply to | #8651 |
On 07/24/11 07:34 PM, HumbleWorker wrote: > Write a C program to create an array of 10 integer elements. 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. Why? -- Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | "Kleuskes & Moos" <kleuske@xs4all.nl> |
|---|---|
| Date | 2011-07-24 00:58 -0700 |
| Message-ID | <089cc17f-7475-4b56-9f55-00e68c52990e@c41g2000yqm.googlegroups.com> |
| In reply to | #8651 |
On Jul 24, 9:34 am, HumbleWorker <amardeep.develo...@gmail.com> wrote: > Write a C program to create an array of 10 integer elements. 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. The solution, of course, is to do your own homework.
[toc] | [prev] | [next] | [standalone]
| From | pete <pfiland@mindspring.com> |
|---|---|
| Date | 2011-07-24 08:49 -0400 |
| Message-ID | <4E2C14D8.4C3@mindspring.com> |
| In reply to | #8651 |
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
[toc] | [prev] | [next] | [standalone]
| From | HumbleWorker <amardeep.developer@gmail.com> |
|---|---|
| Date | 2011-07-24 10:34 -0700 |
| Message-ID | <c9a87376-9c84-478d-afc0-0dd65fa6a29c@p29g2000pre.googlegroups.com> |
| In reply to | #8662 |
On Jul 24, 5:49 pm, pete <pfil...@mindspring.com> wrote:
>/* 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 *));
Is'nt the following more straightforward ?
#include <stdio.h>
void main()
{
int i [10] = {0}, k = 0;
unsigned short iCountEven = 0, iCountOdd = 0, j = 0;
for (j = 0; j < 10; j++)
{
printf ("Enter a number: ");
if (!scanf ("%d", &k))
{
while ('\n' != getc (stdin));
j--;
continue;
}
if (k % 2 && iCountOdd != 5)
{
i [iCountOdd] = k;
iCountOdd++;
}
else if (0 == k % 2 && iCountEven != 5)
{
i [iCountEven + 5] = k;
iCountEven++;
}else
{
printf ("You cannot add more than five odd "
"and five even numbers. Retry\n");
j--;
}
}
// display
for (j = 0; j < 10; j++)
{
printf ("i [%u] = %d\n", j, i [j]);
}
}
[toc] | [prev] | [next] | [standalone]
| From | HumbleWorker <amardeep.developer@gmail.com> |
|---|---|
| Date | 2011-07-24 10:38 -0700 |
| Message-ID | <23d76507-4eaa-474e-ac5e-5c66d2033cf0@r28g2000prb.googlegroups.com> |
| In reply to | #8673 |
On Jul 24, 10:34 pm, HumbleWorker <amardeep.develo...@gmail.com> wrote: > On Jul 24, 5:49 pm, pete <pfil...@mindspring.com> wrote: > Sorry missed the int main () in the previous post.
[toc] | [prev] | [next] | [standalone]
| From | pete <pfiland@mindspring.com> |
|---|---|
| Date | 2011-07-27 13:28 -0400 |
| Message-ID | <4E304AAE.4713@mindspring.com> |
| In reply to | #8674 |
HumbleWorker wrote: > > On Jul 24, 10:34 pm, HumbleWorker <amardeep.develo...@gmail.com> > wrote: > > On Jul 24, 5:49 pm, pete <pfil...@mindspring.com> wrote: > > > Sorry missed the int main () in the previous post. I agree with Ben Bacarisse's comments in this thread. -- pete
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2011-07-24 20:23 +0100 |
| Message-ID | <0.9a610132dd5c7cff354c.20110724202353BST.87pqkzscs6.fsf@bsb.me.uk> |
| In reply to | #8673 |
HumbleWorker <amardeep.developer@gmail.com> writes:
<snip>
> printf ("Enter a number: ");
> if (!scanf ("%d", &k))
This goes wrong when scanf returns EOF. It's clearer just to write
if (scanf("%d", &k) != 1)
because you know that any return but 1 is a problem.
> {
> while ('\n' != getc (stdin));
Due to the previous error, this is fine, but when you fix it this loop
can get stuck on end-of-file.
There is a general problem with programs of this sort. You need 10 ints
but if you reach end-of-file there is a good change that you'll never
get them. It's worth considering stopping the program in situations
like this.
> j--;
> continue;
A small point, but I have grown wary of code that adjusts the control
variable in a for loop. It's very easy to miss this adjustment on
reading the code and it's easy to forget it if you add another error
condition. Such a loop is almost always more robust as a while loop.
In this case:
while (count_odd + count_even != 10) ...
> }
<snip>
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Fonz <fonz@bogus.from.address.nl> |
|---|---|
| Date | 2011-07-24 17:32 +0000 |
| Message-ID | <slrnj2olon.nq5.fonz@spectrum.skysmurf.nl> |
| In reply to | #8651 |
HumbleWorker ordered: > Write a C program to create an array of 10 integer elements. Do it yourself. Fonz (that's probably what your instructor would say, too)
[toc] | [prev] | [next] | [standalone]
| From | Seebs <usenet-nospam@seebs.net> |
|---|---|
| Date | 2011-07-24 22:09 +0000 |
| Message-ID | <slrnj2p5qp.1hsf.usenet-nospam@guild.seebs.net> |
| In reply to | #8651 |
On 2011-07-24, HumbleWorker <amardeep.developer@gmail.com> wrote: > Write a C program to create an array of 10 integer elements. I'd love to. However, while your problem spec is admirably clear, I can't help but notice you've failed to disclose terms. For a small project like this I'd normally expect payment immediately upon delivery. Do you have your own QA team or do you want me to include test development and testing in the quote? -s -- Copyright 2011, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated! I am not speaking for my employer, although they do rent some of my opinions.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web