Question

The output of the "Sorted" is in Ascending. I have been wracking my brain for an...

The output of the "Sorted" is in Ascending. I have been wracking my brain for an hour on how to switch it around and make it print in "descending". Help is appreciated.

//push 15 random integers in the range 0-99
for (int i = 1; i < 15; i++)
v.push_back(rnd.random(100));

cout << "Original:";
writeMiniVector(v);

sortMiniVector(v);

cout << "Sorted:";
writeMiniVector(v);

}


// output miniVector v
template
void writeMiniVector(const miniVector& v)
{
for (int i = 0; i < v.size(); i++)
cout << " " << v[i];

cout << endl << endl;
}
// use insertion sort to place miniVector v to place in a descending order
template
void sortMiniVector(miniVector& v)
{
int i, j, currentVal;
for (i = 1; i < v.size(); i++)
{
currentVal = v[i];
j = i - 1;

///compare with each previous element and swap if previous is greater than the one before
while (j >= 0 && v[j] > currentVal)
{
v[j + 1] = v[j];
j = j - 1;
}
v[j + 1] = currentVal;
  
}
} //end

I need the sorted list to print largest to smallest. It's currently printing smallest to largest. Thanks

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The mistake was when comparing v[j] value with currentVal, please check that v[j] is less than currentVal for descending order, I have highlighted that code below. Now the issue is solved.

//push 15 random integers in the range 0-99
for (int i = 1; i < 15; i++)
v.push_back(rnd.random(100));

cout << "Original:";
writeMiniVector(v);

sortMiniVector(v);

cout << "Sorted:";
writeMiniVector(v);

}


// output miniVector v
template
void writeMiniVector(const miniVector& v)
{
for (int i = 0; i < v.size(); i++)
cout << " " << v[i];

cout << endl << endl;
}
// use insertion sort to place miniVector v to place in a descending order
template
void sortMiniVector(miniVector& v)
{
int i, j, currentVal;
for (i = 1; i < v.size(); i++)
{
currentVal = v[i];
j = i - 1;

///compare with each previous element and swap if previous is greater than the one before
while (j >= 0 && v[j] < currentVal)
{
v[j + 1] = v[j];
j = j - 1;
}
v[j + 1] = currentVal;
  
}
} //end

Add a comment
Know the answer?
Add Answer to:
The output of the "Sorted" is in Ascending. I have been wracking my brain for an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help fixing my code: In C++ *************** 1) I want to sum the digits...

    I need help fixing my code: In C++ *************** 1) I want to sum the digits of an n*n matrix 2) find the average I have completed the rest ****Do not use C++ standard library. You must use pointers and pointer arithmetic to represent the matrix and to navigate through it. MY CODE: (I have indicated at which point I need help) #include <iostream> using namespace std; void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp;...

  • The goal is to generate some random number and output a sorted list by quicksort. what...

    The goal is to generate some random number and output a sorted list by quicksort. what do I need to add in my main to accomplish that? i also want it to output the run time. thank you #include "pch.h" #include <iostream> using namespace std; /* C implementation QuickSort */ #include<stdio.h> void swap(int* a, int* b) {    int t = *a;    *a = *b;    *b = t; } int partition(int arr[], int low, int high) {   ...

  • where is the error in my code? it wont print all positions in the array for...

    where is the error in my code? it wont print all positions in the array for ascending (ralph is missing) or when it lists who got onto the elevator. This is C++. Input Data Anne 150 Bob 250 Ralph 305 Tim 250 Barbara 85 Jane 160 Steve 180 Tom 210 Mike 165 Shirley 155 Pam 125 Frank 130 Code #include <iostream> #include <fstream> using namespace std; /* The purpose of this program is to compare different ways for people to...

  • Language C++ (Please include a short description & Screenshot of output) Implement a Priority...

    Language C++ (Please include a short description & Screenshot of output) Implement a Priority queue using a SORTED list. Use Quick sort after adding a new node. Example of quick sort below. Adopt to your program the code below. #include <iostream> void quickSort(int a[ ], int first, int last); int pivot(int a[], int first, int last); void swap(int& a, int& b); void swapNoTemp(int& a, int& b); void print(int array[], const int& N); using namespace std; int main() { int test[]...

  • How do I do this? -> string print() const; Function to output the data, return the...

    How do I do this? -> string print() const; Function to output the data, return the output in the form of string, with all elements in the hash table included and each seperated by a space this is my code: template <class elemType> string hashT<elemType>::print() const { for (int i = 0; i < HTSize; i++){        if (indexStatusList[i] == 1){        cout <<HTable[i]<< " " << endl;        }   }    } **********Rest of the code...

  • Any help in the compiler error //Driver Program //***************** /** * * CSCI 241 Assignment 8...

    Any help in the compiler error //Driver Program //***************** /** * * CSCI 241 Assignment 8, Part 3 * * Author: your name * z-ID: your z-ID * Date: due date of assignment * * This program builds, sorts and prints lists using the quicksort and * merge sort algorithms. */ #include <iostream> #include <iomanip> #include <vector> #include <string> #include "sorts.h" #include "quicksort.h" #include "mergesort.h" using std::cout; using std::fixed; using std::left; using std::setprecision; using std::string; using std::vector; // Data files...

  • Add binary_search() (page 462) to your program. Modify main() to prompt the user for a number...

    Add binary_search() (page 462) to your program. Modify main() to prompt the user for a number to search (until ^D) and display the position of the number in the sorted vector. Try your program for the following user input: 1 15 18 40 30 50 ^D The output should be: -1 2 -1 7 5 -1 int binary_search(vector<int> v, int from, int to, int value) { if (from > to) return -1; int mid = (from + to) / 2;...

  • C++ Create a program that finds the dot product of two vectors. I'm currently trying to...

    C++ Create a program that finds the dot product of two vectors. I'm currently trying to display the dot product by calling the dotProduct member function however I am confused as to how to do this. What would be the proper way to display the dot product? I don't believe my dotProduct member function is set up correctly to access the proper data. Feel free to modify the dotProduct member function to allow for it to work with the other...

  • 81. The following function call doesn’t agree with its prototype:              cout << BoxVolume( 10, 5...

    81. The following function call doesn’t agree with its prototype:              cout << BoxVolume( 10, 5 );                    int BoxVolume(int length = {1}, int width = {1}, int height = {1});                                                     T__   F__                                                                     82. The following function is implemented to swap in memory the          argument-values passed to it:         void swap(int a, int b)                   {           int temp;             temp = a;             a = b;             b = temp;        ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT