Thread: Sorting/comparing arrays from inputs.

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    4

    Sorting/comparing arrays from inputs.

    Greetings fellow programmers. I'm just starting out as a programmer in Microsoft Visual C++ Express and i've run into a stump with a more or less simple program.
    The aim is to open an input file, read the file and store it, then alphabetically sort and display them.
    My programming is sound for the alphabetically sorting and displaying, but once I changed my code from manually inputing the names to trying to read them from an input file, things went horribly wrong. Here is my code so far: any help would be greatly appreciated.

    Code:
    Code:
    #include
    "stdafx.h"
    
    #include
    "string.h"
    
    #define
     STRSIZE 21
    
    #define
     MAXBAT 16
    
    
    int
     alpha_first(char *list[], int min_sub, int max_sub);
    
    void
     select_sort_str(char *list[], int n);
    
    
    int
     main(void)
    
    {
    
        
    char batters[MAXBAT][STRSIZE];
    
        
    char batman[STRSIZE];
    
        
    char *alpha[MAXBAT];
    
        
    int num_bat = 0;
    
        
    int i = 0;
    
        
    FILE *inpa;
    
        
    int inpa_status;
    
    
    inpa = fopen("H:\\GENG1003\\input2a.dat", "r");
    
    
    inpa_status = fscanf(inpa, "%s", &batman);
    
    num_bat = 16;
    
    
        
    while (inpa_status != EOF){
    
            printf("\nPlayer:\n%s\n", batman);
    
            inpa_status = fscanf (inpa, "%s", &batman);
    
            strcpy(batman[STRSIZE],batters[MAXBAT]);
    
        }
    
    
        
    for (i = 0; i < num_bat; ++i){
    
            alpha[i] = batters[i];
    
        }
    
        select_sort_str(alpha, num_bat);
    
        
    
        printf("\n\n%-30s%5c%-30s\n\n", "Application Order", ' ', "Alphabetical Order");
    
        
    for (i = 0; i < num_bat; ++i){
    
            printf("%-30s%5c%-30s\n", batters[i], ' ', alpha[i]);
    
        }
    
        fclose(inpa);
    
    
        
    return 0;
    
    }
    
    

    Last edited by siege; 10-14-2011 at 09:11 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Your problem is that you aren't using fscanf correctly. You can refresh your memory by reading this.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    Thanks for contributing Whiteflags, but the fscanf debarkle didn't solve my problem.

    My updated code:
    Code:
        
    char batters[MAXBAT][STRSIZE];
    
        
    char *alpha[MAXBAT];
    
        
    int num_bat = 0;
    
        
    int i = 0;
    
        FILE *inpa;
    
        
    int inpa_status;
    
    
        inpa = fopen("H:\\GENG1003\\input2a.dat", "r");
    
    
        inpa_status = fscanf(inpa, "%s", batters);
    
        num_bat = 16;
    
    
        
    while (inpa_status != EOF){
    
            printf("\nPlayer:\n%s\n", batters);
    
            inpa_status = fscanf (inpa, "%s", batters);
    
        }
    
    
        
    for (i = 0; i < num_bat; ++i){
    
            alpha[i] = batters[i];
    }
    
    


    The problem is, batters scans and displays 'player' just fine. However, when it comes to displaying the 'player' list in alphabetical order, I get the last player from the input file under both 'Application' order and 'Alphabetical' order, then a giant list of garbled characters, much to this effect;
    [|[|[|[|[|[|[|[|[|[|[|[|[|[|[|[|[|[|[|[|[|[|[|[|[|[|[|[|[|[|[|[|[|[|[|[|[|[|[|[|[|[|U^eC^''^

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is supposed to be C or C++? If it is really supposed to be C, I will move this thread accordingly.

    Anyway, look closely:
    Code:
    inpa_status = fscanf(inpa, "%s", batters);
    batters is an array of array of char. You really want to pass a pointer to char. Therefore, you need to provide it with an array of char, which will then be converted to a pointer to its first element, e.g.,
    Code:
    inpa_status = fscanf(inpa, "%s", batters[0]);
    Since you will be doing this in a loop, you need a variable to keep track of which array of char you are reading into.
    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
    Oct 2011
    Posts
    4
    Wonderful! That helped supremely! Thankyou very much Laserlight.
    As for your question of it being C or C++...I'm actually unsure of that. I'm using a C++ compiler, but i'm still very much an amateur.

    Another small question while I have your attention, can you point me to a tutorial or example of one array being compared to another? The rest of my code is going to be introducing a second input (one with batter's name along with a score of runs, how they got out, average innings, etc.) and displaying names and scores in alphabetical order, then batting average order.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by siege View Post
    As for your question of it being C or C++...I'm actually unsure of that. I'm using a C++ compiler, but i'm still very much an amateur.
    Do you have a textbook? What's the title of it? Maybe it mentions the language you're supposed to be learning.

    Quote Originally Posted by siege View Post
    Another small question while I have your attention, can you point me to a tutorial or example of one array being compared to another? The rest of my code is going to be introducing a second input (one with batter's name along with a score of runs, how they got out, average innings, etc.) and displaying names and scores in alphabetical order, then batting average order.
    Well for the comparison part of the sort, how do you determine the order of words in a dictionary?

    You would break up the word into letters. Look at the first letter in both words and if they are the same, go to the next letter... until you find two letters that are different. The word with the letter that comes before the other letter in the other word comes before the other word in the list. You do that enough times over the entire list, you've actually sorted the list.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by siege
    As for your question of it being C or C++...I'm actually unsure of that. I'm using a C++ compiler, but i'm still very much an amateur.
    I am not asking what it is. I am asking what it is supposed to be. If you don't know what it is supposed to be, then you should decide. Otherwise, it is as if you are trying to learn Chinese in a Japanese language school: someone unfamiliar with both languages might think they are the same, but they are not.
    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

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    Okay, i'll try coding it that way and see what happenes, thank you Whiteflags.

    Ha! Yes, I see what you're getting at Laser. Based off what i've learned and seen, i'm going to assume 'C' and not 'C++'.

    EDIT:

    Yes, definatly 'C' programming language. Sorry for the confusion.
    Last edited by siege; 10-15-2011 at 01:23 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing two arrays
    By Gonz in forum C Programming
    Replies: 2
    Last Post: 03-23-2011, 12:38 AM
  2. Sorting a set of numbers based on what the user inputs?
    By vmckoon2 in forum C Programming
    Replies: 42
    Last Post: 07-02-2010, 11:48 AM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Comparing Arrays
    By Raison in forum C++ Programming
    Replies: 10
    Last Post: 04-20-2004, 02:21 PM
  5. Replies: 2
    Last Post: 02-23-2004, 06:34 AM

Tags for this Thread