[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Solution ?

amardeep.developer

7/24/2011 7:34:00 AM

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.
9 Answers

Kleuskes & Moos

7/24/2011 7:58:00 AM

0

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.

Ian Collins

7/24/2011 7:59:00 AM

0

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

pete

7/24/2011 12:49:00 PM

0

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

Fonz

7/24/2011 5:32:00 PM

0

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)

amardeep.developer

7/24/2011 5:35:00 PM

0

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]);
}
}

amardeep.developer

7/24/2011 5:38:00 PM

0

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.

Ben Bacarisse

7/24/2011 7:24:00 PM

0

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.

Seebs

7/24/2011 10:10:00 PM

0

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.seeb... <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/...(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

pete

7/27/2011 5:28:00 PM

0

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