Hey everyone! For this HW I need to compare permutations to a dictionary file with binary search. I got the permutations down and I stored all the different permutations in an array. Now for binary search I need to know the size of text document, the text document that is given has the 1st line as the number of words so example:
10
cool
hi
test
sweet
coolio
cat
dog
town
car
ear
My only question is how do I get the loop to store the # as the size of the array of dictionary words? Right now I just have it reading in like this:
for(i=0; i<filelength; i++)
{
fscanf(ifp, "%s", words[i]);
i++;
}
It goes till the file ends, for this HW it specifically states we must scan the int in as the array size.
Scanning in text file 1st line is # of words
Page 1 of 18 Replies - 134 Views - Last Post: 10 October 2012 - 05:45 AM
Replies To: Scanning in text file 1st line is # of words
#2
Re: Scanning in text file 1st line is # of words
Posted 09 October 2012 - 11:39 PM
Something like
fscanf(ifp, "%d", &numWords);
for ( i = 0 ; i < numWords ; i++ ) {
fscanf(ifp, "%s", words[i]);
}
#3
Re: Scanning in text file 1st line is # of words
Posted 09 October 2012 - 11:39 PM
After you retrieve the int then you should use malloc/free combination to create the array to hold the data.
Jim
Jim
#4
Re: Scanning in text file 1st line is # of words
Posted 09 October 2012 - 11:41 PM
jimblumberg, on 09 October 2012 - 11:39 PM, said:
After you retrieve the int then you should use malloc/free combination to create the array to hold the data.
Jim
Jim
Haha that's actually exactly what she wanted to do. I need to read up on malloc more. She said we need to allocate space for the dictionary array. This was weird because every other assignment I just used the simple for loop to scan in all the words. Could you give me any references that would better explain how to use malloc in this situation?
#6
#7
Re: Scanning in text file 1st line is # of words
Posted 09 October 2012 - 11:54 PM
Show your code.
Also since this is a character you don't need the multiplication:
And don't forget to include <stdlib.h>
Jim
Also since this is a character you don't need the multiplication:
char *words = malloc(numWords);
And don't forget to include <stdlib.h>
Jim
#8
Re: Scanning in text file 1st line is # of words
Posted 10 October 2012 - 12:58 AM
> char *words = malloc(numWords*sizeof(char));
But this is only sufficient for a single word up to however many characters numWords is.
If you really want an array of words, you need something like
char (*words)[100] = malloc( numWords * sizeof(*words) );
Which is the malloc'ed equivalent of saying
char words[numWords][100];
But this is only sufficient for a single word up to however many characters numWords is.
If you really want an array of words, you need something like
char (*words)[100] = malloc( numWords * sizeof(*words) );
Which is the malloc'ed equivalent of saying
char words[numWords][100];
#9
Re: Scanning in text file 1st line is # of words
Posted 10 October 2012 - 05:45 AM
Salem_c, on 10 October 2012 - 12:58 AM, said:
> char *words = malloc(numWords*sizeof(char));
But this is only sufficient for a single word up to however many characters numWords is.
If you really want an array of words, you need something like
char (*words)[100] = malloc( numWords * sizeof(*words) );
Which is the malloc'ed equivalent of saying
char words[numWords][100];
But this is only sufficient for a single word up to however many characters numWords is.
If you really want an array of words, you need something like
char (*words)[100] = malloc( numWords * sizeof(*words) );
Which is the malloc'ed equivalent of saying
char words[numWords][100];
This helped me so much,so the pointer in (*words) pointed to the (*words). You where right I was just allocating memory for the one word the size of the int. thats what was causing the error. I would post my whole code here but sadly that is not aloud and I would get an F for this assignment.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|