Word sorting program

  • (10 Pages)
  • +
  • « First
  • 2
  • 3
  • 4
  • 5
  • 6
  • Last »

140 Replies - 7087 Views - Last Post: 04 November 2013 - 03:14 PM Rate Topic: -----

#46 lewm  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 160
  • Joined: 29-March 13

Re: Word sorting program

Posted 05 September 2013 - 11:15 AM

Ok i tried this code it worked for 1 word but when i tried it for 2 words it crashed:
int main(void)
{
    FILE *p;
    char *parray[2], word[50];

    p=fopen("words","r");
    fscanf(p,"%s",&word);
    parray[0]=malloc(sizeof(word));
    parray[0]=word;
    fscanf(p,"%s",&word);
    parray[1]=malloc(sizeof(word));
    parray[1]=word;
    printf("%s %s",*parray[0], *parray[1]);
    free(parray[0]);
    free(parray[1]);
    fclose(p);

    return 0;
}

This post has been edited by lewm: 05 September 2013 - 11:42 AM

Was This Post Helpful? 0
  • +
  • -

#47 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Word sorting program

Posted 05 September 2013 - 11:53 AM

View Postlewm, on 05 September 2013 - 11:02 AM, said:

I dont mean to be rude but i am still no closer to understanding how to get them ALL into memory if i wanted to.
I know a little about pointers and a little about malloc but ive never used them together.
Im using a 64bit system with 8Gb ram.
Is there a way around using malloc other than splitting them up into seperate files could i use a 3 dimentional array or something?
Could someone help me understand how i would read them into memory using pointers and malloc.
The code that adak provided looks very useful and is much appreciated but id rather not use the system to sort id rather do it myself if possible.

We can't tell you if you can get all your words into memory or not. We would have to have the actual word list and see how long each word was, etc.

Understand, there are limits to what you can hold in memory, at one time. 200,000 words MIGHT be doable, and we've shown you one way to do that, with the very large global char array[WORDNUM][MAXLETTERS], before main().

#include <stdio.h>
#define WORDNUM 200000
#define MAXLETTERS 29

char words[WORDNUM][MAXLETTERS];

int main(void) {
   int i;
   for(i=0;i<10;i++)
      printf("%d\n",i);
   //etc.
   return 0;
}





Did you try that, on your system? I can run that on my Windows 7 64 bit but using a 32 bit console project (my normal type). My upper limit was 74,000,000+ words (2147483647 bytes), with this tiny test program, above.

The second way to do this is to use an array of pointers to strings. That can save memory, but it's a bit more complicated.

So both ways will work, AND both ways will also fail at some point, if you keep adding more and more words.

In my opinion, you can use the very large global array for your words - it won't make the programmers jump for joy with it's elegance, but it seems well suited for your use, in this particular case.
Was This Post Helpful? 0
  • +
  • -

#48 lewm  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 160
  • Joined: 29-March 13

Re: Word sorting program

Posted 05 September 2013 - 12:06 PM

Thank you for your advice, but jim has givem me a lot of good advice (some of which i still dont follow) and he said dont use globals on more than one occasion ive also read it elsewhere so although that will work im trying to do it by the book so to speak.

I keep hearing that the best way to do it is to use an array of pointers so thats what im focusing on at the moment (i have a one track mind).

So is the most recent code on the right track? I know that most methods will fail if the amount of words exceed the computer memory.

Thanks for your patience.
Was This Post Helpful? 0
  • +
  • -

#49 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Word sorting program

Posted 05 September 2013 - 12:11 PM

If you were close, and you wanted to learn to use malloc(), then I would have recommended THAT way of doing it for you.

Good luck.

This post has been edited by Adak: 05 September 2013 - 12:14 PM

Was This Post Helpful? 0
  • +
  • -

#50 lewm  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 160
  • Joined: 29-March 13

Re: Word sorting program

Posted 05 September 2013 - 12:15 PM

View PostAdak, on 05 September 2013 - 12:11 PM, said:

To be honest - no. Not even close. If you were, and you wanted to learn to use malloc(), then I would have recommended THAT way of doing it for you.

Now that i know this is probably the best way to do it i do want to learn this way.

So :) could you teach me how to read in one word using a pointer and malloc i should be able to take it from there, maybe.
Was This Post Helpful? 0
  • +
  • -

#51 lewm  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 160
  • Joined: 29-March 13

Re: Word sorting program

Posted 05 September 2013 - 12:23 PM

View PostAdak, on 05 September 2013 - 12:11 PM, said:

If you were close, and you wanted to learn to use malloc(), then I would have recommended THAT way of doing it for you.

Good luck.

I took on board what you said and i was going to use that method, then i read other posts and read again you shouldnt use globals, so i thought it would be 'cheating' doing it that way and set my sights on using pointers.

Thanks for your input though it is very much appreciated. :)
Was This Post Helpful? 0
  • +
  • -

#52 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1344
  • View blog
  • Posts: 4,608
  • Joined: 19-February 09

Re: Word sorting program

Posted 05 September 2013 - 12:29 PM

Hi, it is eventually less confusing to use well-named variables.

int main()
{
    FILE *infile;
    char *strparray[2], buffer[50];




When reading to an array the array name decays to an address so the ampersand(&) is not required.

    infile = fopen("words", "r");
    fscanf(infile, "%s", buffer);




sizeof(word) probably gives you the size of a pointer so use strlen and add 1 for a null character.

    strparray[0] = malloc( strlen(buffer) + 1);




Instead of trying to assign arrays you will need to use strcpy.

/* parray[0]=word; */
strcpy(strparray[0], buffer);   



The memory allocation (malloc) may fail and return null.
Was This Post Helpful? 1
  • +
  • -

#53 lewm  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 160
  • Joined: 29-March 13

Re: Word sorting program

Posted 05 September 2013 - 12:49 PM

Thank you #define how do i free the memory?

int main(void)
{
    FILE *p;
    char *strparray[2], buffer[50];

    p=fopen("buffers","r");
    fscanf(p,"%s",buffer);
    strparray[0]=malloc(strlen(buffer)+1);
    strcpy(strparray[0], buffer);
    fscanf(p,"%s",buffer);
    strparray[1]=malloc(strlen(buffer)+1);
    strcpy(strparray[1], buffer);
    printf("%s %s",*strparray[0], *strparray[1]);
    free(strparray[0]);
    free(strparray[1]);

    fclose(p);

    return 0;
}

Still crashes?

Free like this? strparray[0]=0; ?
Was This Post Helpful? 0
  • +
  • -

#54 lewm  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 160
  • Joined: 29-March 13

Re: Word sorting program

Posted 05 September 2013 - 01:02 PM

I think the problem might be with this line?
printf("%s %s",*strparray[0], *strparray[1]);

Was This Post Helpful? 0
  • +
  • -

#55 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1344
  • View blog
  • Posts: 4,608
  • Joined: 19-February 09

Re: Word sorting program

Posted 05 September 2013 - 01:31 PM

Yep, you don't need the * operator.

printf("%s %s", strparray[0], strparray[1]);




Your freeing here is correct;

14	    free(strparray[0]);
15	    free(strparray[1]);




A character array and a char pointer are similar in some respects:

  char  str_one[] = "first";
  char *str_two   = "second";

  printf("%s \n", str_one);
  printf("%s \n", str_two);


Was This Post Helpful? 1
  • +
  • -

#56 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3572
  • View blog
  • Posts: 11,107
  • Joined: 05-May 12

Re: Word sorting program

Posted 06 September 2013 - 06:12 AM

Hmmmm... With code that looks like:
int main()
{
:
    char *strparray[MAX_NUMBER_OF_WORDS];
:
}



In a 32-bit program you would still have 4 * MAX_NUMBER_OF_WORDS sitting on the stack. It'll be 8 * MAX_NUMBER_OF_WORDS for a 64-bit program. Yes, that's a lot better than 50 * MAX_NUMBER_OF_WORDS, but you've still got a bunch of memory being eaten up on the stack.

My recommendation is this:
int main()
{
:
    char **wordPointerArray;
    wordPointerArray = malloc(MAX_NUMBER_OF_WORDS * sizeof(char *));

    int countOfWords = 0;
    while(read word from file)
    {
        wordPointerArray[countOfWords] = malloc((strlen(word) + 1) * sizeof(char));
        strcpy(wordPointerArray[countOfWords], word);
        countOfWords++;
    }
:
}


Was This Post Helpful? 1
  • +
  • -

#57 lewm  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 160
  • Joined: 29-March 13

Re: Word sorting program

Posted 06 September 2013 - 07:49 AM

Im going to have to study that one I don't quite understand what you have done there with **wordpointerarray.

Well I got it all working like this:
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include <math.h>
#include <string.h>
#include <conio.h>
#include <dos.h>
#include <time.h>

int count_words(void);

int main(void)
{
    FILE *original_file, *no_duplicate_output_file;
    char word_buffer[100];
    int number_of_words=0, i=0, j=0, duplicate_flag=0;
    number_of_words=count_words();
    char *str_pointer_array[number_of_words+1];

    if((original_file=fopen("Words","r"))==0)
        return 1;
    do
    {
        fscanf(original_file,"%s",word_buffer);
        str_pointer_array[i]=malloc(strlen(word_buffer)+1);
        strcpy(str_pointer_array[i], word_buffer);
        i++;
    }while(i<=number_of_words);
    fclose(original_file);
    if((no_duplicate_output_file=fopen("NoDupWords","w"))==0)
       return 1;
    for(i=0;i<number_of_words;i++)
    {
        printf("%d\n",i);
        strcpy(word_buffer,str_pointer_array[i]);
        for(j=i+1;j<number_of_words;j++)
        {
            if((strcmp(word_buffer,str_pointer_array[j]))==0)
            {
                duplicate_flag=1;
            }
        }
        if(!duplicate_flag)
        {
            fprintf(no_duplicate_output_file, "%s\n", str_pointer_array[i]);
        }
        duplicate_flag=0;
    }
    fclose(no_duplicate_output_file);
    for(i=0;i<=number_of_words;i++)
    {
        free(str_pointer_array[i]);
    }
    return 0;
}

int count_words(void)
{
    FILE *original_file;
    char word_buffer[50];
    int word_count=0;

    if((original_file=fopen("Words","r"))==0)
        exit(1);
    do
    {
        fscanf(original_file,"%s",word_buffer);
        if(word_buffer[0]!='*')
            word_count++;
    }while(word_buffer[0]!='*');
    fclose(original_file);
    return(word_count);
}

This post has been edited by lewm: 06 September 2013 - 07:50 AM

Was This Post Helpful? 0
  • +
  • -

#58 lewm  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 160
  • Joined: 29-March 13

Re: Word sorting program

Posted 06 September 2013 - 07:54 AM

Is **pointer a pointer to a pointer?
Was This Post Helpful? 0
  • +
  • -

#59 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 659
  • View blog
  • Posts: 2,270
  • Joined: 31-December 10

Re: Word sorting program

Posted 06 September 2013 - 08:04 AM

View Postlewm, on 06 September 2013 - 10:54 AM, said:

Is **pointer a pointer to a pointer?

Yes.
Was This Post Helpful? 0
  • +
  • -

#60 jimblumberg  Icon User is offline

  • member icon


Reputation: 4068
  • View blog
  • Posts: 12,554
  • Joined: 25-December 09

Re: Word sorting program

Posted 06 September 2013 - 08:30 AM

Why are you mixing VLA and malloc for the same variable?

    number_of_words=count_words();
    char *str_pointer_array[number_of_words+1];
...
        str_pointer_array[i]=malloc(strlen(word_buffer)+1);



That is a very dangerous way of managing your memory.

Also I thought you wanted to add words to your dictionary. Where are you adding the words? In this code you just seem to be checking for duplicates in one file.


Jim
Was This Post Helpful? 0
  • +
  • -

  • (10 Pages)
  • +
  • « First
  • 2
  • 3
  • 4
  • 5
  • 6
  • Last »