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


Groups > comp.lang.c > #54038

Re: C-Programming

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: C-Programming
Date 2014-12-07 13:07 +0000
Organization A noiseless patient Spider
Message-ID <87lhmjvc9h.fsf@bsb.me.uk> (permalink)
References <8effc0a8-53e1-4b2d-940b-913db8f0ce0e@googlegroups.com>

Show all headers | View raw


arvindgupta01031988@gmail.com writes:

> How to use a recursive function in C ?

The best way to find out is to write programs to solve problems where
there is a reasonably "natural" recursive solution.  Here are a few
ideas that, with luck, don't rely on data structures you may not yet
have learned about.

Binary search.  Given a sorted array of numbers (let's say ints for now)
find out if a given number is in the list.  I.e. write

  bool contains(int n, const int *numbers, size_t how_many);

Making change.  Given a sorted array of coin denominations, count how
many ways there are to make up a given quantity.  For example, in the UK
with coin denominations of 1, 2, 5, 10, 20, 50, 100 and 200 (pence)
there are 73,682 ways to make 2 pounds (200 pence).

Variation: print the different ways.

Variation: do the same thing but limited by the number of coins of each
denomination that are available.

Permutations.  Print all the permutations of a string.  I.e. write a
function print_perms so that so that print_perms("abc") prints

  abc
  acb
  bac
  bca
  cab
  cba

in some order of your choosing.

Eight queens.  How many ways are there of placing 8 queens on an 8x8
chess board so that no two are attacking each other (i.e. no two are on
the same row, column or diagonal)?

Variation: what about N queens on and NxN chess board?

All of these can be written without recursion, and sometimes that
natural recursive solution is far from the most efficient, but they do
give you a way into thinking about breaking a problem up identical
sub-problems with is, in essence, what recursion allows you to do.

-- 
Ben.

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


Thread

C-Programming arvindgupta01031988@gmail.com - 2014-12-06 11:09 -0800
  Re: C-Programming "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2014-12-06 11:16 -0800
  Re: C-Programming James Kuyper <jameskuyper@verizon.net> - 2014-12-06 14:46 -0500
    Re: C-Programming Kleuske <kleuske@nowhere.net> - 2014-12-07 10:48 +0000
      Re: C-Programming Keith Thompson <kst-u@mib.org> - 2014-12-07 07:43 -0800
  Re: C-Programming Siri Crews <chine.bleu@yahoo.com> - 2014-12-06 12:22 -0800
    Re: C-Programming gordonb.ukogy@burditt.org (Gordon Burditt) - 2014-12-19 03:29 -0600
      Re: C-Programming Siri Crews <chine.bleu@yahoo.com> - 2014-12-19 01:34 -0800
        Re: C-Programming Richard Heathfield <invalid@see.sig.invalid> - 2014-12-19 10:18 +0000
          Re: C-Programming jacob navia <jacob@spamsink.net> - 2014-12-19 17:42 +0100
            Re: C-Programming rouben@shadow.(none) (Rouben Rostamian) - 2014-12-19 19:00 +0000
              Re: C-Programming jacob navia <jacob@spamsink.net> - 2014-12-19 20:31 +0100
                Re: C-Programming jacob navia <jacob@spamsink.net> - 2014-12-19 20:42 +0100
              Re: C-Programming Keith Thompson <kst-u@mib.org> - 2014-12-19 11:54 -0800
        Re: C-Programming raltbos@xs4all.nl (Richard Bos) - 2014-12-19 11:29 +0000
          Re: C-Programming Noob <root@127.0.0.1> - 2014-12-20 10:22 +0100
            Re: C-Programming Keith Thompson <kst-u@mib.org> - 2014-12-20 11:48 -0800
        Re: C-Programming Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-12-19 03:33 -0800
          Re: C-Programming Siri Crews <chine.bleu@yahoo.com> - 2014-12-19 04:02 -0800
      Re: C-Programming glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-12-19 17:28 +0000
  Re: C-Programming jacob navia <jacob@spamsink.net> - 2014-12-06 21:27 +0100
  Re: C-Programming Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-07 13:07 +0000
  Re: C-Programming David Brown <david.brown@hesbynett.no> - 2014-12-08 01:24 +0100
    Re: C-Programming Tim Rentsch <txr@alumni.caltech.edu> - 2014-12-16 04:44 -0800
      Re: C-Programming David Brown <david.brown@hesbynett.no> - 2014-12-16 15:02 +0100
        Re: C-Programming BartC <bc@freeuk.com> - 2014-12-16 18:27 +0000
          Re: C-Programming David Brown <david.brown@hesbynett.no> - 2014-12-17 01:01 +0100
            Re: C-Programming BartC <bc@freeuk.com> - 2014-12-17 00:23 +0000
        Re: C-Programming Tim Rentsch <txr@alumni.caltech.edu> - 2014-12-16 16:30 -0800
          Re: C-Programming David Brown <david.brown@hesbynett.no> - 2014-12-17 02:18 +0100
            Re: C-Programming Tim Rentsch <txr@alumni.caltech.edu> - 2014-12-16 18:23 -0800
              Re: C-Programming David Brown <david.brown@hesbynett.no> - 2014-12-17 09:59 +0100
                Re: C-Programming Tim Rentsch <txr@alumni.caltech.edu> - 2014-12-17 08:52 -0800
                Re: C-Programming David Brown <david.brown@hesbynett.no> - 2014-12-18 11:11 +0100
                Re: C-Programming Tim Rentsch <txr@alumni.caltech.edu> - 2014-12-18 09:49 -0800
          Re: C-Programming JohnF <john@please.see.sig.for.email.com> - 2014-12-17 10:14 +0000
            Re: C-Programming Richard Heathfield <invalid@see.sig.invalid> - 2014-12-17 14:26 +0000
              Re: C-Programming Tim Rentsch <txr@alumni.caltech.edu> - 2014-12-17 09:13 -0800
                Re: C-Programming Richard Heathfield <invalid@see.sig.invalid> - 2014-12-17 17:34 +0000
                Re: C-Programming Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-12-17 23:26 -0800
                Re: C-Programming Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-12-18 00:21 -0800
                Re: C-Programming Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-18 17:06 +0000
                Re: C-Programming Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-12-18 09:29 -0800
                Re: C-Programming Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-19 02:36 +0000
                Re: C-Programming Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-12-19 03:41 -0800
                Re: C-Programming Tim Rentsch <txr@alumni.caltech.edu> - 2014-12-18 08:52 -0800
              Re: C-Programming Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-17 23:23 +0000
                Re: C-Programming BartC <bc@freeuk.com> - 2014-12-18 00:02 +0000
                Re: C-Programming Richard Heathfield <invalid@see.sig.invalid> - 2014-12-18 00:15 +0000
                Re: C-Programming Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-18 16:37 +0000
                Re: C-Programming Richard Heathfield <invalid@see.sig.invalid> - 2014-12-18 18:08 +0000
                Re: C-Programming Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-19 02:40 +0000
                Re: C-Programming Richard Heathfield <invalid@see.sig.invalid> - 2014-12-19 07:51 +0000
            Re: C-Programming Tim Rentsch <txr@alumni.caltech.edu> - 2014-12-17 09:07 -0800
              Re: C-Programming JohnF <john@please.see.sig.for.email.com> - 2014-12-19 04:22 +0000
                Re: C-Programming Tim Rentsch <txr@alumni.caltech.edu> - 2014-12-19 09:22 -0800
                Re: C-Programming Greg Martin <greg.at.softsprocket@dot.com> - 2014-12-20 02:39 +0000
                Re: C-Programming JohnF <john@please.see.sig.for.email.com> - 2014-12-20 06:57 +0000
  Re: C-Programming Tim Rentsch <txr@alumni.caltech.edu> - 2014-12-15 23:53 -0800

csiph-web