-
-
Save lxr1017/17cc0b7574f004bf41a5fb9c9958e39d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*This program is intended to help you refresh your memory with programming in C++ and consists only of concepts from 146. | |
Write a program that reads the appropriate number of integers into an array, and sorts these numbers in reverse order | |
(decreasing order). The data will consist of 3 sets of data (Please use a loop instead of running the program 3 times). | |
Each set of data begins with an integer that indicates the number of integers on the next newline that are to be read | |
and sorted. So, unnecessary (i.e., to be ignored-not sorted)numbers may exist in the data. | |
The 3rd and 5th will be set up like the 1st. Likewise, the 4th and 6th will be set up like the 2nd. | |
Requirements: | |
Function to print the array of numbers; pass be value must be used | |
Function to sort the numbers in reverse order; pass by reference must be used | |
An array must be used; assume the maximum number of elements to be 100. | |
Extraneous numbers are to be ignored*/ | |
#include <iostream> | |
#include <string> | |
using namespace std; | |
void Sort(double list[], int Size) | |
{ | |
for(int i = 0; i< Size-1; i++) | |
{ | |
double firstMin = list[i]; | |
int firstMinIndex = i; | |
for(int j = i; j< Size; j++) | |
{ | |
if (firstMin > list[j]) | |
{ | |
firstMin = list[j]; | |
firstMinIndex = j; | |
} | |
} | |
if( firstMinIndex != i) | |
{ | |
list[firstMinIndex] = list[i]; | |
list[i] = firstMin; | |
} | |
} | |
} | |
int main() | |
{ | |
double list [] = {1,9,4.5,6.6,5.7,-4.5}; | |
Sort(list,6); | |
cout << "The sorted values are "<< Sort << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment