Problem #30 on http://cs.nmu.edu/programming/c/problems.htm
Author Message
Problem #30 on http://cs.nmu.edu/programming/c/problems.htm

Hey,
I just tried those programming problems on
http://www.*-*-*.com/
but I'm stuck on Problem #30, which says:

30.    Input ten numbers into an array, and print the numbers in reverse
order.

Example Input
Enter the numbers: 5 4 6 7 22 19 12 15 2 1
1 2 15 12 19 22 7 6 4 5

Printing them in reverse order is no problem for me, but how can i enter all
10 numbers in one line? should this line of code look like:

scanf("%i %i %i %i %i %i %i %i %i %i", array[0], array[1], etc.);

or is there an easier way?

Alex E.

Thu, 10 Mar 2005 17:16:22 GMT
Problem #30 on http://cs.nmu.edu/programming/c/problems.htm

Quote:

> Hey,
> I just tried those programming problems on
> http://cs.nmu.edu/programming/c/problems.htm
> but I'm stuck on Problem #30, which says:

> 30.    Input ten numbers into an array, and print the numbers in reverse
> order.

> Example Input
> Enter the numbers: 5 4 6 7 22 19 12 15 2 1
> 1 2 15 12 19 22 7 6 4 5

> Printing them in reverse order is no problem for me, but how can i enter all
> 10 numbers in one line? should this line of code look like:

> scanf("%i %i %i %i %i %i %i %i %i %i", array[0], array[1], etc.);

> or is there an easier way?

> Alex E.

int i, array[10];
for (i=0; i < 10; i++ ) {
scanf(" %i", array + i);

- Show quoted text -

Quote:
}

Thu, 10 Mar 2005 17:55:52 GMT
Problem #30 on http://cs.nmu.edu/programming/c/problems.htm
hmm, strange..
why do i only see your reply, but not my original post??

ok, can i also enter all 10 numbers in one line with this code, or do i have
to press enter after each number?

Thu, 10 Mar 2005 18:20:07 GMT
Problem #30 on http://cs.nmu.edu/programming/c/problems.htm
Try it and see! Then see if you can explain why it works the way it does.

Quote:
> hmm, strange..
> why do i only see your reply, but not my original post??

> ok, can i also enter all 10 numbers in one line with this code, or do i
have
> to press enter after each number?

Fri, 11 Mar 2005 01:28:15 GMT
Problem #30 on http://cs.nmu.edu/programming/c/problems.htm

Quote:

> hmm, strange..
> why do i only see your reply, but not my original post??

> ok, can i also enter all 10 numbers in one line with this code, or do i have
> to press enter after each number?

Notwithstanding Engel, and assuming canonical C proggie named foo, ..

\$ foo 5 4 3 2 1

will present foo's arguments as character strings.

#include <stdio.h>
int main(int argc, char *argv[]) {
while (argc-- > 1)
printf("%s ", argv[argc]);
return 0;

Quote:
}

--

"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---

Fri, 11 Mar 2005 01:51:26 GMT
Problem #30 on http://cs.nmu.edu/programming/c/problems.htm
On Sun, 22 Sep 2002 12:20:07 +0200, "Alex Engel"

Quote:

>hmm, strange..
>why do i only see your reply, but not my original post??

I'll make a guess. I'd say it's one of two things - either a hiccup in
the system has caused it to be delayed and it will show up eventually,
or the URL in the subject made some spam filtering software tag it as
spam and delete it on your server. It shows up on mine. You could
the problem, maybe suggesting that they use better spam filtering
software (if that was the cause), but it's probably not worth it.

Quote:

>ok, can i also enter all 10 numbers in one line with this code, or do i have
>to press enter after each number?

I'm going to agree with the other reply. You should try it and see. Or
even better, read up on scanf and decide for yourself what should
happen. While "try and see" can be useful, it can also be misleading -
in the case of undefined or implementation defined behavior, you may
be lead to believe that the result you see is the "correct" result.

For example, if you were to "try and see" what this does:

int i = 4;
i = i++;

You would probably find that you got different answers on different
implementations. There is no single "right" answer in this case.
Technically any result at all is correct (including your computer
sprouting legs and walking out in disgust).

-Kevin

Fri, 11 Mar 2005 04:14:06 GMT
Problem #30 on http://cs.nmu.edu/programming/c/problems.htm

Quote:

> > ok, can i also enter all 10 numbers in one line with this code, or do i have
> > to press enter after each number?

> Notwithstanding Engel, and assuming canonical C proggie named foo, ..

> \$ foo 5 4 3 2 1

> will present foo's arguments as character strings.

> #include <stdio.h>
> int main(int argc, char *argv[]) {
>   while (argc-- > 1)
>     printf("%s ", argv[argc]);
>   return 0;
> }
> --
> Joe Wright

Cool, but I like the tail-recursive version better

#include <stdio.h>

int main (int argc, char *argv[])
{
printf ("%s ", argv[--argc]);
return argc > 1 ? main (argc, argv) : argc;

- Show quoted text -

Quote:
}

Fri, 11 Mar 2005 20:29:32 GMT
Problem #30 on http://cs.nmu.edu/programming/c/problems.htm

Quote:

>>>ok, can i also enter all 10 numbers in one line with this code, or do i have
>>>to press enter after each number?

>>Notwithstanding Engel, and assuming canonical C proggie named foo, ..

>>\$ foo 5 4 3 2 1

>>will present foo's arguments as character strings.

>>#include <stdio.h>
>>int main(int argc, char *argv[]) {
>>  while (argc-- > 1)
>>    printf("%s ", argv[argc]);
>>  return 0;
>>}
>>--
>>Joe Wright

> Cool, but I like the tail-recursive version better

> #include <stdio.h>

> int main (int argc, char *argv[])
> {
>     printf ("%s ", argv[--argc]);
>     return argc > 1 ? main (argc, argv) : argc;
> }

Hate to bring it up, but it is possible for argc to be 0, in which case both
the above examples would result in undefined behavior.

-nrk.

Sat, 12 Mar 2005 14:56:07 GMT
Problem #30 on http://cs.nmu.edu/programming/c/problems.htm

Quote:
> Try it and see! Then see if you can explain why it works the way it does.

________________________________________________________________________________

printf and scanf are not part of standard C. They have been given for
the conveniance of us programmers. So the answer to whether you can
enter all the numbers in one line is YES ( In most C compilers ).  All
you have to do is separate each number by a space.
(5_4_6_7_22_19_12_15_2_1) replace the '_' with blanks.
Or you can press Return after entering each number as you mentioned

What actually happens is, the scanf funtion assumes a SPACE,TAB or

Sat, 12 Mar 2005 19:40:02 GMT
Problem #30 on http://cs.nmu.edu/programming/c/problems.htm

> > Try it and see! Then see if you can explain why it works the way it does.
>
> printf and scanf are not part of standard C.

They are part of standard C.  They are described in the standard.
--
dik t. winter, cwi, kruislaan 413, 1098 sj  amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn  amsterdam, nederland; http://www.cwi.nl/~dik/

Sat, 12 Mar 2005 19:57:52 GMT
Problem #30 on http://cs.nmu.edu/programming/c/problems.htm

Quote:
>printf and scanf are not part of standard C.

are you on crack?  i hope that `not' creeped in whilst you were looking
elsewhere.

--
bringing you boring signatures for 17 years

Mon, 14 Mar 2005 01:18:05 GMT
Problem #30 on http://cs.nmu.edu/programming/c/problems.htm

Quote:
> > Try it and see! Then see if you can explain why it works the way it
does.

____________________________________________________________________________
____

Quote:

> printf and scanf are not part of standard C. They have been given for
> the conveniance of us programmers.

Ask your money back.  And refrain from posting here until you learn C.  They
ARE part of the standard and are FULLY standardized.

--

ICQ#: 26070936
More ways to contact me:
http://wwp.icq.com/26070936

Mon, 14 Mar 2005 01:34:27 GMT
Problem #30 on http://cs.nmu.edu/programming/c/problems.htm
Groovy hepcat nrk was jivin' on Tue, 24 Sep 2002 01:56:07 -0500 in
comp.lang.c.
Re: Problem #30 on http://cs.nmu.edu/programming/c/problems.htm's a
cool scene! Dig it!

Quote:

>>>#include <stdio.h>
>>>int main(int argc, char *argv[]) {
>>>  while (argc-- > 1)
>>>    printf("%s ", argv[argc]);
>>>  return 0;
>>>}

>> Cool, but I like the tail-recursive version better

>> #include <stdio.h>

>> int main (int argc, char *argv[])
>> {
>>     printf ("%s ", argv[--argc]);
>>     return argc > 1 ? main (argc, argv) : argc;
>> }

>Hate to bring it up, but it is possible for argc to be 0, in which case both
>the above examples would result in undefined behavior.

Nonsense! In the former example, the value of argc is checked to
make sure it is greater than 1 before it is ever used. The printf()
call that comprises the body of the while loop will be skipped
completely if argc <= 1, in which case the program exits without
The latter example does indeed, however, invoke undefined behaviour
if argc == 0 upon program entry. And it writes the program name if
argc == 1 on program entry.

--

Dig the even newer still, yet more improved, sig!