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


Groups > comp.lang.c > #150306

Re: After doing a lot of the problems on codingbat.com

From Lew Pitcher <lew.pitcher@digitalfreehold.ca>
Newsgroups comp.lang.c
Subject Re: After doing a lot of the problems on codingbat.com
Date 2020-02-27 14:52 -0500
Organization The Pitcher Digital Freehold
Message-ID <r396mg$uob$1@dont-email.me> (permalink)
References <2f146b5c-e025-46ef-af7b-7c328c9f4206@googlegroups.com> <20200227200243.157a5e51fe7577f22aa37378@g{oogle}mail.com>

Show all headers | View raw


Anton Shepelev wrote:

> chad:
> 
>> I've come to this horrible realization that I don't
>> understand a for loop as well as what I thought I did.
> 
> Just don't abuse the for loop, cramping all kinds of
> unrelated code into its control expressions.  Always use\
> it as a loop with a counter:
[snip]

Good advice to someone who doesn't understand how to use a for() loop.

However, once chad gets past the basic understanding, there's a lot more you 
can do with for() than just count loops

You can
 - find the end of a string
   char *ptr;
   for (ptr = "string" ; *ptr ; ++ptr) continue;

- or count the number of characters
   char *ptr;
   size_t nchars;
   for (ptr = "string", nchars = 0 ; *ptr ; ++ptr) ++nchars;

 - or traverse a linked list
   struct list { struct list *next; int valu; } *root = NULL;
   /* ... */
  for (struct list *node = root; node; node = node->next)
  {
    /* do something with this node */
  }

- or even perform a binary search through an ordered array
int *binsearch(int key, int array[], size_t nelement)
{
  size_t lowpt = 0,             /* lowest index in search range */
         midpt,                 /* midpoint index; where to search */
         highpt = nelement - 1; /* highest index in search range */

  /* LOOP through search UNTIL match found OR no elements left to search */
  for (midpt = (lowpt+highpt)/2;                /* start in the middle */
       array[midpt] != key && highpt > lowpt;   /* not found AND in bounds*/
       midpt = (lowpt+highpt)/2)                /* compute new midpoint */
  { /* ============================ **
    ** array[midpt] was NOT a match **
    ** ============================ */
    if (key > array[midpt])
      lowpt = midpt + 1;        /* narrow search to between here and high */
    else
      highpt = midpt - 1;       /* narrow search to beween low and here */
  }

  /* loop terminates when entry found OR no more array to search */

  return (array[midpt] == key ? array+midpt : NULL);
}

A good programmer doesn't limit themselves to using for() strictly as a 
counted loop.

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


Thread

After doing a lot of the problems on codingbat.com chad <cdalten@gmail.com> - 2020-02-25 06:55 -0800
  Re: After doing a lot of the problems on codingbat.com Anton Shepelev <anton.txt@g{oogle}mail.com> - 2020-02-27 20:02 +0300
    Re: After doing a lot of the problems on codingbat.com James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-02-27 11:41 -0800
      Re: After doing a lot of the problems on codingbat.com Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2020-02-27 14:48 -0700
      Re: After doing a lot of the problems on codingbat.com Bart <bc@freeuk.com> - 2020-02-27 23:17 +0000
        Re: After doing a lot of the problems on codingbat.com James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-02-27 16:40 -0800
          Re: After doing a lot of the problems on codingbat.com Bart <bc@freeuk.com> - 2020-02-28 01:08 +0000
            Re: After doing a lot of the problems on codingbat.com Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-02-28 01:37 -0800
            Re: After doing a lot of the problems on codingbat.com James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-02-28 06:21 -0800
              Re: After doing a lot of the problems on codingbat.com Bart <bc@freeuk.com> - 2020-02-28 16:15 +0000
                Re: After doing a lot of the problems on codingbat.com James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-02-28 10:37 -0800
                Re: After doing a lot of the problems on codingbat.com Bart <bc@freeuk.com> - 2020-02-28 19:22 +0000
                Re: After doing a lot of the problems on codingbat.com David Brown <david.brown@hesbynett.no> - 2020-03-01 14:54 +0100
                Re: After doing a lot of the problems on codingbat.com Bart <bc@freeuk.com> - 2020-03-01 15:36 +0000
                Re: After doing a lot of the problems on codingbat.com David Brown <david.brown@hesbynett.no> - 2020-03-01 17:08 +0100
          Re: After doing a lot of the problems on codingbat.com Richard Damon <Richard@Damon-Family.org> - 2020-02-27 21:57 -0500
          Really??? (Was: After doing a lot of the problems on codingbat.com) gazelle@shell.xmission.com (Kenny McCormack) - 2020-02-28 13:06 +0000
            Re: Really??? (Was: After doing a lot of the problems on codingbat.com) chad <cdalten@gmail.com> - 2020-02-28 05:47 -0800
            Re: Really??? (Was: After doing a lot of the problems on codingbat.com) James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-02-28 05:59 -0800
          Re: After doing a lot of the problems on codingbat.com Bart <bc@freeuk.com> - 2020-02-28 14:04 +0000
            Re: After doing a lot of the problems on codingbat.com Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-02-28 17:50 +0000
              Re: After doing a lot of the problems on codingbat.com Anton Shepelev <anton.txt@g{oogle}mail.com> - 2020-02-28 20:59 +0300
                Re: After doing a lot of the problems on codingbat.com Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-02-28 22:37 +0000
              Re: After doing a lot of the problems on codingbat.com Bart <bc@freeuk.com> - 2020-02-29 00:17 +0000
                Re: After doing a lot of the problems on codingbat.com Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-02-29 01:01 +0000
        Re: After doing a lot of the problems on codingbat.com Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-04-02 19:22 -0700
    Re: After doing a lot of the problems on codingbat.com Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2020-02-27 14:52 -0500
      Re: After doing a lot of the problems on codingbat.com Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-02-27 20:42 +0000
        Re: After doing a lot of the problems on codingbat.com David Brown <david.brown@hesbynett.no> - 2020-02-27 22:59 +0100
      Re: After doing a lot of the problems on codingbat.com Bart <bc@freeuk.com> - 2020-02-27 20:48 +0000
      Re: After doing a lot of the problems on codingbat.com Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-03-27 20:06 -0700
    Re: After doing a lot of the problems on codingbat.com chad <cdalten@gmail.com> - 2020-02-27 15:28 -0800

csiph-web