PDA

View Full Version : what is the problem with this code?




saleh.hi.62
Dec 12, 2011, 09:46 AM
Hello guys,

what is the problem with 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(NSString *q in query){

if ([stopList containsObject:q]== YES) {
[finalsentence addObject:q];
}

}
NSLog(@"%@",finalsentence);


[pool drain];
return 0;
}



Mr. Retrofire
Dec 12, 2011, 10:06 AM
What is the problem with this code?

Which problems do you have with the code?

chown33
Dec 12, 2011, 10:12 AM
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.

saleh.hi.62
Dec 12, 2011, 10:37 AM
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
Dec 12, 2011, 10:49 AM
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
Dec 12, 2011, 11:12 AM
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
Dec 12, 2011, 11:24 AM
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. (http://www.mikeash.com/getting_answers.html); take the time to actually read it this time and stop wasting everyone's time.

chown33
Dec 12, 2011, 11:36 AM
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.

subsonix
Dec 12, 2011, 11:41 AM
i said no error! it does not detect the same string in those arrays, out put is the first sentence again.

Seems like an error to me. ;)

gnasher729
Dec 12, 2011, 12:01 PM
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.

chown33
Dec 12, 2011, 12:07 PM
Or run the code in the debugger, and look at the contents of the variables.

GorillaPaws
Dec 12, 2011, 12:11 PM
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.

marc0015
Dec 12, 2011, 01:46 PM
I see what he's done - but how did he do it?

chown33
Dec 12, 2011, 02:35 PM
^^^
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
Dec 12, 2011, 02:56 PM
The problem is the for (NSString *q in query) line just use a normal for loop with appropriate boundary conditions like this.


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

chown33
Dec 12, 2011, 03:36 PM
^^^
Effin' brilliant!! ROTFL!

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

larswik
Dec 12, 2011, 05:20 PM
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
Dec 12, 2011, 06:02 PM
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.


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
Dec 12, 2011, 09:32 PM
The problem is the for (NSString *q in query) line just use a normal for loop with appropriate boundary conditions like this.


#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
Dec 13, 2011, 02:52 AM
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.

lloyddean
Dec 13, 2011, 11:47 AM
saleh.hi.62,

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

saleh.hi.62
Dec 13, 2011, 12:04 PM
saleh.hi.62,

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

I am using arabic plus english, why ?

jared_kipe
Dec 13, 2011, 12:11 PM
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
Dec 13, 2011, 12:11 PM
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
Dec 13, 2011, 12:24 PM
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.

lloyddean
Dec 13, 2011, 12:26 PM
I am using arabic plus english, why ?

Wanted to figure out how it is you keep selecting the wrong double-quote characters 'C' uses to denote string boundaries.

jared_kipe
Dec 13, 2011, 12:29 PM
Wanted to figure out how it is you keep selecting the wrong double-quote characters 'C' uses to denote string boundaries.

I bet a lot of money, he got them from cmd+c and cmd+v.

lloyddean
Dec 13, 2011, 12:32 PM
Odds are you're correct but being as how the Roman character set is not used in his native language he may not be use to noting the difference between what is presented in PDF documents and actual 'C' code.

chown33
Dec 13, 2011, 12:51 PM
3 would have worked here.


So would this, which is a simple way to look at the value of a variable:
NSLog( @"stopList len: %d", [stopList count] );
NSLog( @"stopList: %@", stopList );

If stopList is working as desired, the expected output should be a length greater than 1, and an array of several small words. However, the actual output is something else. Something that isn't an array holding several small words. This is a good example of why it's useful to describe expected results and actual results.

It's also a good example of how running a program in the debugger and simply looking at variables can provide information on what the bug is. Unfortunately, learning to use the debugger doesn't seem to be a skill the OP is interested in.

Equally unfortunate, using NSLog() requires thinking about what variables to look at. Which requires having a plan for finding bugs. Which requires breaking the problem down and looking carefully at the smaller parts. These skills also seem to be lacking.

subsonix
Dec 13, 2011, 12:57 PM
Wanted to figure out how it is you keep selecting the wrong double-quote characters 'C' uses to denote string boundaries.

I seem to recall that MS Word and similar auto correct this by default, could be a clue perhaps.

Edit: Yep, aka smart quotes. http://en.wikipedia.org/wiki/Quotation_mark#Smart_quotes

chown33
Dec 13, 2011, 01:18 PM
unfortunately some people in this forum know what is the question & problem, but !!! since they have no idea of how solve the problem say :

I think you're wrong. I think almost everyone replying knows exactly what the problem is, and exactly how to solve it. They are intentionally not giving you the answer, because they expect you to find it for yourself.

This is called "learning to find the answer yourself" instead of "having someone else give you the answer". The first skill is very important in programming. The second one, not so much.

1-improve your english

No one said that. No one even hinted at it. They are telling you to improve your descriptions, which means they are asking you to provide complete descriptions. A complete description, even in poor English, is better than no description at all. Your first post had no description of the problem.

2-learn memory management

No one suggested that, either. I did say that you have a history of posts where the code doesn't do correct memory management. You started those posts by saying there was "no error". I was using that as an example of one possible meaning for "no error".

I was also saying that we can't tell what you mean when all you say is "no error". If you say "no compiler errors", it's clear what you mean. If you say "it doesn't crash", it's clear what you mean. If all you say is "no error", it's not clear.

3-learn debugger using
etc

guys these are not the problem !

Wrong. Not knowing how to use the debugger is exactly the problem.

You have no way to debug programs yourself. That is a serious problem. You also don't seem to have any interest in learning the debugger. Another serious problem.

If you knew how to use the debugger, you could find and fix the problem in your code yourself.

If you knew how to use NSLog to look at variables, you could also do a lot of debugging, because sometimes simply looking at the value of a variable is enough to see the reason for a bug.


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

Yes, I ran it. Yes, I know exactly what the problem is. Yes, I know exactly how to solve it. No, I'm not going to tell you exactly how to solve it. No, I'm not going to post code that shows how to solve it. Because I think it's more important that you learn how to find and fix bugs like this by yourself.

gnasher729
Dec 13, 2011, 04:41 PM
unfortunately some people in this forum know what is the question & problem, but !!! since they have no idea of how solve the problem say

I told you: Before Jared's joke program there were four or five posts that told you exactly how to find the problem. There is a common problem that beginner programmers have: They believe that their code is correct and something strange is going on that stops it from working. With experience you'll find that if your code doesn't work, chances are 99.99% that it is something you did wrong, and often some rather stupid mistake.

You seem to have an additional problem. You ask for help, but not only do you not believe that there is anything wrong with your code (which you could have found in less than a minute), but you don't believe that the replies you get have any value. Here was my very first advice: "Look at the code in a really big font. " Did you do it? No, you didn't or you would have immediately spotted the problem. The second advice by chown: "Or run the code in the debugger, and look at the contents of the variables." Did you do it? No, you didn't, or you would have found the error within a minute.

larswik
Dec 13, 2011, 05:18 PM
They are intentionally not giving you the answer, because they expect you to find it for yourself.

This is called "learning to find the answer yourself" instead of "having someone else give you the answer".

saleh.hi.62 - This is a very true statement. I have been asking questions for a while now as I learn. People here are very willing to help you learn but giving away the answer almost never happens. They will push you in the right direction so you can discover the answer yourself which means you will better understand and remember it. If they did not want to help or care they would not take the time to respond with lengthy replies to your questions.

saleh.hi.62
Dec 14, 2011, 09:57 AM
Thank you guys,

i have found the problem! here it is !

NSArray *stopList=[NSArray arrayWithObjects:@"an”, @“and”, @“by”, @“for”, @“from”, @“of”, @“the”, @“to”,@“with",nil];

when i print it out :

NSLog(@"%@",stopList);

the output is :


2011-12-14 23:51:57.242 ICrawler[3710:80f] (
"an\U201d, @\U201cand\U201d, @\U201cby\U201d, @\U201cfor\U201d, @\U201cfrom\U201d, @\U201cof\U201d, @\U201cthe\U201d, @\U201cto\U201d,@\U201cwith"
)

i got crazy with this !

gnasher729
Dec 14, 2011, 12:49 PM
Thank you guys,

i have found the problem! here it is !

i got crazy with this !

To find the problem: Set a breakpoint on the line that sets "sentence". Command-R. Step over. Look at the "sentence" variable in the debugger. Looks alright. Step over the line that sets stopList. Look at "stopList" variable in the debugger. Wonder why it shows "1 element". Right click "Print description of stoplist". See the content. Less than one minute.

Since you spent days on finding the solution instead of a minute, it is now time for you to step back and spend an hour finding out what strategies you should have used to find the solution a lot quicker. Maybe look through this thread again from the start and check where you went wrong.

saleh.hi.62
Dec 14, 2011, 01:24 PM
Right click "Print description of stoplist"
right click on stoplist variable? i did but there was no Print description !
could you plz tell me how?

admanimal
Dec 14, 2011, 02:23 PM
right click on stoplist variable? i did but there was no Print description !
could you plz tell me how?

Are you looking at it in your source code or in the debugger?

lloyddean
Dec 14, 2011, 02:45 PM
Look closely at the initializer list portion of your 'stopList' array -


@"an”
, @“and”
, @“by”
, @“for”
, @“from”
, @“of”
, @“the”
, @“to”
, @“with"
, nil


Please note the difference between:

"

and



and



While these Roman characters look similar they are NOT the same.

If you can't see the difference try taking a closer look.

A quick way to take a closer look is multiple hits of 'Command +'

Only one is a valid delimiter in marking the beginning AND end of a 'C' language zero terminated character string.

saleh.hi.62
Dec 14, 2011, 08:49 PM
Are you looking at it in your source code or in the debugger?

debuger, i am using xcode 3.2 ...

----------

Look closely at the initializer list portion of your 'stopList' array -


@"an”
, @“and”
, @“by”
, @“for”
, @“from”
, @“of”
, @“the”
, @“to”
, @“with"
, nil


Please note the difference between:

"

and



and



While these Roman characters look similar they are NOT the same.

If you can't see the difference try taking a closer look.

A quick way to take a closer look is multiple hits of 'Command +'

Only one is a valid delimiter in marking the beginning AND end of a 'C' language zero terminated character string.

Thank you so much, i wish someone could have told me earlier!

lloyddean
Dec 14, 2011, 09:26 PM
Actually we did, but I think I most directly told you several days ago.

gnasher729
Dec 15, 2011, 06:01 AM
Thank you so much, i wish someone could have told me earlier!

Let's see. Did you ever read the article that explains how to ask questions? Let's do a test: Who wrote that article? That article links to another article by a rather famous (or infamous) person, who has a very impatient attitude. What is the name of that person? And did you read any of these?

Look at the code in a really big font. Or in XCode with syntax coloring.

Or run the code in the debugger, and look at the contents of the variables.

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.

All posted on December 12th.