Hello Everyone!

I am due with an assignment that I just can't seem to acomplish. So here's the deal: I get a file with the following structure:

TyP_1 TyP_C2 TyP_C3
ORDER
Nr_rec
rec1_field1 rec1_field2 rec1_field3
rec2_field1 rec2_field2 rec2_field3

Typ_x can be S for string N for numeric or C for character
Order is a numeric (1,2,3) by which the sorting will take place
Nr_rec is the number of data lines below Nr_rec

What I have to do is sort them acording to these specifications,and output to out.txt.

Any help would be greatly appreciated. :)

Recommended Answers

All 7 Replies

Well, have you managed to get as far as

- open the input file
- read all the data into a suitable data structure
- close the input file
Then
- open the output file
- write all the data from your structure to the file
- close the output file.

Being able to read the file in, and write it back out again unchanged is an important first step. The sort just isn't going to happen until that's done.

Are you using C or C++?
Are you allowed to use one of the standard sort functions, or do you have to write your own?

yes, I have opened the file and have stored the values in two arrays, one is made up of the init variables, like type, number of records, etc, and the other one is for the data itself.

it would be best if I would write my own sort routine, but it is not mandatory.

I can output to a file easily, as soon as I sort them :)

C or C++, they're both ok.

Member Avatar for iamthwee

>C or C++, they're both ok.

So pick one. Then come back when you've decided otherwise you're going to get a very varied and confusing replies.

Ok. I will use C then. any ideas on structure or examples of code will be greatly appreciated.

Lookup the qsort() function declared in stdlib.h

All you need to do is write a compare function to compare two elements of you array.

If your array has a type T, then the compare function receives two T* parameters (as const void*), which you then cast back to T*.
You then dereference and compare the data to implement your sort ordering.

Member Avatar for iamthwee
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NELEMS 4

struct crap
{
   char word[200];
   int word_length;
};

static int name_comp(const void *, const void *);
static int word_length_comp(const void *, const void *);

int main()
{
   size_t i;
   static struct crap stuff[NELEMS] =
     {{"yo",2},
      {"momma",5},
      {"iz",2},
      {"phat",4}};

   qsort(stuff, NELEMS, sizeof stuff[0],
        name_comp);

   puts("By alphabetical order:");
   for (i = 0; i < NELEMS; ++i)
      printf("%s, %d\n",
        stuff[i].word,
        stuff[i].word_length);

   qsort(stuff, NELEMS, sizeof stuff[0],
        word_length_comp);

   puts("\nBy word length:");
   for (i = 0; i < NELEMS; ++i)
      printf("%s, %d\n",
        stuff[i].word,
        stuff[i].word_length);
        
        getchar();
        getchar();
   return 0;
}

static int name_comp(const void *p1, const void *p2)
{
   struct crap *sp1 = (struct crap *) p1;
   struct crap *sp2 = (struct crap *) p2;
   int order = strcmp(sp1->word,sp2->word);

  
   return order;
}

static int word_length_comp(const void *p1, const void *p2)
{
   struct crap *sp1 = (struct crap *) p1;
   struct crap *sp2 = (struct crap *) p2;

   return sp1->word_length - sp2->word_length;
}

How about this. Two of the sorting parts are done for you, eg. sort by string and sort by number. All you have to do is define one more function to sort by a single character.

1- press 1 to write a file with data in the following format.

Index Data type value
3 string orange
2 Char A
1 Int 1234
4 float 34.567

2- the data type has three columns;
>the first column contains the number of serving incdices. you basically need to sort the data on the basis of this column.
>the second column contains text representing data types of values present in the third column.
>the third column contains the value that could be number,strings,characters,decimals etc. depending on the data type in the second column.

3- press two to sort your file.under this option your program will ask the user to enter file name and sorting option(sort on index,data type or value).your program then open the above mentioned file containing above data, sort the data, and then write the sorted data in another file called result.
above data will be sorted as follow:


Index Data type value
1 int 1234
2 char A
3 string orange
4 float 34.567

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.