1

I am new to C and trying to code a program to sort char arrays by shorting an array of pointers pointing on them(A program as in D&K The C programming language pg-108). here is my code-

#include <stdio.h>
#include <string.h>
#define MAXLINES 5000
#define MAXLEN 100
char *line[MAXLINES];
int readlines(char *line[], int t);
void writelines(char *line[], int t);
void qsort(char *line[], int lo, int m);
int getline(char *, int);
char *alloc(int);
int partition(char *line[], int lo, int m);
int main()
{
    int nlines;   //no of input lines
    if(nlines=readlines(line,MAXLINES)>=0)
    {

        qsort(line, 0, nlines-1);
        writelines(line, nlines);
        return 0;
    }
    else
    {
        printf("Error too many ");
        return 1;

    }



}
char store[10000];   //storage for alloc function
char *p = store;
char *alloc(int n)   //next free position for allocating to the input lines 
{
    if (p + n <= store + 10000)
    {
        p += n;
        return p - n;
    }
    else
        return NULL;
}
int readlines(char *le[], int maxlines) //read the input lines to be sorted
{
    char *p;
    int i = 0;
    char line[MAXLEN];
    int len;
    while ((len=getline(line,MAXLEN)) > 0)
    {
        if ((i >= maxlines)||((p = alloc(len)) == NULL))
        {
            return -1;
        }
        else
        {
            line[len - 1] = '\0';
            le[i++] = p;
            strcpy(p, line);
        }

    }
    return i;

}
void writelines(char *line[], int t)  /*function to print lines after 
{                                          sorting */
    for (int i = 0; i <= t - 1; i++)
        printf("%s\n",line[i]);

}
int getline(char a[], int t)    //function to read a line
{
    int i = 0;                 //to count no of input char  
    int c;
    while ((--t > 0) && (c = getchar()) != '\n'&&c != EOF)
        a[i++] = c;
        ;
    if (c== '\n')
        a[i++]='\n';
    a[i] = '\0';
    return i;

}

void qsort(char *line[], int lo, int m) /*function to quick sort the pointer
{                                         array*/
    if (lo >= m)
        return;
    int p = partition(line,lo,m);
    qsort(line,p+1,m);
    qsort(line,lo,p-1);


}
int partition(char *line[], int lo, int m)   /*function for partition in 
{                                               quick sort */
    int index=0;
    char *t = line[m];
    for (int i = lo; i <= m - 1; i++)
    {
        if (strcmp(line[i], t) <= 0)
        {
            char *p = line[i];
            line[i] = line[index];
            line[index] = p;
            index++;
        }
    }
        char *p = line[m];
        line[m] = line[index];
        line[index] = p;
        return index;
}

It consists of a number of functions and I have gave a tried to give a sort description of each.Even after spending several hours I was unable to figure out why it prints the first input line only.Please help to figure out what are the problems in this code.

Alpa8
  • 470
  • 4
  • 12
  • 2
    Run in a debugger, step through the code line by line, keep an eye on the variables and their values. – Some programmer dude Jan 20 '15 at 18:21
  • What leads to errors like `error conflicting types for ‘getline’` ? – Meninx - メネンックス Jan 20 '15 at 18:32
  • the answer is [here](http://stackoverflow.com/questions/13112784/undefined-reference-to-getline-in-c) I didn't know that ! +1 for OP question – Meninx - メネンックス Jan 20 '15 at 18:42
  • Just so you're aware... there's already a function called `qsort` (but different from yours) in the standard library, via `stdlib.h`... and on many systems also a `getline` function (again, different from yours) via `stdio.h`. – Dmitri Jan 20 '15 at 19:04
  • @Meninx But there is no error during compilation or output.The problem is that in the terminal if I enter few lines to be sorted then in the output only the first line is printed and nothing else.No sorting is taking place and even the other lines are not getting printed. – Alpa8 Jan 20 '15 at 19:08
  • I am under linux that's why I get that error !! @SwatantraKumar – Meninx - メネンックス Jan 20 '15 at 20:35
  • @Meninx I don't know.I am using Visual Studio Express 2013 and it complied there successfully as I told. – Alpa8 Jan 20 '15 at 20:53

1 Answers1

0

You need to replace:

if(nlines=readlines(line,MAXLINES)>=0)

...with

if((nlines=readlines(line,MAXLINES))>=0)
DanUK
  • 1
  • 1