954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

descending

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

nurulshidanoni
Posting Whiz in Training
219 posts since Nov 2007
Reputation Points: 9
Solved Threads: 0
 

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

Sky Diploma
Practically a Posting Shark
865 posts since Mar 2008
Reputation Points: 673
Solved Threads: 131
 

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;
}
}
nurulshidanoni
Posting Whiz in Training
219 posts since Nov 2007
Reputation Points: 9
Solved Threads: 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?

Agni
Practically a Master Poster
655 posts since Dec 2007
Reputation Points: 431
Solved Threads: 116
 

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;	}
nurulshidanoni
Posting Whiz in Training
219 posts since Nov 2007
Reputation Points: 9
Solved Threads: 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.

Agni
Practically a Master Poster
655 posts since Dec 2007
Reputation Points: 431
Solved Threads: 116
 

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

/* 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
#include

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 = 0 ; j--)
{
for(int i =0; i <= j;i++)
{
if(notinorder(arr[i],arr[i+1]))
{
swap(arr[i],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[i],arr[i+1]))
{
stillwapping = true;
swap(arr[i],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>c;

if(c=='x') // choices
{
cout<>n;
if((!(cin))) //error checking if integer
{
cout<MAX)
{
n = MAX;
}
//caling and displaying sorts
if(c=='b')
{
cout<<"Numbers Generated... "<

localp
Junior Poster
191 posts since Apr 2008
Reputation Points: 1
Solved Threads: 3
 

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

nurulshidanoni
Posting Whiz in Training
219 posts since Nov 2007
Reputation Points: 9
Solved Threads: 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

/* 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 #include

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 = 0 ; j--) { for(int i =0; i <= j;i++) { if(notinorder(arr[i],arr[i+1])) { swap(arr[i],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[i],arr[i+1])) { stillwapping = true; swap(arr[i],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>c; if(c=='x') // choices { cout<>n; if((!(cin))) //error checking if integer { cout<MAX) { n = MAX; } //caling and displaying sorts if(c=='b') { cout<<"Numbers Generated... "<

Please use 'Code Tags', i dont know how many times the mods have been telling this.

secondly 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.

Agni
Practically a Master Poster
655 posts since Dec 2007
Reputation Points: 431
Solved Threads: 116
 

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

Traicey
Posting Whiz in Training
283 posts since Mar 2008
Reputation Points: 26
Solved Threads: 19
 
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

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 ;)

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

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 !

localp
Junior Poster
191 posts since Apr 2008
Reputation Points: 1
Solved Threads: 3
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You