+- Southperry.net (https://www.southperry.net)
+-- Forum: Social (https://www.southperry.net/forumdisplay.php?fid=14)
+--- Forum: Rubik's Cube (https://www.southperry.net/forumdisplay.php?fid=58)
+--- Thread: Need help with C Programming assignment (/showthread.php?tid=19569)
Need help with C Programming assignment - Sivrat - 2009-11-23
I know i've posted asking for help before (again, thank you for those that helped me), but I'm falling behind again. I have an assignment due thanksgiving night, midnight est (so 9 pm pst) and I could use any help I could get. This one, to me at least, seems like it will take a long time for me to figure out on my own, and I supposed it might not be a bad idea to beseech SP for help this time.
assignment in spoiler, copied and pasted
Spoiler
Objective
1. To give students practice writing functions.
2. To give students practice using C strings.
3. To give students practice utilizing an array of strings (two dimensional array).
Problem: Spell Checker
Many of us have horrible spelling and would get great practical use out of a spell-checker. In this assignment, you will write a simplified version of a spell checker. In the process, you will write several of your own string functions.
Implementation Details: string functions to write
Note: Assume that all of the formal parameters for each of the functions below are lowercase alphabetic strings.
A string is a substring of another one if all of its letters appear contiguously (in the same order) in the second string. For example, bound is a substring of homebound and ire is a substring of firefly, but car is NOT a substring of camera because the letters m and e are in between the letters a and r in camera.
// Returns true if shortstr is a substring of longstr,
// and false otherwise.
int substring(char shortstr[], char longstr[]);
A string is a subsequence of another string if all the letters in the first string appear in the second string, in the same ordering, but not necessarily contiguously. For example, car is a subsequence of camera, since the letters c,a, and r appear in that order (but not contiguously) in camera. Similarly, hikes is a subsequence of chickens, but tale is NOT a subsequence of wallet because the letter t occurs AFTER the letter a in wallet.
// Returns true if shortstr is a subsequence of longstr,
// and false otherwise.
int subsequence(char shortstr[], char longstr[]);
A permutation of letters is simply a different ordering of those letters. For example, care is a permutation of race and spill is a permutation of pills, but apple is NOT a permutation of pale because the letter p appears only once instead of twice in pale. A natural consequence of this definition is that two strings can only be permutations of one another if they are both the same length.
// Returns true if string1 and string2 are permutatations
// of each other, false otherwise.
int permutation(char string1[], char string2[]);
When we are comparing two strings of equal length, we can define a term called match score which is simply the number of corresponding letters in which the two strings disagree. For example, the match score between drink and blank is 3 because the first three letters dont match. The match score between marker and master is two because letters 3 (r vs. s) and 4 (k vs. t) do not match.
// Precondition: string1 and string2 are the same length
// and only contain lowercase letters.
// Postcondition: returns the score of the match score
// between these two strings.
int matchscore(char string1[],char string2[]);
Input File Format(dictionary.txt)
The first line of the file will have a single positive integer, n (n ≤ 30000), representing the number of words in the file. The following n lines will contain one word each, in alphabetic order. All words will contain lowercase letters only and will be no longer than 29 letters long.
Here is a short sample dictionary:
5
apple
boat
car
dog
elephant
What your program should do
Your program should first read in the dictionary into a two-dimensional character array (from dictionary.txt) and print a message to the screen stating that the dictionary has been loaded.
Then your program should prompt the user to enter a word, all lowercase.
If the word is in the dictionary, you should simply print out a message stating this.
If the word is NOT in the dictionary, then you should provide a list of possible suggestions (of correctly spelled words that the user might have been trying to spell) in alphabetical order.
Here are the criteria for whether or not a real word should end up on this list:
1) If either string is a substring of the other, and the lengths of the two strings are within 2 of one another.
2) If either string is a subsequence of the other and the lengths of the two strings are within 2 of one another. (This would get rid of the car, camera case, for example.)
3) If the strings are permutations of one the other. (Only try this test if the two strings are of the same length.)
4) If the matchscore between the two strings is less than 3. (Only try this test if the two strings are of the same length.)
Continue prompting the user to enter words until they decide that they dont want to enter any more. (Thus, they will be forced to enter the first word, but after that, youll provide them a choice as to whether or not they want to enter another word to check.
References
Textbook: Chapters 8, 9, 11, 13 Notes: Lectures on functions, arrays
and strings.
Restrictions
Name the file you create and turn in spellcheck.c. Although you may use other compilers, your program must compile and run using Dev C++. Your program should include a header comment with the following information: your name, course number, section number, assignment title, and date. You should also include comments throughout your code, when appropriate. If you have any questions about this, please see a TA.
Deliverables
A single source file named spellcheck.c turned in through WebCourses.
Sample Output (with a real dictionary)
Welcome to the Spell Checker!
The dictionary has been loaded.
Please enter the word you would like checked.
flag
Great, flag is in the dictionary!
Would you like to enter another word? (yes/no)
yes
Please enter the word you would like checked.
peice
Here are the possible words you could have meant:
alice
beige
brice
deuce
fense
heince
hense
ice
juice
paine
peace
peach
peale
pease
pee
pence
perch
percy
perle
peste
pewee
pie
piece
place
poise
ponce
price
prick
pride
prime
prize
reich
seize
slice
spice
twice
voice
Would you like to enter another word? (yes/no)
yes
Please enter the word you would like checked.
independant
Here are the possible words you could have meant:
independent
Would you like to enter another word? (yes/no)
no
It deals with functions, which im not great at, and strings, which i know absolutely nothing about currently (reading lecture notes now, and textbook). Any help would be appreciated
Spoiler
heck, if someone wants to just write the code for me(preferably with comments that would show me whats going on so i can learn it myself) i'd pay them in mesos, but to a degree would rather do it myself to learn this stuff, life is just hectic and finding time to learn is getting harder.
Need help with C Programming assignment - Russt - 2009-11-23
Need help with C Programming assignment - Fiel - 2009-11-23
Luckily, the text here already says what your program should do. All of the pre-writing is done, you just have to code it! Sounds like fun to me - pre-writing is a pain in the ass.
Code:
What your program should do
Your program should first read in the dictionary into a two-dimensional character array (from dictionary.txt) and print a message to the screen stating that the dictionary has been loaded.
Then your program should prompt the user to enter a word, all lowercase.
If the word is in the dictionary, you should simply print out a message stating this.
If the word is NOT in the dictionary, then you should provide a list of possible suggestions (of correctly spelled words that the user might have been trying to spell) in alphabetical order.
Here are the criteria for whether or not a real word should end up on this list:
1) If either string is a substring of the other, and the lengths of the two strings are within 2 of one another.
2) If either string is a subsequence of the other and the lengths of the two strings are within 2 of one another. (This would get rid of the “car”, “camera” case, for example.)
3) If the strings are permutations of one the other. (Only try this test if the two strings are of the same length.)
4) If the matchscore between the two strings is less than 3. (Only try this test if the two strings are of the same length.)
Continue prompting the user to enter words until they decide that they don’t want to enter any more. (Thus, they will be forced to enter the first word, but after that, you’ll provide them a choice as to whether or not they want to enter another word to check.
This bit of text right here is telling you exactly what your main function should look like. Let's have a look
Code:
//PSEUDOCODE
int main(void)
{
//Your program should first read in the dictionary into a two-dimensional character
//array (from dictionary.txt) and
//print a message to the screen stating that the dictionary has been loaded.
//Then the user should be prompted for the word. Afterwards...
//"(Thus, they will be forced to enter the first word, but after that,
//you’ll provide them a choice as to whether or not they want to enter
//another word to check."
//This sounds like a do-while statement!
do
{
//"Then your program should prompt the user to enter a word, all lowercase."
print "Please enter a word all in lowercase"
UserInput = GetUserInput()
//"If the word is in the dictionary, you should simply print out a message stating this."
if(IfInDict(UserInput))
{
print "Great, %s is in the dictionary!", UserInput
}
//"If the word is NOT in the dictionary, then you should
//provide a list of possible suggestions in alphabetical order.
else
{
print "Here are a list of words you could have meant"
RunThroughDictionary(UserInput) //Gets all of the new words and prints them
}
//Continue prompting the user to enter words until they decide
//that they don’t want to enter any more
UserInput = GetUserInput("Would you like to do another word?")
}while(UserInput == 'y' || UserInput == 'Y');
return 0;
}
Need help with C Programming assignment - Russt - 2009-11-23
Oh. Did I misinterpret... are the functions written for you? -blink-
Edit: Nevermind.
"In the process, you will write several of your own string functions."
Fiel did provide what you should do about the main program though, which I completely neglected. That actually works out nicely for you.
Need help with C Programming assignment - Fiel - 2009-11-23
No, they aren't written for you. You have to write them. I was talking about main, you were talking about the other functions.
Need help with C Programming assignment - Sivrat - 2009-11-23
thank you both, I'm slowly catching up with notes and stuff, as well.
Need help with C Programming assignment - Sivrat - 2009-11-24
ok i'm pretty sure i got the code written for the substring and subsequence done, but am somewhat unsure of how to do the permutation fully. This is what i have so far
Code:
int permutation(char string1[], char string2[]);
{
int i, j;
the thing is i cant figure out where/how to phrase the return true statement. Also, since i havent tried it i may as well check, would the break statement exit out of both loops? or only the one?
and is string1[i] really a boolean expression to denote when its not null? or do i have to say like string[i] != '\0' ?
here are the other functions i had, if you care to double check. I havent debugged them yet.
substring
Spoiler
Code:
int substring(char shortstr[], char longstr[])
{
int i, j;
for (i = 0; longstr[i]; i++){
for (j=0; shortstr[j]; j++) {
if longstr[i+j] != shortstr[j]
break;
if (shortstr[j] == '\0')
return true;
}
}
return false;
}
subsequence
Spoiler
Code:
int subsequence(char shortstr[], char longstr[]);
{
int i, j;
j = 0;
for (i = 0; longstr[i]; i++) {
if (longstr[i] == shortstr[j])
j++;
}
if (j == '\0')
return true;
else
return false;
}
}
Need help with C Programming assignment - Russt - 2009-11-24
Watch your brackets, make sure they go around what you want them to. And the indentation's a bit messy; fixing that up should help with the brackets too.
Break statements only break the innermost loop.
"and is string1[i] really a boolean expression to denote when its not null? or do i have to say like string[i] != '\0' ?"
I'm pretty sure that a loop condition (and if for that matter) is treated as false if it is 0, and true otherwise. So if the character is '\0', it'll be treated as false, otherwise it's true.
But yes; the "clearer" way to do it is string[i] != '\0'.
Need help with C Programming assignment - Sivrat - 2009-11-25
thank you russt.
I'm attempting to test parts of it now, and Dev-C++ is giving me an error on the matchscore function you did, saying
"[Warning] passing arg 1 of `matchscore' makes pointer from integer without a cast"
and
"[Warning] passing arg 2 of `matchscore' makes pointer from integer without a cast"
Need help with C Programming assignment - Russt - 2009-11-25
Are you passing it a char[] (i.e. string)? Sounds like you're putting numbers into it.
Need help with C Programming assignment - Fiel - 2009-11-25
We need to see code to really help you out.
Need help with C Programming assignment - Sivrat - 2009-11-25
this is what i was doing to try to test matchscore
Code:
int main(){
int matchscore(char string1[], char string2[]);
char first, second;
int length1, length2;
int score;
printf("Enter the first word\n");
scanf("%s", &first);
printf("Enter the second word\n");
scanf("%s", &second);
if (strlen(first) == strlen(second))
printf("The matchcount is %d\n", matchscore(first, second);
else
printf("The words are different lenghts\n");
system("PAUSE");
return 0;
}
int matchscore(char string1[], char string2[])
{
int count = 0;
int i;
for (i = 0; string1[i] != '\0' && string2[i] != '\0' ; i++)
{
if (string1[i] != string2[i])
count++;
}
return count;
}
Need help with C Programming assignment - Fiel - 2009-11-25
"char first, second;"
That declares a variable named "first" which is assigned one byte. You need it to be a pointer.
"char* first, second;"
This makes "first" be a memory location for an array of characters (whether string or actual array - in C these are the same concept) somewhere else in memory. The way it was before was a reference to one byte of memory.
Also, because it's a pointer, you don't need the "&" in the scanf function call because your char* is already a pointer.
scanf("%s", first);
Also, instead of using "system("PAUSE");", try running your programs from the command prompt.
Finally, most C Programming courses allow functions to be global in scope, so you can move the function prototype for matchscore into the global section of the program.
" printf("The matchcount is %d\n", matchscore(first, second);" << missing a parenthesis there on the right?
Need help with C Programming assignment - Nikkey - 2009-11-25
Need help with C Programming assignment - Russt - 2009-11-25
You have to declare a function before you use it, so for DS's code to work, you'd have to either move the matchscore function above main, or declare its function prototype ("int matchscore(char[], char[])") above main.
Need help with C Programming assignment - Sivrat - 2009-11-25
i got matchscore and subsequence working, having trouble still on permutation.
And realization struck when i realized that... for the point of the program... substring doesnt even matter because anything that would be a substring is also subsequence...
Need help with C Programming assignment - Nikkey - 2009-11-25
Sivrat Wrote:i got matchscore and subsequence working, having trouble still on permutation.
And realization struck when i realized that... for the point of the program... substring doesnt even matter because anything that would be a substring is also subsequence...
But a subsequence is not necessarily a substring.
For permutations, think of this: Sort the arrays. If they print the same after sorted, then a is a permutation of b and otherwise.
Need help with C Programming assignment - fawrh - 2009-11-25
something that may be of use is you need to make sure that you are carful with initalizeing the strings that you pass into your functions, if they are not, the for loop conditions may never fail as the data in of the string may not be 0 therefor it will continue to iterate. This may also be a bad thing if someone types in 0 in the string, as sting[i] will == 0 therefor the for loop will break. As far as your break statement goes, it is eaiser to use a bool and set it to false, then break with an if([I]boo) break;
Just some food for thought... Error handleing is your friend.
Need help with C Programming assignment - Sivrat - 2009-11-25
fawrh Wrote:something that may be of use is you need to make sure that you are carful with initalizeing the strings that you pass into your functions, if they are not, the for loop conditions may never fail as the data in of the string may not be 0 therefor it will continue to iterate. This may also be a bad thing if someone types in 0 in the string, as sting[i] will == 0 therefor the for loop will break. As far as your break statement goes, it is eaiser to use a bool and set it to false, then break with an if([I]boo) break;
Just some food for thought... Error handleing is your friend.
i know, thats why the boolean expressions are [i] == '\0', which is the void statement for strings.
and @ devil's sunrise, even if a subsequence isnt necessarily and substring, it matters not. The test is to figure out if the word is in the dictionary.txt file, spelled correctly. If it isnt i'm to offer suggestions that either make it a matchscore less then 3 with same wordlength, a permutation with same wordlength, or subsequence or a substring with wordlength within 2. If it would test true for substring, then even if i deleted that test completely it would still test true for subsequence, making it somewhat unnecessary.
But the sorting the permutations seems like not a bad idea, just think there is a somewhat easier way, but since i can't figure out how to do loops to test correctly, i suppose that's a workaround i can try.
Need help with C Programming assignment - Nikkey - 2009-11-26
Sivrat Wrote:i know, thats why the boolean expressions are [i] == '\0', which is the void statement for strings.
and @ devil's sunrise, even if a subsequence isnt necessarily and substring, it matters not. The test is to figure out if the word is in the dictionary.txt file, spelled correctly. If it isnt i'm to offer suggestions that either make it a matchscore less then 3 with same wordlength, a permutation with same wordlength, or subsequence or a substring with wordlength within 2. If it would test true for substring, then even if i deleted that test completely it would still test true for subsequence, making it somewhat unnecessary.
But the sorting the permutations seems like not a bad idea, just think there is a somewhat easier way, but since i can't figure out how to do loops to test correctly, i suppose that's a workaround i can try.
There are some other ways I can think of right now, both seem to be possibly faster:
Set up two arrays with 256 slots.
Whenever "a" is found in any string, increase the "a"-value for that string's array. Do that for all values. When null is found, check that the two arrays you've made is equal.
Set up a list or a hash-array. Repeat the same thing as written above.