Selection sort / Number Swapping

Good day guys, im working on this challenge which says:

Create a Menu driven program with the following options: Input Numbers, Print Ascending
Order, Print Descending Order, Exit.
Input numbers - This option allows the user to enter as many numbers in any order. After each entry the user will be asked if he/she wants to enter another number.

So far i have the following code: //
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

#include <iostream>          
using namespace std;

void selectionSort (double list [], int listSize ) {
	
	for (int i = 0; i < listSize - 1; i++) {
		
		// Finding the minimum in the list [i...listSIze - 1]
		double currentMin = list [i];// This variable will hold all the numbers
		int currentMinIndex = i; // This variable will hold the index of each iterration
		
		for ( int j = i + 1; j < listSize; j++ ) { // this loop will start at the same
			
			if ( currentMin > list [j] ) {
				
				currentMin = list [j];
				currentMinIndex = j;
			}
		}
		
		// Swapping list [i] with list [currentMinIndex] if necessary;
		if ( currentMinIndex != i ) {
			
			list [currentMinIndex] = list [i];
			list [i] = currentMin;
		}
	}
}


int main () {
	
	int exit = 1;
	
	cout << "\tHow mnay values do you wish to enter? " << endl << endl;
	int size = 0;
	cin >> size;
	double list [size];
	
	
	for (int i = 0; i < size; i++) {
		
		cout << "\tEnter a number: ";
		cin >> list [i];	
		
		selectionSort( list [i], size);

	}
	


	return 0;
}


I have 2 main issues. First, I do not know how code the answer for allowing the user to enter as many numbers in any order. The way I understand it is that it will require an array or vector size to keep changing; I tried with a while loop:


int number = 1;
while ( number !=0 ) {


int i = 0;
cout << "Enter a number"; i++;

int catch [i];
cin >> caatch [i];

This code will run but then crash. I dont know about this particular issue. So what I did was asking for an amount of values to be entered and then making that value the size of the array as coded above, but I will would like know how to solve this the way it is asked.

Second, the compiler (dev c++) gives me this error:
53 32 {Error] cannot convert 'double' to 'double*' for argument '1' to 'void selectionSort(double*, int)'

The code I copied from a text book. I would gladly appreciate your help.
I do not know how code the answer for allowing the user to enter as many numbers in any order. The way I understand it is that it will require an array or vector size to keep changing; I tried with a while loop:

For this propose the standard library provides std::vector, which provides dynamically growing size.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <vector>
#include <sstream>
#include <string>
#include <iostream>
int main () {
	
    std::vector<double> v;
    std::cout << "Type in as many numbers you want, and then press <Enter>\n";
    std::string line;
    std::getline(cin,  line);
    std::istringstream iss(line);
    double tmp;
    while (iss >> tmp)  v.push_back(tmp);
    selectionSort( &v.front(), v.size());
    for ( auto value : v) std::cout << value << ' ';
}

Second, the compiler (dev c++) gives me this error:
53 32 {Error] cannot convert 'double' to 'double*' for argument '1' to 'void selectionSort(double*, int)'
At line 47 you try passing the i-th element of 'list', not the list-array at all.
Last edited on
Topic archived. No new replies allowed.