C++: Trying to get a sortAccounts() function working where it will sort all of t
ID: 3802261 • Letter: C
Question
C++: Trying to get a sortAccounts() function working where it will sort all of the vector structures based on the value of the accountNumber. So far, here is my structure, makeAccount(), and sortAccounts() function. I posted this question previously, but I am not allowed for this assignment to have an extra function to assist in the sorting. All sorting must be done in the sortAccount() function. Thanks.
I tried to make something like above, but it doesn't work as I would like it to and I don't think this would be the correct approach. I was attepting to use sort() but also couldn't get that to work the way I wanted it to.
struct Account int accountNumber; string lastName string first Name double account Balance vectorExplanation / Answer
To sort the elements of a vector, you need to do it repeatedly.
But your logic will just push the largest account number to the right most.
Actually, you're on the right path. But this should be done repeatedly. In the first loop, push the largest element to the right most, as you did. In the next loop, push the next largest element to the second right. And this should happen n-1 times, to push n-1 largest elements to their respective places.
Try this swapping technique...
void sortAccounts()
{
for(int i = 0; i < bankAccounts.size()-1; i++)
for(int j = 0; j < bankAccount.size()-i-1; j++)
if(bankAccounts[j].accountNumber > bankAccounts[j+1].accountNumber)
{
Account temp = bankAccounts[j];
bankAccounts[j] = bankAccounts[j+1];
bankAccounts[j+1] = temp;
}
}
Hopefully this should work.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.