C++ Help

I need help finishing an assignment. How to pass my array to the function and return it as a sorted vector. I'm kind of lost with the functions part.

Problem:
Write a program that performs a sorting routine as discussed in class. Create
an array of 10 integers (e.g., 63, 42, 27, 111, 55, 14, 66, 9, 75, 87). Pass
this array to a separate function that sorts the integers and returns a vector
containing the sorted integers. Use either the SELECTIONSORT algorithm or the
INSERTIONSORT.

Code so far:
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iomanip>


using namespace std;

vector<int> fillvector(int[], int);
void selectionsort(int[], int);


// main function ***********************************************************

int main ()
{
int array[] = {63, 42, 27, 111, 55, 14, 66, 9, 75, 87};
vector<int> myvector1;
myvector1 = fillvector(array, 10);

for(int i=0; i < myvector1.size(); ++i)
{
cout << i+1 << " : " << myvector1[i] << endl;
}



// End of program statements
cout << "Please press enter once or twice to continue...";
cin.ignore().get(); // hold console window open
return EXIT_SUCCESS; // successful termination

}
// end main() **************************************************************

vector<int> fillvector(int array[], int size)
{
vector<int> myvector(size);
insertionsort(array,size);

for(int i=0; i < size; ++i)
{
myvector[i] = array[i];
}

return myvector;
}

void sort(int[],int)
{

}

If ANYONE could help me I'd truly appreciate it!!
Last edited on
It looks good so far. You just need to write the insertionsort() function. Review your notes or a book on how this algorithm works and try to code it.
please use code brackets. It makes the code easier to read and copy.
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
55
56
57
58
59
#include <iostream>
#include <string>
#include <vector> 
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iomanip>


using namespace std;

vector<int> fillvector(int[], int);
void selectionsort(int[], int);
void insertionsort();

// main function ***********************************************************

int main ()
{	
int array[] = {63, 42, 27, 111, 55, 14, 66, 9, 75, 87};
vector<int> myvector1; 
myvector1 = fillvector(array, 10);

for(int i=0; i < myvector1.size(); ++i)
{
cout << i+1 << " : " << myvector1[i] << endl;
}



// End of program statements
cout << "Please press enter once or twice to continue...";
cin.ignore().get(); // hold console window open
return EXIT_SUCCESS; // successful termination

}
// end main() **************************************************************

vector<int> fillvector(int array[], int size)
{
vector<int> myvector(size);
insertionsort(array,size);

for(int i=0; i < size; ++i)
{
myvector[i] = array[i];
}

return myvector;
}

void sort(int[],int)
{

}
void insertionsort()
{

}
Last edited on
Topic archived. No new replies allowed.