Like Tree1Likes
  • 1 Post By CommonTater

Noob array question

This is a discussion on Noob array question within the C Programming forums, part of the General Programming Boards category; I have two arrays: a1 and a2. Assuming a1 is smaller than a2, how do I verify if a1 is ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    4

    Question Noob array question

    I have two arrays: a1 and a2. Assuming a1 is smaller than a2, how do I verify if a1 is part of a2?
    For example: if a1 is [1|3|8|7] and a2 is [5|6|1|3|8|7|3|3|4], the answer to the verification will be yes because a2 contains the sequence ...1|3|8|7... which is the a1 array

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Well if they were two arrays of characters, the answer would be strstr()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    To solve the problem you need to understand the problem... in the tiniest steps possible.
    Set yourself down and think about what has to happen, and in what order to compare the two arrays.

    I'll give you a start...
    The first thing you have to do is find the first character of a1 in a2 ...
    But, already a complication... what happens if it's not there?

    Ok... now what do you think has to happen next?
    Last edited by CommonTater; 05-27-2011 at 02:09 PM. Reason: "Spelling"?? Now where'd I put that Dictonary?

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    4
    Quote Originally Posted by CommonTater View Post
    To solve the problem you need to understand the problem... in the tiniest steps possible.
    Set yourself down and think about what has to happen, and in what order to compare the two arrays.

    I'll give you a start...
    The first thing you have to do is find the first character of a1 in a2 ...
    But, already a complication... what happens if it's not there?

    Ok... now what do you think has to happen next?

    The program is gonna end I guess

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by popolianuz View Post
    The program is gonna end I guess
    This discussion will, if you don't put in some effort...
    quzah likes this.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    4
    Quote Originally Posted by CommonTater View Post
    This discussion will, if you don't put in some effort...
    If the program doesn't end then I don't know what's gonna happen because this is the only logical answer I can think of. If the first character in a1 isn't in a2, it means a1 is not part of a2, thus resulting in the ending of the program.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    And what if it IS in there?
    What should happen then?

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    4
    If it is in there, the next a1 character got to be in there too, right after the corresponding a2 character for the previous a1 character, except if the number of remaining characters in a1 is bigger than the number of remaining characters in a2. Is this correct?

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Think analytically... if the first character is there... now you need a test for the second character..

    Is it there? What do I do next?
    If it's not there... what to do next?

    Do you really care at this point how many characters remain in the a2?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob question! Code completion - Array challenge
    By theleprechaun in forum C++ Programming
    Replies: 6
    Last Post: 08-22-2008, 08:58 PM
  2. Noob : Creating a global array?
    By Swerve in forum C++ Programming
    Replies: 3
    Last Post: 06-07-2008, 10:38 AM
  3. Noob question (redeclaring a array)
    By Demon.killer in forum C Programming
    Replies: 8
    Last Post: 10-21-2006, 12:06 PM
  4. Noob fopen Array help!!!
    By elementalwinds in forum C Programming
    Replies: 8
    Last Post: 10-19-2006, 09:54 PM
  5. Replies: 2
    Last Post: 11-24-2005, 12:30 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21