Thread: Problem with arrays structures sorting.

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    7

    Problem with arrays structures sorting.

    I am trying to write a program that reads elements and their relative abundances from a file, then to sort them alphabetically or in order of abundance. however i have been having problems with the part involving structures and arrays to read the data from the file and get it into a format that can be sorted in the desired manner.

    My code so far is

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    char name[128];
    FILE *f; 
    char s[1000];
    printf("\nPlease input a file name including extension\n");
    scanf("%s",name);
    f=fopen(name,"r");
    if (!f) return 1;
      while (fgets (s,1000,f)!=NULL)
      printf("%s",s);
      fclose(f); 
    /*sorting*/
      
    	printf("how would you like the data sorted?\n press 1 if you would like the data sorted alphabetically by name of element, or press 2 to sort data by ascending percentage abundance\n ");
    	scanf("%d", &y);
    
    /*sorting functions*/
    
    		if(y == 1) /*alphabet*/
            {
              
    
    
            }
    
            else if (y == 2) /*number*/
            {
    
    
            }
      
      
    
      return 0;
    
    }

    a sample input is:

    Oxygen 15
    Carbon 60
    Hydrogen 2
    Nitrogen 20
    Boron 3

    if anyone has any pointers or help to offer, i would be very grateful.
    pitifulworm.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    All element names are one word, and numbers are numbers, so you should be able to use the formatted read functions (like *scanf) to read from your file into ... whatever you want to put them in. Presumably you would make an array of structs, where each struct would contain some chars for the name and an int for the int.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Structures would be a good way to go, another good, and simpler way to go, would be to use parallel arrays:

    element[N][n] atoms[N]

    Where N would correspond in the element array, with the same N in the atom array. The small n would be a length that would exceed any word in your list - 20 should surely do.
    (what element has the longest name? californium?

    Data would look like:
    Code:
    Hydrogen  1  
    Helium    2
    Boron     5
    etc.

    after it was sorted by number of atoms. Edit: we're sorting by abundance, not number of atoms.

    In the sorter, when a swap is needed, you need to swap both the name of the element, in the element[] *and* the atom[], to keep the proper alignment of name with number, in place.
    Last edited by Adak; 02-05-2009 at 09:06 AM.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    it's easier to sort an array of structures than parallel arrays, so that bit of complexity is worth it. you can also use qsort instead of making up a sort algorithm.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Parallel arrays are taught just before structures, in nearly all intro CS classes.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    pitifulworm mentioned structures, so what's the problem?

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    however i have been having problems with the part involving structures
    The OP doesn't know how to work with structures, yet. This assignment "smells" like a parallel array problem, to me.

    I have no problem if the OP wants to use a structure, at all. Always more than one way to get the job done.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    oh, i don't want to get in the way of your sense of smell then.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Pitifulworm, you need to chime in here. If the assignment requires the name of the element, the number of atoms, *and* anything else (like it's abundance in a file), then you need a structure to hold that info.

    If your assignment just requires the name of the element and the number of atoms, then a parallel array is fine, OR a structure, either one.

    Both can be sorted, loaded, etc., with very little difference in terms of work involved.

    So, which way do you prefer?

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    7
    the sample data is the name of the element and its % abundance, so i need these to stay together so that it can sort in order of abundance or alphabetically. i am pretty new to c programming so haven't done structures and arrays much, so which would be better; a parrallel array (of which i have not done at all ever) or i could just do an array of structures?

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    They're really about the same. (despite our debate on it in the thread). IMO parallel arrays is a better choice if you have two things to keep together. For three or more things, a struct is the choice that makes sense.
    Last edited by Adak; 02-05-2009 at 08:11 AM.

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    Quote Originally Posted by pitifulworm
    i am pretty new to c programming so haven't done structures and arrays much, so which would be better; a parrallel array (of which i have not done at all ever) or i could just do an array of structures?
    try them both. the worst that could happen is you learn another way to solve the problem.
    Quote Originally Posted by Adak
    IMO parallel arrays is a better choice if you have two things to keep together.
    except that you have to manage two entities instead of one, and if you're sorting you can't use qsort anymore. imo when ppl can't use qsort they resort to something crappy and easy to write like bubble sort. parallel arrays pretty much always make the code worse in one way or another.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can use qsort with parallel arrays.

    Just swap the corresponding second array's element whenever you swap an element from the first array (the key array). That's the whole extent of "managing the entity". LOL

  14. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    Quote Originally Posted by Adak
    You can use qsort with parallel arrays.

    Just swap the corresponding second array's element whenever you swap an element from the first array (the key array).
    show me the code, please. btw, i'm talking about the qsort function in stdlib.h, not your own ad hoc quicksort function.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Adak
    Just swap the corresponding second array's element whenever you swap an element from the first array (the key array).
    hmm... but you only provide the comparator and qsort() performs the swapping. I do not see how it can be done without changing the implementation for qsort() to support the parallel arrays since qsort() does not use a callback function to perform swapping.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  2. Array Sorting problem
    By ___________ in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 12:17 AM
  3. Problem with structures
    By dereach in forum C Programming
    Replies: 2
    Last Post: 10-30-2007, 08:22 AM
  4. problem with arrays and structures
    By gell10 in forum C++ Programming
    Replies: 7
    Last Post: 11-03-2003, 12:02 AM
  5. Sorting an Array of Structures
    By bob2509 in forum C++ Programming
    Replies: 7
    Last Post: 05-12-2002, 08:26 AM