Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #60460

Re: can this be done recursively

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: can this be done recursively
Date 2015-04-19 14:09 -0700
Organization None to speak of
Message-ID <87sibvomfh.fsf@kst-u.example.com> (permalink)
References <b0319922-1b5e-48d2-8cc7-5cd1a5e866e6@googlegroups.com>

Show all headers | View raw


G G <gdotone@gmail.com> writes:
> This problem comes from "C How to Program" by Deitel and Deitel
>
> 5.18
>
>  (Even or Odd) Write a program that inputs a series of integers 
>     and passes them one at a time to function even, which uses the remainder 
>     operator to determine whether an integer is even. The function should 
>     take an integer argument and return 1 if the integer is even and 0 
>     otherwise.
>
> this is from chapter 5, arrays have not been introduced yet, recursion has.
>
> no need to show an answer, i'm just curious.
>
> to solve the problem where it can continuous have input, i used a while loop
> and a sentinel.

Was there a question in there?  I didn't see it.

Using a sentinel value is one reasonable approach.  A more general
one is to read as much input as is provided.  For example, if you use

    scanf("%d", &n);

to read each int value, you can check the value returned by scanf
to determine whether it was able to read an integer value, and quit
if it wasn't.  (Read the documentation for scanf for details.)
The user can then signal end of input by typing Control-D on a
UNIX-like system, or Control-Z on Windows.

Using scanf for numeric input has some serious problems (in
particular its behavior is undefined if the input value is out
of range), but it's good enough for a simple program like this.
A more robust solution would be to read lines using fgets() and
then parse them using strtol().  You'll get to that later.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

can this be done recursively G G <gdotone@gmail.com> - 2015-04-19 09:50 -0700
  Re: can this be done recursively Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-04-19 21:14 +0100
    Re: can this be done recursively G G <gdotone@gmail.com> - 2015-04-19 14:20 -0700
  Re: can this be done recursively Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-04-19 16:32 -0400
    Re: can this be done recursively G G <gdotone@gmail.com> - 2015-04-19 14:25 -0700
  Re: can this be done recursively Keith Thompson <kst-u@mib.org> - 2015-04-19 14:09 -0700
    Re: can this be done recursively G G <gdotone@gmail.com> - 2015-04-19 14:35 -0700
      Re: can this be done recursively jt@toerring.de (Jens Thoms Toerring) - 2015-04-19 22:57 +0000
  Re: can this be done recursively G G <gdotone@gmail.com> - 2015-04-20 04:30 -0700
    Re: can this be done recursively Richard Heathfield <rjh@cpax.org.uk> - 2015-04-20 12:49 +0100
      Re: can this be done recursively G G <gdotone@gmail.com> - 2015-04-20 05:28 -0700
      Re: can this be done recursively jt@toerring.de (Jens Thoms Toerring) - 2015-04-20 13:09 +0000
  Re: can this be done recursively G G <gdotone@gmail.com> - 2015-04-22 09:42 -0700
    Re: can this be done recursively Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-04-22 10:12 -0700
    Re: can this be done recursively James Kuyper <jameskuyper@verizon.net> - 2015-04-22 13:29 -0400
    Re: can this be done recursively Ben Bacarisse <ben.lists@bsb.me.uk> - 2015-04-22 20:57 +0100
      Re: can this be done recursively James Kuyper <jameskuyper@verizon.net> - 2015-04-22 16:12 -0400
        Re: can this be done recursively Ben Bacarisse <ben.lists@bsb.me.uk> - 2015-04-22 23:14 +0100
          Re: can this be done recursively James Kuyper <jameskuyper@verizon.net> - 2015-04-22 18:56 -0400
  Re: can this be done recursively G G <gdotone@gmail.com> - 2015-04-23 07:59 -0700

csiph-web