what is the problem with this code?

Discussion in 'Mac Programming' started by saleh.hi.62, Dec 12, 2011.

  1. macrumors member

    Joined:
    Jul 25, 2011
    #1
    Hello guys,

    what is the problem with this code?


    Code:
    #import <Foundation/Foundation.h>
    
    int main (int argc, const char * argv[]) {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
      
    	
    	NSString *sentence=[NSString stringWithString:@"i want to filter this sentence by stoplist array for my program"];
    	
    	
    	NSArray *stopList=[[NSArray alloc]initWithObjects:@"an”,@“and”,@“by”,@“for”,@“from”,@“of”,@“the”,@“to”,@“with",nil];
    	
    	NSArray *query = [sentence componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    	
    	NSMutableArray *finalsentence=[NSMutableArray array];
    	
    	
    	for(NSString *q in query){
    	
    		if ([stopList containsObject:q]== YES) {
    			[finalsentence addObject:q];
    		}
    	
    	}
    	NSLog(@"%@",finalsentence);
        
    	
    	[pool drain];
        return 0;
    }
    
     
  2. macrumors 601

    Mr. Retrofire

    Joined:
    Mar 2, 2010
    Location:
    www.emiliana.cl
    #2
    Which problems do you have with the code?
     
  3. chown33, Dec 12, 2011
    Last edited: Dec 12, 2011

    macrumors 603

    Joined:
    Aug 9, 2009
    #3
    The first problem is: you haven't described what problem you're seeing.

    The second problem is: you haven't posted any output, error message, or description of what happened that makes you think there's a problem.

    The third problem is: the first two problems are ongoing. You continually fail to describe what you expect to happen, or what actually happens, despite repeated requests to do so. Both of those are necessary for anyone else to understand why you think there's a problem. We can't read your mind. If you don't explain what the problem is, how can anyone solve it?

    Rules of Thumb:
    Describe what you expected to happen.
    Describe what actually happened.
    Post any error messages.
    Explain why these constitute a problem.
     
  4. thread starter macrumors member

    Joined:
    Jul 25, 2011
    #4
    i have 2 arrays, i want to check if each element in first array exists in the second array or not, it does not have any error! but logically it does not work as what i want.
     
  5. macrumors 603

    Joined:
    Aug 9, 2009
    #5
    Describe what actually happened.

    Post any error messages that appear when compiling.

    Post any warning messages that appear when compiling.

    Post the output of running the program.
     
  6. thread starter macrumors member

    Joined:
    Jul 25, 2011
    #6
    my friend thank you for consideration, but i think you dont read what i wrote!!! i said no error! it does not detect the same string in those arrays, out put is the first sentence again.
     
  7. GorillaPaws, Dec 12, 2011
    Last edited: Dec 12, 2011

    macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #7
    THIS IS THE KIND OF THING YOU NEED TO INCLUDE IN YOUR FIRST POST!!!

    Don't make others play 20 questions with you to get you to explain what the hell is going on!!! It's better to give more information about what's happening than less information.

    This is a good place to start.; take the time to actually read it this time and stop wasting everyone's time.
     
  8. macrumors 603

    Joined:
    Aug 9, 2009
    #8
    Your history of saying "no error" is unreliable. It could mean any of the following (possibly more):
    1. There are no compiler errors.
    2. The program doesn't crash when run.
    3. You believe there's no error in the code, but something isn't working correctly, usually because you've made a memory management error.

    So when you say "No error", there's no way to tell which of those you mean.

    Also, I should point out that there can be compiler warnings which are in fact fatal errors. These may or may not cause runtime errors.


    You should probably learn how to use the debugger. Not knowing how to use it, even on simple programs like this, is going to become even more of a problem. A problem that will only be solved by learning to use the debugger.
     
  9. macrumors 68040

    Joined:
    Feb 2, 2008
    #9
    Seems like an error to me. ;)
     
  10. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #10
    Got it. Nice one.

    Hint (I don't want to spoil it for everyone): Look at the code in a really big font. Or in XCode with syntax coloring.
     
  11. macrumors 603

    Joined:
    Aug 9, 2009
    #11
    Or run the code in the debugger, and look at the contents of the variables.
     
  12. macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #12
    I can see the error too. You need to run the debugger and set a breakpoint at the first line of code. Check each line to make sure the objects and values are what you expect them to be. If you don't know how to do this, search Google. If you run into problems, tell us the article you're following and where you get tripped up. Be specific, be descriptive.
     
  13. macrumors newbie

    Joined:
    Dec 12, 2011
  14. macrumors 603

    Joined:
    Aug 9, 2009
    #14
    ^^^
    Probably a copy & paste from somewhere else.

    I've seen people post their code as .doc files, or as .rtf files. I can't explain why. I've even occasionally seen posters that tried to compile the .rtf file.
     
  15. jared_kipe, Dec 12, 2011
    Last edited: Dec 12, 2011

    macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #15
    The problem is the for (NSString *q in query) line just use a normal for loop with appropriate boundary conditions like this.


    Code:
    #import <Foundation/Foundation.h>
    
    int main (int argc, const char * argv[]) {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
      
    	
    	NSString *sentence=[NSString stringWithString:@"i want to filter this sentence by stoplist array for my program"];
    	
    	
    	NSArray *stopList=[[NSArray alloc]initWithObjects:@"an”,@“and”,@“by”,@“for”,@“from”,@“of”,@“the”,@“to”,@“with",nil];
    	
    	NSArray *query = [sentence componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    	
    	NSMutableArray *finalsentence=[NSMutableArray array];
    	
    	
    	for (int i = 0; i < [query count] - 1 ; i++) {
    		if ([stopList containsObject: [query objectAtIndex:i]] || (i |= 0x02) && (i -= (i > 0x07)?1:0)) {
    			[finalsentence addObject: [query objectAtIndex: i]];
    		}i++;
    	}
    	NSLog(@"%@",finalsentence);
        
    	
    	[pool drain];
        return 0;
    }
    
    EDIT: Also, this loop is the most efficiency.
     
  16. chown33, Dec 12, 2011
    Last edited: Dec 12, 2011

    macrumors 603

    Joined:
    Aug 9, 2009
  17. macrumors 68000

    Joined:
    Sep 8, 2006
    #17
    jared_kipe - I was totally confused with your boundary conditions and saw Chown33 response. To me in the original code the user is evaluating are addresses vs. the Strings he thinks he his. My Java professor had a big thing about that last semester and kept bring it up.
     
  18. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #18
    Not 100% sure I follow you, but NSArray's - (BOOL) containsObject: (id) obj, works by sending -(BOOL) isEqual: obj to every object in the array. For a lot of objects this is an address compare or hash compare, for NSString and collection classes it is a little more complicated.

    Code:
    NSString *myOriginal = [NSString stringWithString: @"hello"];
    NSMutableString *myTest = [NSMutableString string];
    [myTest appendString: @"hell"];
    [myTest appendString: @"o"];
    NSLog(@"\nmyOriginal = %#lx\nmyTest = %#lx\n[myOriginal isEqual: myTest] = %@\n", myOriginal, myTest, ([myOriginal isEqual: myTest])?@"YES":@"NO" );
    
     
  19. thread starter macrumors member

    Joined:
    Jul 25, 2011
    #19
    thank you, i tried to run this code, it worked well, but i think this code is fixed for the fixed sentences! i changed the length of my sentence variable and the answer was not right anymore. because i need to get the value of the sentence from user.
     
  20. gnasher729, Dec 13, 2011
    Last edited: Dec 13, 2011

    macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #20
    You just made my day, for all the wrong reasons.

    There are people who laugh three times at a joke: When the joke is told, when it is explained to them, and when they understand it. So we go to phase two, the explaining.

    Look at your code in XCode, with syntax coloring. You know that XCode shows strings in your code in a certain colour? Now look at "stoplist". Do you see that the string colouring isn't quite right? Think about why that would be. XCode doesn't do that without a reason. If you can't see it, look at the start of this page and make the font a lot bigger, then look again at the line where you set "stoplist". It is actually quite funny.

    Then came Jared's post. It was a joke. A really well done joke. He looked at your code, figured out which words you wanted to appear in finalSentence, and the bits that he added and that you didn't understand put exactly those words into finalSentence. Whatever you have in sentence, his code will always put the same words into finalSentence. It's a joke.


    I know you completely ignored all the posts that would have actually helped you and jumped straight at the one that contained code, but this one you should really have read. To improve your knowledge of the English language: "Effin'" is short for "Effing" which is an alternative for the word "****ing" which the software running MacRumors will edit for me. ROTFL stands for "rolling over the floor laughing". That's how funny chown33 thought Jared's code was. And he is right.
     
  21. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #21
    saleh.hi.62,

    I'm curious concerning your computers language setting and countries keyboard and layout. Do you mind sharing this information?
     
  22. thread starter macrumors member

    Joined:
    Jul 25, 2011
    #22
    I am using arabic plus english, why ?
     
  23. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #23
    I had no idea, since you didn't use argc or argv, nor did you read from standard in.
    Its called optimization, why check values I know aren't going to be in the expected output?
    A lot of people think factoring is hard, but I say to them, "not when you know the factors ahead of time... or !(x & 1)"

    ----------

    Thanks, I really regret not learning how to program before those were completely shut down. But they're still inspiration to me.

    ----------

    Mine too! Thanks again.
     
  24. thread starter macrumors member

    Joined:
    Jul 25, 2011
    #24
    unfortunately some people in this forum know what is the question & problem, but !!! since they have no idea of how solve the problem say :

    1-improve your english
    2-learn memory management
    3-learn debugger using
    etc

    guys these are not the problem ! this program logically has problem! this post had 21 replies so far, you guys that make fun of this post have you suffered to put this little code in your editor and run it instead of fooling other people's post ???

    i dont know if you guys are educated or not ! but i suggest you to change your course!
     
  25. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #25
    3 would have worked here.

    You have 20 people telling you to change your course, and instead you tell us to change ours?

    Also, you got your answer eventually, so what does it matter that it takes a day to get to it? Didn't really take any work from you, just some time.
    I WISH I could solve all my problems by just making a thread somewhere and waiting for the answer to magically arrive.
     

Share This Page