Thread: No CLUE what to do...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    18

    Angry No CLUE what to do...

    Basically, I have an assignment and have NO clue. Can someone please help? Here is the assignment along with the link for the bubble sort:

    In this assignment you are to implement an array of 10 structs, each of which has two members:

    Name : which contains strings of arbitrary lengths.
    ID : an integer

    You should read them in from standard input. The format is one line of name , then another line of id. It continues until the end of file.

    Then you are to use bubble-sort to sort the array according to the ID. The output should be id followed by the name, one line per pair. So in total there should be 10 lines.


    Bubble Sort

    If anyone can help, I would greatly appreciate it. Thank you.

    -Frustrated with Structure

  2. #2
    Hello stweade, I might be able to help.

    First question I have is, do you have what the file's layout of data to be inputted looks like?

    Example:

    Tom
    1

    Jack
    2
    Is it like that, or something different?


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    it said read from standard input, which is quite easy heres help with the struct declaration:

    Code:
    struct info_struct {
       short int id;
       char *name;
    } members[10];
    Now see that name will have to be allocated for each name you read in, I assume that is what is required as it said names of arbitrary length.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    18
    The input will come from the keyboard. The output will look something like this:

    Please enter name: Stewart
    Please enter ID: 123
    Please enter name: Joe
    Please enter ID: 456

    for 10 names/ids. I hope that answers what you were asking.

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Code:
    #include <stdio.h>
    
    struct info_struct {
       int id;
       char *name;
    } members[10];
    
    int main(void)
    {
       char buf[BUFSIZ];
       int i=0;
       
       while (i<10) {
          /* get input here and store it in "buf"
              next malloc the sizeof buf for members[i].name, then strcpy buf into members[i].name
              next get the id directly into the structure, something like:
          fgets(buf, sizeof buf,stdin);
          members[i].id = atoi(buf);
              of course you'll need error checking for the malloc and the input and all that... */
          i++;
       }
    
        /* sort and output array of structs here ... */
    
       return 0;
    }
    If you give us some actual attempt at doing this, we may be able to help, otherwise, we're not just gonna do your homework for you, sorry.

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    18
    This is what I have had so far. I don't know how to read in an integer for the ID and then I don't know how to pass it to the bubble array.

    Code:
    #include <stdio.h>
    
    void bubbleSort(int numbers[], int array_size);
    
            struct member {
                    char name[30];
                    int id[10];
                    };
    
    int main(void)
    {
    
            struct member n[10], d[10];
            int i, j, k, *p1, size;
    
            for (i=0; i<10; i++)
                    {
                    printf("Please enter name: ");
                    gets(n[i].name);
                    printf("Please enter ID:   ");
                    gets(d[i].id);
                    }
    
            for (k=0; k<10; k++)
                    {
                    p1 = d[k].id;
                    size = 10;
                    bubbleSort(*p1, size);
                    }
    
            for (j=0; j<10; j++)
                    {
                    printf("%s, %s\n", d[j].id, n[j].name);
                    }
    
            return 0;
    
    }
    
    
    void bubbleSort(int &numbers[], int array_size)
    {
      int i, j, temp;
    
      for (i = (array_size - 1); i >= 0; i--)
      {
        for (j = 1; j <= i; j++)
        {
          if (numbers[j-1] > numbers[j])
          {
            temp = numbers[j-1];
            numbers[j-1] = numbers[j];
            numbers[j] = temp;
          }
        }
      }
    }
    I have attempted this, it's not just a plea to do my homework for me. I really just am having a hard time understanding structure I guess. I dunno...

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void bubbleSort(int &numbers[]
    Well that's incorrect, for starters, because there are no "references" in C like there are in C++. That is to say, you can't use the reference operator that C++ has.

    Consider reading this link on how to read a line correctly from the user, and this link on how to read a number.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    18
    Wahoo! I changed it to a regular "int" and it worked. Thanks man. I have another part to the problem I have to figure out, so I am going to try and figure that out...you never know, I could be back with more problemos. Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. Replies: 8
    Last Post: 04-19-2006, 09:07 AM
  3. No clue what the problem is...
    By FingerPrint in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2006, 03:16 PM
  4. A really weird problem. I understand the error, but not a clue why I get the errors.
    By Finchie_88 in forum Networking/Device Communication
    Replies: 1
    Last Post: 02-20-2005, 09:48 AM
  5. Not a clue
    By cyberCLoWn in forum C++ Programming
    Replies: 11
    Last Post: 12-28-2003, 11:54 AM