i have this data..but how to sort the data in descending order together with the examid?

examid total

1: 25

2: 20

3: 46

4: 56

5: 12

6: 22

7: 20

8: 18

0

i have this data..but how to sort the data in descending order together with the examid?

examid total

1: 25

2: 20

3: 46

4: 56

5: 12

6: 22

7: 20

8: 18

0

Till what have you got to? I mean paste in the program that you have worked on until now.

0

I have like this...but i have a problem..to sort it descending together..examis and total. Can anybody help me?.........

```
#include <iostream> // std::cout
#include <fstream>
#include <iomanip>
#include <string> // std::string
#include <vector> // std::vector<>
#include <algorithm> //std::for each()
using namespace std; // import "std" namespace into global namespace
struct exam
{
int examid;
vector <int> total;
};
int main()
{
ifstream stream1("STA83SOLUTION.txt");
if ( !stream1.is_open())
{
cout << "Tak Buka" << endl;
}
else
{
cout << "Fail Di buka....." << endl;
}
vector <exam> exams;
exam aExam;
int tempExamID;
int tempTotal;
stream1 >> tempExamID >> tempTotal;
aExam.examid = tempExamID;
aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
while (stream1 >> tempExamID >> tempTotal)
{
if(tempExamID != aExam.examid)
{
exams.push_back(aExam); // no more exam codes for this student. Add aStudent to students vector
aExam.total.clear();
aExam.examid = tempExamID;
}
aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
}
exams.push_back(aExam); // no more exam codes for this student. Add aStudent to students vector
stream1.close(); // We have read the entire file, so time to close it.
{
ofstream myfile;
myfile.open("411.txt");
if (myfile.is_open())
{
for (size_t i = 0; i < exams.size(); i++)
{
for (size_t j = 0; j<exams.at(i).total.size(); j++)
{
cout<<"\n"<<i+1<<":"<<" "<< exams.at (i).total.at(j)<<"\t"; // output list of exam codes for this student
}
}
}
cin.get();
return 0;
}
}
```

0

where's the code to do sorting just the total?

at the time you swap the totals, you can swap the examid's also, i think that should work. what say?

0

this,,,but how to take together the 2 arrays which i only sort the total only..

```
if (exams.at (i).total.at(j+1)<exams.at (i).total.at(j))
{
temp=exams.at(i).total.at();
exams.at(i).total.at()=exams.at(i).total.at()+1;
exams.at(i).total.at()+1=temp; }
```

0

i edited my post later, just read the last line again. when you swap the total, swap the exam id's as well. in short you will swap the entire structures and not just the totals.

0

this is some thing i designed for my tutorial .. this uses quick sort, insertion, bubble and flag bubble to sort numbers ... you can select one of them and design your code.. i think this code will you but ,, if you have any problem you can contact me.. any way the code here seems to be a bit too much for what you are expecting but .. you will get to know alot about the sorting mechanisms used

```
#include<iostream>
#include<cstdlib>
using namespace std;
typedef int intarray[];
//acts as a counter to count the no of compares
int compares(int a)
{
static int count = 0;
count += a;
return count;
}
//acts as a counter to count the no of swaps
int swaps(int a)
{
static int count = 0;
count += a;
return count;
}
//checks if not in order
bool notinorder (int a,int b)
{
compares(1);
return (a>b);
}
//generate random numbers
void generate(intarray arr,int size,int low,int high)
{
for (int i = 0 ; i <size ; i++)
{
int range=(high-low)+1;
arr = rand ( ) % (low+high);
}
cout<<endl<<endl;
}
//swaps if not in order
void swap(int& a,int& b)
{
swaps(1);
int c = 0;
c = a;
a = b;
b = c;
}
//the bubble sort
void bubsort(intarray arr,int size)
{
for(int j = size-2;j>= 0 ; j--)
{
for(int i =0; i <= j;i++)
{
if(notinorder(arr,arr[i+1]))
{
swap(arr,arr[i+1]);
}
}
}
}
//flagbubble sort
void flagbubble(intarray arr,int size)
{
bool stillwapping = false;
for(int j = size-2;j>= 0 ; j--)
{
for(int i =0; i <= j;i++)
{
if(notinorder(arr,arr[i+1]))
{
stillwapping = true;
swap(arr,arr[i+1]);
}
}
if (!stillwapping)
break;
}
}
// sift funcrion associated with insertion sort
void sift(intarray arr, int i)
{
int j = i - 1;
while (( j >= 0) && notinorder(arr[j],arr[j+1]))
{
swap(arr[j],arr[j+1]);
j--;
}
}
//insertion sort
void insertion(intarray arr,int n)
{
for(int i=1;i < n;i++)
{
sift(arr,i);
}
}
//find best position associated with exchange sort
int bestpos(intarray arr,int start,int end)
{
int best = start;
for(int j = start + 1; j<end; j++)
{
if(notinorder(arr[best],arr[j]))
{
best = j;
}
}
return best;
}
//the exchange sort
void exchangesort(intarray arr,int n)
{
for(int i=0;i < n-1;i++)
{
int j = bestpos (arr,i,n);
if(j != i)
{
swap(arr[j],arr);
}
}
}
//display in width 4 and 15 per slot
void display(intarray arr,int n)
{
for (int t = 0 ; t <n ; t++)
{
if(t % 15 == 0)
cout<<endl;
cout.width(4);
cout<<arr[t]<<" ";
}
}
//displaying summary of all sort mechanisms
void result()
{
cout<<"\t\tData Size 50\t\tData Size 100\t\tData Size 1000"<<endl;
cout<<"Type Of Sort\tSwaps\tCompares\tSwaps\tCompares\tSwaps\tCompares"<<endl;
cout<<"Bubble Sort \t1225\t579\t\t4950\t2525\t\t124750\t67574"<<endl;
cout<<"Flag Bubble \t1225\t579\t\t4950\t2525\t\t124750\t67574"<<endl;
cout<<"Insertion \t628\t579\t\t2622\t2525\t\t68069\t67574"<<endl;
cout<<"Exchange Sort\t1225\t47\t\t4950\t98\t\t124750\t493"<<endl;
}
void main()
{
const int MAX = 5000;
int arr[MAX], n =0;
char c;
p:
//the main program - selection
cout<<endl<<endl;
cout << "\t:::::::::: S O R T I N G ::::::::::"<<endl<<endl;
cout<<" 1 For Bubsort Click [ b ] "<<endl;
cout<<" 2 For Flagbubble Click [ f ] "<<endl;
cout<<" 3 For Insertion Click [ i ] "<<endl;
cout<<" 4 For Exchangesort Click [ e ] "<<endl;
cout<<" 5 For Sorting Summary Click [ s ] "<<endl;
cout<<" 6 To Escape Click [ x ] "<<endl<<endl<<endl;
cout<<" Enter Your Option : ";
cin>>c;
if(c=='x') // choices
{
cout<<endl<<" You Have Chosen The Exit Option "<<endl<<endl;
exit(1);
}
//error checking
if(!((c=='x')||(c=='b')||(c=='f')||(c=='i')||(c=='e')||(c=='s')))
{
cout<<endl<<" You Have Chosen The Wrong Option "<<endl<<endl;
goto p;
}
if(c=='s')
{
result();
goto p;
}
cout<<"Enter How Many Numbers You Wish To Generate : ";
cin>>n;
if((!(cin))) //error checking if integer
{
cout<<endl<<endl<<"ERROR::You Entered A Non Integer Value"<<endl<<endl;
exit (1);
}
if(n>MAX)
{
n = MAX;
}
//caling and displaying sorts
if(c=='b')
{
cout<<"Numbers Generated... "<<endl;
generate(arr,n,0,999);
display(arr,n);
cout<<endl<<endl;
cout<<"After Sorting... "<<endl;
bubsort(arr,n);
display(arr,n);
cout<<endl<<endl;
cout<<"Number Of Compares : "<<compares(0)<<endl;
cout<<"Number Of Swaps : "<<swaps(0)<<endl<<endl;
goto p;
}
else if(c=='f')
{
cout<<"Numbers Generated... "<<endl;
generate(arr,n,0,999);
display(arr,n);
cout<<endl<<endl;
cout<<"After Sorting... "<<endl;
flagbubble(arr,n);
display(arr,n);
cout<<endl<<endl;
cout<<"Number Of Compares : "<<compares(0)<<endl;
cout<<"Number Of Swaps : "<<swaps(0)<<endl<<endl;
goto p;
}
else if(c=='i')
{
cout<<"Numbers Generated... "<<endl;
generate(arr,n,0,999);
display(arr,n);
cout<<endl<<endl;
cout<<"After Sorting... "<<endl;
insertion(arr,n);
display(arr,n);
cout<<endl<<endl;
cout<<"Number Of Compares : "<<compares(0)<<endl;
cout<<"Number Of Swaps : "<<swaps(0)<<endl<<endl;
goto p;
}
else if(c=='e')
{
cout<<"Numbers Generated... "<<endl;
generate(arr,n,0,999);
display(arr,n);
cout<<endl<<endl;
cout<<"After Sorting... "<<endl;
exchangesort(arr,n);
display(arr,n);
cout<<endl<<endl;
cout<<"Number Of Compares : "<<compares(0)<<endl;
cout<<"Number Of Swaps : "<<swaps(0)<<endl<<endl;
goto p;
}
}
```

*Edited
by happygeek*: fixed formatting

0

thank you so much chandra rajat and localp...I will try try to do your suggestions.

1

just pasting too much code is not going to help anyone, sometimes it can even confuse the person. It's better to give as much info as is asked.

*Edited
by happygeek*: fixed formatting

0

And also quoting it wont help anyone and as you can see the guy just joined the site so there is no way that he can know everything

0

On the quoting part: agreed.

On the new guy excuse: everyone who posts anything here, has to do it in an inputfield. On the background of every inputfield on daniweb, you can read about [code]

[/code] tags (the gray text). The codetags are also explained in the rules, and in a sticky topic on the top of the thread list. So even if someone is a new user, they could know about code-tags with a little effort.

I won't complain about it, if it's someone's first posts but I always give them a link to one of the sticky's so that the next post will be better. If the poster uses code-tags the chance that they will get help will increase, so it's also in their own benefit ;)

0

thanx **nike_e** .. for the helpful links provided . most of the members here provide good solutions to problems but most of them are for the sake of being here. i have seen some giving false explanation to questions posted, this will mislead the person. i have seen some members logging in and writing a reply saying that "the question is not clear can you describe your question" but at most times the question is clear and it seems that the member don't understand it .

if a person is unaware of any posting rules in this site, that is a weakness of the site it self. the site should make sure that all members get to know the rules well. and i agree with **nike_e **..

i am new to this site and i didn't know its rules, but what i wanted to do is to help that person .. any way i am sorry for what i have done , i decided to move out from this site and remove all links that i have included in my company web sites and also unsubscribe form all threads as well .. ( i hope this would help )

good luck all !

This topic has been dead for over six months. Start a new discussion instead.

Recommended Topics

Hi there

I am new to python.

is there a program i could use to make a Clock with day and date in big fonts to help my gran with ...

SO Basically i have this problem with this test build of a little project im rewriting and so far its not looking to good

I have multiple tables that are ...

I just wanna know why mostly bucket sort use the range of element in between 0 and 1 ...