Thread: how to implement array and sort

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

    how to implement array and sort

    Hi again,

    I've progressed somewhat since yesterday and now have my .h files working half way.
    I have been able to get the program to read from a file and then print to screen the data in the file.
    The data is elements and their abundances.
    Oxygen 65
    Carbon 18
    Hydrogen 10
    Nitrogen 3
    Calcium 1.5
    Phosphorus 1.2
    Potassium 0.2
    Sulfur 0.2
    Chlorine 0.2
    Sodium 0.1
    Magnesium 0.05
    END
    My task after this is to sort the data either alphabetically or numerically.

    So far, the code for my .h file that handles this request is

    Code:
    int abundance_sorter() {
    
    char string[100];
    char fname[255], contents[255];
    FILE *fp;
    #define MAX 30
    
    
    	while(1)
    	{
    
            printf("\n[Please enter the filename:]\n\n");
          	scanf("%s", fname);
            /* Try opening the file. */
            fp = fopen(fname, "r+");
            if (fp == NULL)
            {
            	printf("\nThe file '%s' could not be opened!\n\n", fname);
            	continue;
            }
            else
            {
    
    
            	while (strcmp(contents, "END") != 0) {
    
            		fscanf(fp, "%s", contents);
    
            		{
            		printf("%s ", contents);
            		}
            	}
    
            	break;
            }
    	}
    
           //printf("%s", contents);
    
    
    return 0;
    }
    If anyone has any tips on setting up an array for this and then sorting it, I'd be very grateful.

    Cheers

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    One way is to define a struct to represent an element and its abundance, e.g.,
    Code:
    typedef struct Element
    {
        char name[100];
        int abundance;
    } Element;
    Now, you create an array of Element objects. To sort the array, #include <stdlib.h> and use qsort(). You would need to define two comparator functions: one to compare Element objects based on name, the other to compare them based on abundance.

    Another way would be to create two arrays: one of element names, the other of the corresponding abundance values, but then qsort() would not be suitable.
    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

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    21
    Thanks for the tips, unfortunately we haven't covered comparator functions in class so I am unsure how to use them. Would it be possible to explain that also please?

    Thanks

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    A simple example of a comparator function in shown in cppreference.com's entry on qsort(). However, you might want to check with your instructor if you are allowed to use qsort(), or if you must implement the sorting algorithm yourself.
    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

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    21
    We're allowed to use whatever we want. I don't mean to be rude about the instructor but I don't think she knows what she is doing and can't teach the course very well.
    Thanks for your help, I will endeavor to work this out!!!

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    No problem. In that case you should use qsort() first, and maybe later think of how to implement it yourself as an exercise in learning about sorting

    By the way, I made a mistake in my example: abundance should be a double, not an int.
    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. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  2. Sorting
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-10-2003, 05:21 PM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM