Thread: Print sorted lines from file

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    62

    Print sorted lines from file

    I have the following .txt file:
    1 7 9 12
    8 4 22 5
    4 9 0 10
    15 2 3 14
    I wanna print the lines in descending order based on the average value on each individual line.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX 300
    int comp(const void *p, const void *q)
    {
        float l = *(const float*)p;
        float r = *(const float*)q;
        return (r-l);
    }
    int main(int argc, char* argv[])
    {
        if(argc != 2)
        {
            printf("Invalid format.\n");
            return -1;
        }
        FILE* fin = fopen(argv[1], "r");
        if(!fin)
        {
            printf("Error opening the file.\n");
            return 1;
        }
        char buf[MAX];
        int n, countLines = 0;
        float *avg;
        while(fgets(buf, MAX, fin))
        {
            avg = realloc(avg, (countLines+1)*sizeof(float*));
            float sum = 0;
            float countWords = 0;
            char* word = strtok(buf, " ");
            while(word)
            {
                countWords++;
                //printf("Word : %s\n",word);
                n = atoi(word);
                sum = sum+n;
                word = strtok(NULL, " ");
            }
            avg[countLines] =(float)(sum/countWords);
            countLines++;
        }
        qsort(avg, countLines, sizeof(float), comp);
        for(int i = 0; i < countLines; i++)
        {
            printf("%f\n", avg[i]);
        }
        free(avg);
        fclose(fin);
        return 0;
    }
    Output so far:
    9.750000
    8.500000
    7.250000
    5.750000
    So I'm getting the right values, I just need to figure out how to print the actual lines, not the values. Any ideas?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,275
    What I'd do is declare a struct consisting of a member that stores the line and another member that stores the average value. Then I can populate an array of these struct objects and sort it according to the average value, after which I can print it by printing the lines.
    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. Print only specific lines in the file
    By fat32 in forum C Programming
    Replies: 1
    Last Post: 11-02-2018, 01:30 AM
  2. Replies: 6
    Last Post: 10-19-2016, 06:51 AM
  3. Open file and print user selected number of lines
    By steals10304 in forum C Programming
    Replies: 14
    Last Post: 09-01-2009, 01:13 PM
  4. Circular linked list to print last N lines of file
    By FortinsM3 in forum C Programming
    Replies: 7
    Last Post: 10-24-2008, 05:42 PM
  5. print lines
    By 8ball in forum C Programming
    Replies: 8
    Last Post: 05-14-2004, 01:45 AM

Tags for this Thread