Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

saleh.hi.62

macrumors member
Original poster
Jul 25, 2011
95
0
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;
}
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,423
A sea of green
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.
 
Last edited:

saleh.hi.62

macrumors member
Original poster
Jul 25, 2011
95
0
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.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,423
A sea of green
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.

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.
 

saleh.hi.62

macrumors member
Original poster
Jul 25, 2011
95
0
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.

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.
 

GorillaPaws

macrumors 6502a
Oct 26, 2003
932
8
Richmond, VA
it does not detect the same string in those arrays, out put is the first sentence again.

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.
 
Last edited:

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,423
A sea of green
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.

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.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
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.
 

GorillaPaws

macrumors 6502a
Oct 26, 2003
932
8
Richmond, VA
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.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,423
A sea of green
^^^
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.
 

jared_kipe

macrumors 68030
Dec 8, 2003
2,967
1
Seattle
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.
 
Last edited:

larswik

macrumors 68000
Sep 8, 2006
1,552
11
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.
 

jared_kipe

macrumors 68030
Dec 8, 2003
2,967
1
Seattle
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.

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

saleh.hi.62

macrumors member
Original poster
Jul 25, 2011
95
0
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.

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.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
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.

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.


Effin' brilliant!! ROTFL!

Reminds me of early days of Obfuscated C contest.
http://en.wikipedia.org/wiki/International_Obfuscated_C_Code_Contest

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.
 
Last edited:

jared_kipe

macrumors 68030
Dec 8, 2003
2,967
1
Seattle
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.

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

----------

^^^
Effin' brilliant!! ROTFL!

Reminds me of early days of Obfuscated C contest.
http://en.wikipedia.org/wiki/International_Obfuscated_C_Code_Contest

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

----------

You just made my day, for all the wrong reasons.

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

Mine too! Thanks again.
 

saleh.hi.62

macrumors member
Original poster
Jul 25, 2011
95
0
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.

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!
 

jared_kipe

macrumors 68030
Dec 8, 2003
2,967
1
Seattle
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!

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.