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


Groups > comp.lang.c > #73566 > unrolled thread

Two options of a program to find a track in the list

Started byAlla _ <modelling.data@gmail.com>
First post2015-10-22 11:30 -0700
Last post2015-10-24 21:24 +0100
Articles 8 on this page of 68 — 12 participants

Back to article view | Back to comp.lang.c


Contents

  Two options of a program to find a track in the list Alla _ <modelling.data@gmail.com> - 2015-10-22 11:30 -0700
    Re: Two options of a program to find a track in the list Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-22 14:44 -0400
      Re: Two options of a program to find a track in the list Alla _ <modelling.data@gmail.com> - 2015-10-23 01:16 -0700
        Re: Two options of a program to find a track in the list Alla _ <modelling.data@gmail.com> - 2015-10-23 01:22 -0700
          Re: Two options of a program to find a track in the list Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-23 10:52 +0100
        Re: Two options of a program to find a track in the list Richard Heathfield <rjh@cpax.org.uk> - 2015-10-23 09:57 +0100
          Re: Two options of a program to find a track in the list Alla _ <modelling.data@gmail.com> - 2015-10-23 02:35 -0700
            Re: Two options of a program to find a track in the list Richard Heathfield <rjh@cpax.org.uk> - 2015-10-23 10:59 +0100
              Re: Two options of a program to find a track in the list Alla _ <modelling.data@gmail.com> - 2015-10-23 03:42 -0700
              Re: Two options of a program to find a track in the list Alla _ <modelling.data@gmail.com> - 2015-10-23 03:47 -0700
                Re: Two options of a program to find a track in the list Barry Schwarz <schwarzb@dqel.com> - 2015-10-23 11:02 -0700
                Re: Two options of a program to find a track in the list Geoff <geoff@invalid.invalid> - 2015-10-23 11:24 -0700
          Re: Two options of a program to find a track in the list Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-23 10:39 +0100
            Re: Two options of a program to find a track in the list Richard Heathfield <rjh@cpax.org.uk> - 2015-10-23 11:03 +0100
              Re: Two options of a program to find a track in the list Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-23 11:21 +0100
                Re: Two options of a program to find a track in the list Richard Heathfield <rjh@cpax.org.uk> - 2015-10-23 11:36 +0100
          Re: Two options of a program to find a track in the list Alla _ <modelling.data@gmail.com> - 2015-10-23 02:42 -0700
            Re: Two options of a program to find a track in the list Richard Heathfield <rjh@cpax.org.uk> - 2015-10-23 11:10 +0100
          Re: Two options of a program to find a track in the list Alla _ <modelling.data@gmail.com> - 2015-10-23 03:24 -0700
            Re: Two options of a program to find a track in the list Richard Heathfield <rjh@cpax.org.uk> - 2015-10-23 11:45 +0100
              Re: Two options of a program to find a track in the list Alla _ <modelling.data@gmail.com> - 2015-10-23 09:00 -0700
                Re: Two options of a program to find a track in the list Richard Heathfield <rjh@cpax.org.uk> - 2015-10-23 17:16 +0100
                  Re: Two options of a program to find a track in the list Alla _ <modelling.data@gmail.com> - 2015-10-23 09:47 -0700
          Re: Two options of a program to find a track in the list Bartc <bc@freeuk.com> - 2015-10-23 13:19 +0100
            Re: Two options of a program to find a track in the list Richard Heathfield <rjh@cpax.org.uk> - 2015-10-23 15:38 +0100
              Re: Two options of a program to find a track in the list Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-23 10:57 -0400
                Re: Two options of a program to find a track in the list Richard Heathfield <rjh@cpax.org.uk> - 2015-10-23 15:59 +0100
                Re: Two options of a program to find a track in the list David Brown <david.brown@hesbynett.no> - 2015-10-23 17:25 +0200
                  Re: Two options of a program to find a track in the list Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-23 11:55 -0400
                    Re: Two options of a program to find a track in the list Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2015-10-23 10:45 -0600
                  Re: Two options of a program to find a track in the list Richard Heathfield <rjh@cpax.org.uk> - 2015-10-23 16:55 +0100
              Re: Two options of a program to find a track in the list Bartc <bc@freeuk.com> - 2015-10-23 17:21 +0100
                Re: Two options of a program to find a track in the list Bartc <bc@freeuk.com> - 2015-10-23 17:23 +0100
          Re: Two options of a program to find a track in the list Keith Thompson <kst-u@mib.org> - 2015-10-23 12:58 -0700
        Re: Two options of a program to find a track in the list Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-23 02:19 -0700
          Re: Two options of a program to find a track in the list Richard Heathfield <rjh@cpax.org.uk> - 2015-10-23 10:28 +0100
        Re: Two options of a program to find a track in the list Bartc <bc@freeuk.com> - 2015-10-23 11:12 +0100
          Re: Two options of a program to find a track in the list Keith Thompson <kst-u@mib.org> - 2015-10-23 13:42 -0700
            Re: Two options of a program to find a track in the list Bartc <bc@freeuk.com> - 2015-10-23 22:14 +0100
              Re: Two options of a program to find a track in the list Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-23 22:28 +0100
                Re: Two options of a program to find a track in the list Bartc <bc@freeuk.com> - 2015-10-23 22:47 +0100
                  Re: Two options of a program to find a track in the list Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-23 23:21 +0100
                    Re: Two options of a program to find a track in the list Bartc <bc@freeuk.com> - 2015-10-24 01:37 +0100
              Re: Two options of a program to find a track in the list Keith Thompson <kst-u@mib.org> - 2015-10-23 14:52 -0700
                Re: Two options of a program to find a track in the list Bartc <bc@freeuk.com> - 2015-10-23 23:06 +0100
                  Re: Two options of a program to find a track in the list Keith Thompson <kst-u@mib.org> - 2015-10-23 16:39 -0700
          Re: Two options of a program to find a track in the list Richard Heathfield <rjh@cpax.org.uk> - 2015-10-24 21:26 +0100
            Re: Two options of a program to find a track in the list Bartc <bc@freeuk.com> - 2015-10-24 22:28 +0100
              Re: Two options of a program to find a track in the list Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-24 15:26 -0700
                Re: Two options of a program to find a track in the list Richard Heathfield <rjh@cpax.org.uk> - 2015-10-24 23:44 +0100
        Re: Two options of a program to find a track in the list Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-23 09:03 -0400
          Re: Two options of a program to find a track in the list Alla _ <modelling.data@gmail.com> - 2015-10-23 09:10 -0700
        Re: Two options of a program to find a track in the list Geoff <geoff@invalid.invalid> - 2015-10-23 11:21 -0700
    Re: Two options of a program to find a track in the list Barry Schwarz <schwarzb@dqel.com> - 2015-10-22 14:50 -0700
      Re: Two options of a program to find a track in the list Alla _ <modelling.data@gmail.com> - 2015-10-23 01:25 -0700
        Re: Two options of a program to find a track in the list Richard Heathfield <rjh@cpax.org.uk> - 2015-10-23 10:00 +0100
      Re: Two options of a program to find a track in the list Alla _ <modelling.data@gmail.com> - 2015-10-23 01:36 -0700
        Re: Two options of a program to find a track in the list Richard Heathfield <rjh@cpax.org.uk> - 2015-10-23 10:04 +0100
    Re: Two options of a program to find a track in the list Alla _ <modelling.data@gmail.com> - 2015-10-23 03:58 -0700
      Re: Two options of a program to find a track in the list Richard Heathfield <rjh@cpax.org.uk> - 2015-10-23 12:07 +0100
        Re: Two options of a program to find a track in the list Alla _ <modelling.data@gmail.com> - 2015-10-23 09:28 -0700
          Re: Two options of a program to find a track in the list Richard Heathfield <rjh@cpax.org.uk> - 2015-10-23 18:04 +0100
            Re: Two options of a program to find a track in the list Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-23 13:31 -0400
              Re: Two options of a program to find a track in the list Richard Heathfield <rjh@cpax.org.uk> - 2015-10-24 21:21 +0100
        Re: Two options of a program to find a track in the list Keith Thompson <kst-u@mib.org> - 2015-10-23 13:45 -0700
          Re: Two options of a program to find a track in the list Ian Collins <ian-news@hotmail.com> - 2015-10-24 10:32 +1300
            Re: Two options of a program to find a track in the list Keith Thompson <kst-u@mib.org> - 2015-10-23 15:33 -0700
          Re: Two options of a program to find a track in the list Richard Heathfield <rjh@cpax.org.uk> - 2015-10-24 21:24 +0100

Page 4 of 4 — ← Prev page 1 2 3 [4]


#73633

FromAlla _ <modelling.data@gmail.com>
Date2015-10-23 09:28 -0700
Message-ID<22759c37-2632-41d7-bbe3-0ded64824796@googlegroups.com>
In reply to#73610
> int entries_number = sizeof all_tracks / sizeof all_tracks[0];
> 
> That way, if you later decide to make your array bigger by adding 
> another initialiser, you won't have to remember to change the 5 to a 6.

Yes, sorry, should have corrected it here. Done.


> >          while (search_track[j] == all_tracks[j]
> 
> j has not been given a value. Even if it had been, you're trying to 
> compare a single character with an array, which is like comparing iced 
> buns with the Suez crisis - it just doesn't work.
> 
> To compare strings, use strcmp.
> 
> To find a substring (which is what you ought to be doing), use strstr.
> 
My goal in writing this program was to write a 'manual' 
version of strstr or/and strcmp functions. I wanted to 
test my understanding of loops, arrays, etc. 

Yes, I am trying to compare every character in search_tracks 
with every entry of the all_tracks (every character, i.e. every
entry in each column j, of a certain row i). If i scans through
every row, then j should scan through each column of a particular
row (as it was in previous programs with patterns; we used the 
same concept there, if I understand that correctly).

Indeed, I have totally missed assigning initial value to j.

for (i = 0; i < entries_number; i++) 
    {
        
        j = 0;
        
        while (search_track[j] == all_tracks[j] && search_track[j] != '\0' && all_tracks[j] != '\0')
              j++;
...

[toc] | [prev] | [next] | [standalone]


#73637

FromRichard Heathfield <rjh@cpax.org.uk>
Date2015-10-23 18:04 +0100
Message-ID<n0dp7h$ne5$1@dont-email.me>
In reply to#73633
On 23/10/15 17:28, Alla _ wrote:
>
<snip>

> My goal in writing this program was to write a 'manual'
> version of strstr or/and strcmp functions. I wanted to
> test my understanding of loops, arrays, etc.

Okay, that's a perfectly sensible thing to want to do. I'm not sure 
you're going the right way about it, though. ;-)

> Yes, I am trying to compare every character in search_tracks
> with every entry of the all_tracks (every character, i.e. every
> entry in each column j, of a certain row i). If i scans through
> every row, then j should scan through each column of a particular
> row (as it was in previous programs with patterns; we used the
> same concept there, if I understand that correctly).
>
> Indeed, I have totally missed assigning initial value to j.
>
> for (i = 0; i < entries_number; i++)
>      {
>
>          j = 0;
>
>          while (search_track[j] == all_tracks[j] && search_track[j] != '\0' && all_tracks[j] != '\0')
>                j++;

all_tracks has type char[5][80]
all_tracks[0] has type char[80] - that is, it's an array.

You can't compare an array to a single character, as you are attempting 
to do here:

search_track[j] == all_tracks[j]

In fact, if you could, it would make matters worse very quickly, because 
search_track[j] will quickly exceed 4, at which point all_tracks[j] will 
step outside your array bounds!

You would be better off with a function. This would help to clarify 
what's going on:

int mystringcomparefunction(const char *s, const char *t)
{
   int diff = 0;
   if(s != NULL && t != NULL)
   {
     while(diff == 0 && *s != '\0' && *t != '\0' && *s == *t)
     {
       diff = (*s > *t) - (*s < *t); /* This line will repay study! */
       ++s;
       ++t;
     }
     if(diff == 0)
     {
       if(*s != '\0') /* s is longer */
       {
         diff = 1;
       }
       else if(*t != '\0') /* t is longer */
       {
         diff = -1;
       }
       /* otherwise, they're the same */
     }
   }
   return diff;
}

Now you can use that in your loop:

for(i = 0; i < entries_number; i++)
{
   if(mystringcomparefunction(all_tracks[i], search_track) == 0)
   {
     that's a hit! You can print the result.
   }
}

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

[toc] | [prev] | [next] | [standalone]


#73638

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2015-10-23 13:31 -0400
Message-ID<n0dqq1$u7k$1@dont-email.me>
In reply to#73637
On 10/23/2015 1:04 PM, Richard Heathfield wrote:
> [...]
> You would be better off with a function. This would help to clarify
> what's going on:
>
> int mystringcomparefunction(const char *s, const char *t)
> {
>    int diff = 0;
>    if(s != NULL && t != NULL)
>    {
>      while(diff == 0 && *s != '\0' && *t != '\0' && *s == *t)
>      {
>        diff = (*s > *t) - (*s < *t); /* This line will repay study! */

	diff = 0;  /* The payoff! */

     (I imagine you may have started to write the function one way,
decided a different way would be better, and made half the changes. :)

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

[toc] | [prev] | [next] | [standalone]


#73734

FromRichard Heathfield <rjh@cpax.org.uk>
Date2015-10-24 21:21 +0100
Message-ID<n0gp40$18l$1@dont-email.me>
In reply to#73638
On 23/10/15 18:31, Eric Sosman wrote:
> On 10/23/2015 1:04 PM, Richard Heathfield wrote:
>> [...]
>> You would be better off with a function. This would help to clarify
>> what's going on:
>>
>> int mystringcomparefunction(const char *s, const char *t)
>> {
>>    int diff = 0;
>>    if(s != NULL && t != NULL)
>>    {
>>      while(diff == 0 && *s != '\0' && *t != '\0' && *s == *t)
>>      {
>>        diff = (*s > *t) - (*s < *t); /* This line will repay study! */
>
>      diff = 0;  /* The payoff! */

"Oh bother", said Pooh.

>      (I imagine you may have started to write the function one way,
> decided a different way would be better, and made half the changes. :)

#include <stdio.h>

int main(void)
{
   int i;
   while(i < 100)
   {
     printf("I must test before posting, 100% of the time!\n");
   }
   return 0;
}

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

[toc] | [prev] | [next] | [standalone]


#73652

FromKeith Thompson <kst-u@mib.org>
Date2015-10-23 13:45 -0700
Message-ID<lnr3kluxdy.fsf@kst-u.example.com>
In reply to#73610
Richard Heathfield <rjh@cpax.org.uk> writes:
> On 23/10/15 11:58, Alla _ wrote:
[...]
>> int entries_number = 5;
>
> Better (but not perfect):
>
> int entries_number = sizeof all_tracks / sizeof all_tracks[0];

Even better:

const int entries_number = sizeof all_tracks / sizeof all_tracks[0];
[...]

-- 
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"

[toc] | [prev] | [next] | [standalone]


#73657

FromIan Collins <ian-news@hotmail.com>
Date2015-10-24 10:32 +1300
Message-ID<d8vnbuFuctcU3@mid.individual.net>
In reply to#73652
Keith Thompson wrote:
> Richard Heathfield <rjh@cpax.org.uk> writes:
>> On 23/10/15 11:58, Alla _ wrote:
> [...]
>>> int entries_number = 5;
>>
>> Better (but not perfect):
>>
>> int entries_number = sizeof all_tracks / sizeof all_tracks[0];
>
> Even better:
>
> const int entries_number = sizeof all_tracks / sizeof all_tracks[0];
> [...]

An irresistible nit to pick...

Given you are saving the result of sizeof, shouldn't that be

const size_t entries_number = sizeof all_tracks / sizeof all_tracks[0];

?

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#73666

FromKeith Thompson <kst-u@mib.org>
Date2015-10-23 15:33 -0700
Message-ID<lnegglusen.fsf@kst-u.example.com>
In reply to#73657
Ian Collins <ian-news@hotmail.com> writes:
> Keith Thompson wrote:
>> Richard Heathfield <rjh@cpax.org.uk> writes:
>>> On 23/10/15 11:58, Alla _ wrote:
>> [...]
>>>> int entries_number = 5;
>>>
>>> Better (but not perfect):
>>>
>>> int entries_number = sizeof all_tracks / sizeof all_tracks[0];
>>
>> Even better:
>>
>> const int entries_number = sizeof all_tracks / sizeof all_tracks[0];
>> [...]
>
> An irresistible nit to pick...
>
> Given you are saving the result of sizeof, shouldn't that be
>
> const size_t entries_number = sizeof all_tracks / sizeof all_tracks[0];
>
> ?

Yes, that's probably what I'd do.  But in this case we can be reasonably
sure that the number of entries doesn't exceed INT_MAX.

-- 
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"

[toc] | [prev] | [next] | [standalone]


#73735

FromRichard Heathfield <rjh@cpax.org.uk>
Date2015-10-24 21:24 +0100
Message-ID<n0gpa0$2c5$1@dont-email.me>
In reply to#73652
On 23/10/15 21:45, Keith Thompson wrote:
> Richard Heathfield <rjh@cpax.org.uk> writes:
>> On 23/10/15 11:58, Alla _ wrote:
> [...]
>>> int entries_number = 5;
>>
>> Better (but not perfect):
>>
>> int entries_number = sizeof all_tracks / sizeof all_tracks[0];
>
> Even better:
>
> const int entries_number = sizeof all_tracks / sizeof all_tracks[0];

While you're at it, why not teach her about maximum munch and how to 
declare a pointer to function taking as a parameter a pointer to a 
function of that type and returning a pointer to a function of that type 
and the rules of what you can and can't do during an interrupt?

It's imperative that Alla must know everything, right now, before she 
learns anything else.

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

[toc] | [prev] | [standalone]


Page 4 of 4 — ← Prev page 1 2 3 [4]

Back to top | Article view | comp.lang.c


csiph-web