help me please

telfer's Avatar
Go4Expert Member
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 ?
0
exile's Avatar, Join Date: Jun 2009
Go4Expert Member
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...
0
telfer's Avatar
Go4Expert Member
could explen more please
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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?
0
telfer's Avatar
Go4Expert Member
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
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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?
0
SaswatPadhi's Avatar, Join Date: May 2009
~ Б0ЯИ Τ0 С0δЭ ~
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.)
0
telfer's Avatar
Go4Expert Member
SaswatPadhi

I am using turbo c++

with include<stdio.h>

could you help me with turbo c++
0
telfer's Avatar
Go4Expert Member
Quote:
Originally Posted by xpi0t0s View Post
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
0
SaswatPadhi's Avatar, Join Date: May 2009
~ Б0ЯИ Τ0 С0δЭ ~
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();
}

Last edited by SaswatPadhi; 10Jun2009 at 21:36..