Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with C Programming assignment
#1
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

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
Reply
#2
Reply
#3
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.

  TwoDCharArray = LoadFile("Dictionary.txt");
  print "File 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;
}
Reply
#4
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. Biggrin
Reply
#5
No, they aren't written for you. You have to write them. I was talking about main, you were talking about the other functions.
Reply
#6
thank you both, I'm slowly catching up with notes and stuff, as well.
Reply
#7
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;

for(i=0; string1[i]; i++;) {
         for(j=0; string2[j]; j++;) {
         if (string1[i] == string2[j])
            break;
         if (string[j] == '\0')
            return false;
            }
            }

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

subsequence
 Spoiler
Reply
#8
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'.
Reply
#9
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"
Reply
#10
Are you passing it a char[] (i.e. string)? Sounds like you're putting numbers into it.
Reply
#11
We need to see code to really help you out.
Reply
#12
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;
}
Reply
#13
"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?
Reply
#14
Reply
#15
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.
Reply
#16
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...
Reply
#17
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.
Reply
#18
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.
Reply
#19
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.
Reply
#20
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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)