|
Post by countlieberkuhn on Apr 12, 2012 15:32:50 GMT -8
Are you feeling helpful and generous Bob? (or anyone who knows some programming) It's programming related. I'm pretty stuck on a small aspect of my coursework for uni and I'm not able to find the answers online (might be searching the wrong thing). Soooo, I've decided to ask you, in the hopes that you may know the answer and are feeling nice! The language is standard C. The question is pretty simple but I'll give some background anyway. I've been tasked with making a spellchecker program that reads in words from an external dictionary text file, and then for each word in a given piece of text, checks to see if the word matches something in the dictionary database. For the first part of the assignment I simply had to scan all of these words into the program, which I have stored as a 2d character array, because C is lame and doesn't have proper string variables. This array is stored in the main method. In order to get the best marks, I need to have a recursive, binary search method for doing this, so that it doesn't take forever to work. Most of this binary search function has been made. However, the one problem is that for each time the function calls itself, I can't have the new 'mid' point of the binary search retrieve the word from the character array in the main function. So the question simply is: how can I copy variable data from outside of the current method into the current one? I'm thinking it's something like strcpy(x, main().wordList );
where x is the variable within the binary search method, and wordList would retrieve the i'th word in the dictionary. So, wordlist[3] would be aardvark or something. However, it doesn't seem to like me doing main().wordList. But it's probably something close to that that I'm just not working out XD
Anyway, if you know how to read data from variables from outside the method in C, I'd be very grateful if you told me Cheers, if you've read this far. I can send you the code I've got if it'll help. I'm only doing it in notepad++.
|
|
|
Post by Bob on Apr 12, 2012 18:02:41 GMT -8
Hmm.... That's a good question and C is not my forte. Do you have to copy the file in, or can it continue to read it? I'm just thinking if each word is on its new line, then you can do a recursive "if a then check next letter" and match things up that way, call the function recursively and return a "no such word" if it reaches the end of the file with no exact match.
As far as copying the words in, I'm not great with arrays (vectors ftw!), but could you just make a for loop (or a while loop since I'm thinking for loops are C++) that sets array elements equal to what's taken in? Like, taken in characters until you hit an endline, then call that x, then say wordList = x?
Sorry, can't help a ton with this since I'm not great with C, so not great with chars or arrays. I've done a problem like this, but not with regular C, plus it had a suck runtime. I wonder though, to optimize runtime, could you put your words into different lists based on their first letters? Might be a decent option to at least pack in a through l to one list, then m through z in another. But that's about as much help as I can be to get you started. Good luck!
EDIT: Yeah, if you want to upload your code or something, I can glance through it and see what I can help with. Hard to help blindly.
EDIT2: And as my wife pointed out, don't arrays only hold a single char in them? So like, don't you have to make an array of arrays if you want each array to have the whole word? I could be wrong, but you might need to do something like that to store entire words.
|
|
|
Post by countlieberkuhn on Apr 12, 2012 18:27:03 GMT -8
I've emailed you the code from my tetramuse email account, if it helps. I've commented it so hopefully you should be able to work out what's going on without knowing much code. As for the arrays, your wife is indeed correct, I've had to use a 2d array for this. So it's wordList[][], where the first square brackets stores the index for the word (stored as an integer value), and the second square brackets stores the sequence of characters that make up the word. It's pretty much an array of arrays, but uses less space. Unfortunately the assignment requires I use arrays, although luckily I'm good with them. Which is just as well as I have no idea what a vector is As I said, the issue isn't so much the searching aspect, as just being able to access the array from outside of the method. I've got most of the binary search code already worked out. If it makes it easier, just imagine I'm trying to access a single integer variable from outside of the method. The fact that it's an array is probably irrelevant in terms of what I'm trying to figure out. But no worries if you can't help, I was just going out on a limb XD
|
|
|
Post by Bob on Apr 12, 2012 18:38:43 GMT -8
Yeah, I checked it out and I can't help you. I don't remember arrays and their functions well enough. And don't get me started on pointers... I kinda wanna retake a few programming classes now that I've had more practice. Same with web design. I want to go back to college Vectors are awesome. They're arrays of strings, basically. They have a lot of great features built in that make removing them easy and getting info from them easy. Very easy. But then it makes it hard to learn arrays after knowing vectors because you're like "wait, why use these again? We have vectors...."
|
|
|
Post by countlieberkuhn on Apr 12, 2012 18:52:14 GMT -8
Haha, well I guess it's good I understand arrays before I learn the good way of doing things And yeah, pointers are bullshit. The last assignment had a real nasty question involving pointers and dynamic memory allocation and all that painful stuff. It was pretty much put there just so the lecturer could see who the real C geniuses were. I, like many people, was evidently not a C genius XD Thanks for giving it a look though!
|
|
|
Post by Bob on Apr 12, 2012 19:34:33 GMT -8
Yeah, I remember my pointers assignment. The computers in the lab had to closely be monitored because of memory leaks. Lots of system restarts involved since it's a shared connection and all. We were told to make sure we had plenty of time for the assignment and to be prepared for said restarts. Luckily, I didn't cause any large memory leaks, though the assignment still took a hell of a lot of time. I'll always look at any assignments! I may not be the greatest programmer, but I still love trying to follow the logic and find the mistakes. Must be my inner math teacher! Now if only I knew how to program better. Someday, I'LL be the one asking YOU the questions!
|
|
|
Post by countlieberkuhn on Apr 12, 2012 20:46:33 GMT -8
Haha I dunno about that XD But I HAS MADE PROGRESS! And now I'm feeling a little silly about that previous assignment, because I've just gone and solved the problem by using dynamic memory allocation and pointers to create a global 2d array from inside the main routine, which can then be accessed from the binary search method. Even though I copied most of it from the internet and just adapted it slightly, I still feel like a boss Pointers have progressed to 'reasonably terrifying' from 'utterly terrifying' in my books.
|
|
|
Post by Bob on Apr 12, 2012 20:48:21 GMT -8
Woo!
You know, sometimes just adapting existing code is enough to show you know what you're doing. Also, by doing so, I often learn how things work. I initially used some Javascript for my website and didn't know how to use it, so it was just a copy/paste, but now I know how to alter it a bit more. Most of that came from playing with the existing code.
GO THIEVERY!
|
|
|
Post by countlieberkuhn on Apr 12, 2012 21:02:42 GMT -8
Yeah, I think it's probably a good way to start at least. Although I'll still be kinda scared of pointers and dynamic memory til I can code them from scratch without causing a massive memory leak
|
|
|
Post by Bob on Apr 12, 2012 22:09:54 GMT -8
Yeah, likewise. I mean, all you have to remember to do is free them, but I know I always got confused on when to use the *, the &, and nothing. So strange, the pointers.
|
|
|
Post by countlieberkuhn on Apr 12, 2012 22:44:52 GMT -8
From what I gather, * is used when declaring a pointer, and when allocating memory to the pointer. As for the &, it comes and goes at it pleases
|
|
|
Post by Bob on Apr 12, 2012 23:10:55 GMT -8
Heh. I THINK the & is used to de-allocate (un-allocate?) the memory. To free it up, basically. So that's the thing that you MUST do before your program ends or you'll have memory leaks (memory that thinks it still has a job but actually doesn't). I know whenever I did a pointer program, our teacher told us how to make a function that frees the memory and had us use that at the end to make sure our program officially ended.
|
|
|
Post by Fleck on Apr 13, 2012 5:58:13 GMT -8
All right, Count. I was going to stay out of this one, but I can't sit back and watch anymore. Bob is sitting here, filling your head with lies and biscuits, and I want to get you back on the path. You see, I'm the real programming genius here. I once tossed Bob a pamphlet on how to protect your email password from hackers and now he thinks he's the computer genius. I let him continue with his delusion because it's adorable, but now he's running to risk of doing serious harm to your computer.
What your gonna want to do is re-allocate your megadrives into your subsonic particle reversers. Make sure to stack your line drives in alphabetical order or else your computer will think you're attempting to punt, and you'll get a five yard penalty. Hollow out your mainframe bisectors with the Egyptian Cross method to ensure that your computer is in the proper jettison set, before you go any further. Once that's finished, you can write the teletype argumentative chips into your phasor attachments. Open your shock gamma connectors folder and rewrite the file entitled "Artemis Solder Relocators" to make it comply with OSHA regulations. The program should take care of itself for a little bit, but will inevitably crash when it hits the cell wall. It needs forty three extra units of cytoplasm from your dual screen lite. Write a sub-program in Word that will give out the extra cytoplasm and hopefully the program will finish itself without any errors. If anything goes wrong, Google "Blue Waffle Drivers" and copy/paste the first thing you see into your ultra-files to resolve the error.
I hope that helps.
|
|
|
Post by countlieberkuhn on Apr 13, 2012 8:08:38 GMT -8
I ain't falling for your tricks, I've seen the blue waffle pic before!
The rest seems legit.
|
|
|
Post by Fleck on Apr 13, 2012 9:56:02 GMT -8
I should have had you go to Shed.mov, because then if you'd gone and actually googled it, you would have ended up somewhere awesome instead of somewhere disgusting. If you or any other MLP fans haven't seen Shed.mov or Dress.mov or Apple.mov, then go to Youtube and correct this injustice!
|
|