Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   help me please (http://www.go4expert.com/forums/help-me-please-t17912/)

telfer 8Jun2009 23:43

help me please
 
good day


if I have name and gread in a file like this :

telfer 67
jon 89
necales 78
.
.
.



and I want to Sort the name according to their grade (increasing order) >>>
how I can do this ?

exile 9Jun2009 01:36

Re: help me please
 
hi, i don't know c++ syntax, but theoretically, you can explode the file into rows, then split them by space, and then use some sorting algorithm (for example insert sort) to sort them... fig c++ commands is 5 minute work with google...

telfer 9Jun2009 01:43

Re: help me please
 
could explen more please

xpi0t0s 9Jun2009 03:33

Re: help me please
 
Just read the file into whatever data structure seems easiest to you - a linked list of structures if you like, or a couple of static arrays if you're allowed to specify an upper limit of lines in the input file (no point complicating things unnecessarily).
Then run your favourite sort algorithm on the grade, remembering also to swap the entries in the name part of the data structure.

How far have you got with the project? Can you at least read the file into a data structure of your own design?

telfer 10Jun2009 18:42

Re: help me please
 
I did 8 steps of the project and the other three steps not yet
this is one of them

Unfortunately,

I did not know how to respond to this step

could you give me example or simple code

xpi0t0s 10Jun2009 19:20

Re: help me please
 
What data structure do you think would be most appropriate?
Do you need the size to be open ended or is it OK to have an upper limit?

SaswatPadhi 10Jun2009 19:27

Re: help me please
 
This is the complete working code :

Code: C++

#include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>

using namespace std;

struct StuRec
{
    string Name;
    short grades;
};

bool StuRecCompare (StuRec i,StuRec j)
{
    return ( i.grades < j.grades );
}

int main()
{
    vector<StuRec> Data;
    StuRec TStudent;
    Data.clear();

    ifstream fin("C:\\TestData.txt");
    while ( ! fin.eof() )
    {
        fin >> TStudent.Name;
        fin >> TStudent.grades;
        Data.push_back( TStudent );
    }
    fin.close();

    sort(Data.begin(), Data.end(), StuRecCompare);

    ofstream fout("C:\\SortedData.txt");
    for (int i = 0; i < Data.size(); ++i)
    {
        fout << Data[i].Name << " ";
        fout << Data[i].grades << endl;
    }
    fout.close();
    return 0;
}


Brief explanation :

(1) You create a struct StuRec to store the records.
(2) You define your own func that compares two StuRecs.
(3) You create a StuRec vector and fill it up from the file "C:\\TestData.txt". (Change it as per your need.)
(4) You use STL sort to sort the vector according your comparison func.
(5) You write the data to a new file "C:\\SortedData.txt". (Change it as per your need.)

telfer 10Jun2009 20:54

Re: help me please
 
SaswatPadhi

I am using turbo c++

with include<stdio.h>

could you help me with turbo c++

telfer 10Jun2009 21:03

Re: help me please
 
Quote:

Originally Posted by xpi0t0s (Post 49330)
What data structure do you think would be most appropriate?
Do you need the size to be open ended or is it OK to have an upper limit?

it is ok to have an upper limit


thank you

SaswatPadhi 10Jun2009 21:29

Re: help me please
 
Come on man, can't you convert ANSI C++ to Turbo C++ code yourself ?!

This should work in TurboC++.
Code: c++

#include <iostream.h>
#include <fstream.h>

#define UpperLimit 500

struct StuRec
{
    char Name[50];
    short grades;
};

void BubbleSort(StuRec arr[], int size)
{
      int i, j;
      StuRec temp;
      for(j = 0; j < size; j++)
      {
            for(i = 0; i < size-1; i++)
            {
                  if(arr[i].grades > arr[i+1].grades)
                  {
                        temp=arr[i];
                        arr[i]=arr[i+1];
                        arr[i+1]=temp;
                  }
            }
      }
}

void main()
{
      StuRec Data[UpperLimit];
      int DataSize = 0;

      ifstream fin("C:\\Test.txt");
      while ( ! fin.eof() )
      {
            fin >> Data[DataSize].Name;
            fin >> Data[DataSize].grades;
            ++DataSize;
      }
    fin.close();

    BubbleSort(Data, DataSize);

    ofstream fout("C:\\Sorted.txt");
    for (int i = 0; i < DataSize; ++i)
    {
        fout << Data[i].Name << " ";
        fout << Data[i].grades << endl;
    }
    fout.close();
}



All times are GMT +5.5. The time now is 03:33.