Thread: Help me please :-)

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    33

    Help me please :-)

    Ok, basically this program is supposed to put 25 random numbers into a link list, then put them in order, print them out in order, then print the sum and average, it allows me to compile it, but when i run it, it says "memory fault (coredump)"

    Here is the code:
    Code:
      #include<stdio.h>
    #include<time.h>
    #include<stdlib.h>
    
    struct xnode
    {
      int num;
      struct xnode *next;
    };
    
    struct xnode *hptr;
    struct xnode *cptr;
    
    void addn();
    void order();
    void display();
    void del();
    
    void main()
    {
      float ave;
      int i, x, sum;
      srand(time(NULL));
      hptr->num = rand() % 99 + 1;
      hptr->next = NULL;
      for (i = 0; i <= 25; i++)
        {
          x = rand() % 99 + 1;
          addn(x);
        }
      while(cptr->next != NULL)
        {
          cptr = hptr;
          sum += cptr->num;
          cptr = cptr->next;
        }
      ave = sum / 25;
      printf("The sum of all numbers is %d\n", sum);
      printf("The average number is %.2f\n", ave);
      order();
      display();
      del();
    }
    
    void addn(int x)
    {
      struct xnode *nodeptr;
      nodeptr = malloc(sizeof(struct xnode));
      nodeptr->num = x;
      nodeptr->next = NULL;
      cptr->next = nodeptr;
    }
    
    void order()
    {
      int temp;
      while (cptr->next !=NULL)
        {
          if (cptr->num > hptr->num)
            {
              temp = hptr->num;
              hptr->num = cptr->num;
              cptr->num = temp;
            }
          cptr = cptr->next;
        }
    }
    
    void display()
    {
      int e;
      while(cptr!=NULL)
        {
          printf("Node %d =  %d\n", e, cptr->num);
          cptr = cptr->next;
          e++;
        }
    }
    
    void del()
    {
      struct xnode *dptr;
      while(cptr != NULL)
        {
          dptr = cptr->next;
          free(cptr);
          cptr=dptr;
        }
    }
    Thanks in advance

  2. #2
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Use some printf()'s to figure where the error happens. What way there might be an issue with the malloc, or free, or some other access to memory causes an error. Also, your addn() function prototype does not declare that it will receive any variables, you need to change the prototype so it will.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    hptr pointer in main() is NULL, memory needs to be allocated before using.

    hptr->num = rand() % 99 + 1;

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    33
    thanks, well i did that

    Code:
      printf("0000\n");
      hptr->num = rand() % 99 + 1;
      hptr->next = NULL;
      printf("111111\n");

    and it didnt get to the line 111111

    so what am i doing wrong in those 2 lines?

  5. #5
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Did you do what scarlet said, i missed that with the quick overview i did. He is right, you try to assign the member num, and the member next of a struct that hptr points to, but there is no struct, no memory has been allocated yet for that. You need to allocate atleast 1, or declare atleast a biginning struct in the code before you can assign it values. Do something like this right before that code.
    Code:
    hptr = malloc(sizeof(struxt xnode));
    Last edited by stumon; 04-01-2003 at 03:05 PM.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    33
    thanks :-)

  7. #7
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    Also, cptr pointer used in addn() will be NULL when first used, memory needs to be allocated before using.

    cptr->next = nodeptr;
    Last edited by Scarlet7; 04-01-2003 at 03:08 PM.

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    33
    mkay, i fixed those, but my display function doesnt work at all :-( anyone know why thanks for all your help guys/girls

  9. #9
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    And there's more! The following changes will get the numbers displayed. Your mainly not setting cptr back to hptr before using, and using while(cptr->Next) in loops which should be just cptr. In the first while loop you were going cptr = hptr within the loop which should be outside.

    Code:
    void main()
    {
       ...
       cptr = hptr = (struct xnode*)malloc(sizeof(struct xnode));
       ...
       ...
       cptr = hptr;
       while(cptr != NULL)
       {
          sum += cptr->num;
          cptr = cptr->next;
       }
       ...
    }
    
    void addn(int x)
    {
       ...
       cptr->next = nodeptr;
       cptr = cptr->next;
       cptr->next = NULL;
    }
    
    void order()
    {
       ...
       cptr = hptr;
       while (cptr !=NULL)
       {
          ...
       }
    }
    
    void display()
    {
       ...  
       cptr = hptr;
       while(cptr!=NULL)
       {
          ...
       }
    }
    
    void del()
    {
       cptr = hptr;
       struct xnode *dptr;
       ...
    }
    Last edited by Scarlet7; 04-01-2003 at 03:56 PM.

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    33
    :-D you kick ass!! thanks alot

  11. #11
    Registered User
    Join Date
    Feb 2003
    Posts
    33
    ok, another problem, lol

    this function is supposed to put the link list in order but it doesnt work

    Code:
    void order()
    {
      int temp;
      cptr = hptr;
      while (cptr->next !=NULL)
        {
          if (cptr->num > hptr->num)
            {
              temp = hptr->num;
              hptr->num = cptr->num;
              cptr->num = temp;
            }
          cptr = cptr->next;
        }
    }
    again thanks alot for all your help
    Last edited by sayword; 04-01-2003 at 04:33 PM.

  12. #12
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    You need to keep loopng through the list until there's no more swaps, you were just going once. Also you need the compare with the next number in the list, then swap with the next.
    Code:
    void order()
    {
        int temp;
        bool bSorted = 0;
    
        while(!bSorted)
        {
            bSorted = 1;
            cptr = hptr;
            while (cptr !=NULL)
            {
                 if( cptr->next && cptr->num > cptr->next->num)
                {
                     temp = cptr->num;
                     cptr->num = cptr->next->num;
                     cptr->next->num = temp;
    	        bSorted = 0;
                }
                cptr = cptr->next;
            }
        }
    }

  13. #13
    Registered User
    Join Date
    Feb 2003
    Posts
    33
    thanks, question, what is bool?

  14. #14
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    A bool is a boolean value which can only be TRUE or FALSE. You could have done the same with an int

    int iSorted = 0;

Popular pages Recent additions subscribe to a feed