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 ?
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...
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?
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
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?
This is the complete working code : Code: #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.)
Come on man, can't you convert ANSI C++ to Turbo C++ code yourself ?! This should work in TurboC++. Code: #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(); }
If you want the code in Turbo C, it is this way : Code: #include <stdio.h> #define UpperLimit 500 struct Student { char NAME [50]; short GRADE; } StudentData [UpperLimit]; void BubbleSort(struct Student Array[], int Size) { int i, j; struct Student Temp; for(j = 0; j < Size; ++j) { for(i = 0; i < Size - 1; ++i) { if(Array[i].GRADE > Array[i+1].GRADE) { Temp = Array[i]; Array[i] = Array[i+1]; Array[i+1] = Temp; } } } } void main(void) { int i, Index = 0; FILE* MyFile = fopen("C:\\Test.txt", "r"); while( ! feof(MyFile) ) { fscanf(MyFile, "%s %d", StudentData[Index].NAME, &StudentData[Index].GRADE); Index++; } fclose(MyFile); BubbleSort(StudentData, Index); MyFile = fopen("C:\\Sorted.txt", "w"); for(i = 0; i < Index; ++i) fprintf(MyFile, "%s %d\n", StudentData[i].NAME, StudentData[i].GRADE); fclose(MyFile); }
thank you for ever my last question in this a way >>> HOW I can print the order by the name ( first A,B ) and so on
Simply use strcmp ! I have modified the code, and now it has two options, either you can sort by grades with BubbleSortGRADE, or sort by names with BubbleSortNAME. The code has a function MakeUppercase that converts strings to uppercase before comparison, so that letters like 'a' and 'A' are treated alike while sorting. Code: #include <stdio.h> #include <string.h> #define UpperLimit 500 struct Student { char NAME [50]; short GRADE; } StudentData [UpperLimit]; void BubbleSortGRADE(struct Student Array[], int Size) { int i, j; struct Student Temp; for(j = 0; j < Size; ++j) { for(i = 0; i < Size - 1; ++i) { if(Array[i].GRADE > Array[i+1].GRADE) { Temp = Array[i]; Array[i] = Array[i+1]; Array[i+1] = Temp; } } } } void MakeUpperCase(char *MyString) { int i, j = strlen(MyString); for(i = 0; i < j; ++i) MyString[i] = toupper(MyString[i]); } void BubbleSortNAME(struct Student Array[], int Size) { int i, j; struct Student Temp; char Name_A[50], Name_B[50]; for(j = 0; j < Size; ++j) { for(i = 0; i < Size - 1; ++i) { strcpy(Name_A, Array[i].NAME); MakeUpperCase(Name_A); strcpy(Name_B, Array[i+1].NAME); MakeUpperCase(Name_B); if( strcmp(Name_A, Name_B) > 0 ) { Temp = Array[i]; Array[i] = Array[i+1]; Array[i+1] = Temp; } } } } void main(void) { int i, Index = 0; FILE* MyFile = fopen("C:\\Test.txt", "r"); while( ! feof(MyFile) ) { fscanf(MyFile, "%s %d", StudentData[Index].NAME, &StudentData[Index].GRADE); Index++; } fclose(MyFile); BubbleSortNAME(StudentData, Index); MyFile = fopen("C:\\Sorted.txt", "w"); for(i = 0; i < Index; ++i) fprintf(MyFile, "%s %d\n", StudentData[i].NAME, StudentData[i].GRADE); fclose(MyFile); } NOTE : The questions that you are asking are pretty simple. If you are unable to answer these yourself, you are really weak in C/C++. I am not advising you, rather it's just my suggestion as a well-wisher that you try to write some code yourself. Copying the code from someone else and submitting it as a project would do no good. Just my 2 cents ..