Thread: problem with array sorting program

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    1

    Question problem with array sorting program

    I'm new to programming in c and I'm trying to write a program that reads floating point numbers from a user entered file then sorts them and prints them out. I'm not sure where I've messed up in the code. When I run the program and type in the test file, all that happens is it prints a bunch of zero's and some other random numbers not in the test file. Where have I messed up in the code?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define ARY_SIZE 100
    #define FFLUSH while(fgetc(fp) != '\n')
    
    
    
    
    
    
    int main()
    {
        printf("\n\nCOP 2220 Project 4: Kurt Gallagher\n\n");
        printf("Enter File Name: ");
    
    
        float dataArray [ARY_SIZE], val;
        int i, rc, temp, numPrinted, swap;
        char fname[100];
    
    
        FILE* fp;
        rc = scanf("%s", fname);
        fp = fopen(fname, "r");
        if (fp != NULL)
            {
            char line[BUFSIZ];
    
    
            while (fgets(line, sizeof line, fp) != NULL)
                {
                char *start = line;
                float field;
                int n;
    
    
                while (sscanf(start, "%f", &field, &n) == 1)
                    {
                    printf("%f", field);
                    start += n;
                    }
                }
            }
    
    
        for(i = 0; i < ARY_SIZE; i++)
        {
            for (swap = 0; swap > ARY_SIZE; swap--)
                if (dataArray[swap] < dataArray[swap - 1])
                {
                    temp = dataArray[swap];
                    dataArray[swap] = dataArray[swap - 1];
                    dataArray[swap - 1] = temp;
                }
        }
    
    
        numPrinted = 0;
        for(i = 0; i < ARY_SIZE; i++)
        {
            printf("%f", dataArray[i]);
            if (numPrinted < 5)
                numPrinted++;
            else
            {
                printf("\n");
                numPrinted = 0;
            }
        }
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You missed the %n in the format string.
    Code:
    $ gcc -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:37:7: warning: too many arguments for format [-Wformat-extra-args]
    foo.c:18:10: warning: variable ‘rc’ set but not used [-Wunused-but-set-variable]
    foo.c:17:31: warning: unused variable ‘val’ [-Wunused-variable]
    $
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2015
    Location
    Australia
    Posts
    63
    Hi...

    Also, just going through the lines... I find "char line[BUFSIZ]; " what is BUFSIZ, no declaration or initialising .
    Some brackets around " sizeof()...needed.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by JohnGM View Post
    Hi...

    Also, just going through the lines... I find "char line[BUFSIZ]; " what is BUFSIZ, no declaration or initialising .
    Some brackets around " sizeof()...needed.
    BUFSIZ is a standardized macro. Normally used for the setbuf routine, it is an integer that represents the size of user allocated buffers.

    People use it as a fairly good guess how long user inputted strings should be, although there is really nothing stopping the user from typing more at once.

    sizeof does not generally need parentheses if it's used on an expression. Parentheses are required around a type name for sizeof.

  5. #5
    Registered User
    Join Date
    Sep 2015
    Location
    Australia
    Posts
    63
    Hi...

    Well ya learn something new everyday....thanks whiteflags

    I knew BUFSIZ had to be something, with the Capitals.....don't remember ever seeing it used.
    As for sizeof.......it was not causing an issue. I will stick to using them, keeps me out of trouble and some bit closer too sane..

    John

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someone please explain this array sorting program?
    By Frankie15 in forum C Programming
    Replies: 4
    Last Post: 10-03-2011, 05:44 PM
  2. Sorting array problem :)
    By BEST in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2009, 01:57 PM
  3. Array Sorting problem
    By ___________ in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 12:17 AM
  4. array sorting problem?
    By vutek0328 in forum C Programming
    Replies: 14
    Last Post: 09-12-2006, 11:07 AM
  5. Problem with sorting an array
    By lostminds in forum Game Programming
    Replies: 2
    Last Post: 04-24-2002, 09:27 AM