NEED HELP ASAP PLS

Hey guys, im a newbie and im having problems with my first assignment in school . my professor wants to "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 " . my code already works , but its only with arrays, i want it to pass it into a vector. i need help please.
#include <iostream>
#include <utility>

using namespace std;

void sortArray(int size, int array[])
{
//indexFin(size - 1) is the last array element
for (int indexfin(size - 1); indexfin < size; --indexfin)
{
/*The last step compares fin-1 and fin therefore
< is used instead of <= which would go out of range*/
for (int index(0); index < indexfin; ++index)
{
//Compares index and index + 1
if (array[index] > array[index + 1])
std::swap(array[index], array[index + 1]);
//else: no changes are needed
}
}
}

int main()
{
//Given array
const int size(10);
int array[size] = {77, 99, 33, 44, 55, 66, 11, 88, 22,110};


cout << "Unsorted integers :" << endl ;
for(int i = 0; i < size ; i++ )
{ cout << array[i];
cout << " "; }

cout << "\n" << endl;


cout << "Sorted integers :" << endl ;
sortArray(size, array);

//Prints result:
for (auto index : array)
std::cout << index << " ";

return 0;
}
Last edited on
Well, reading your teacher's instructions we can see that he wants you to return a vector of sorted integers, not pass one in. Also next time please use code tags as they help reading code easier for everyone here, more so it just looks better. Anyways, here is the fixed code with comments.

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
#include <iostream>
#include <vector> // used for vector
using namespace std;

std::vector<int> sortArray(int size, int array[])
{
    //Construct a vector of integers which will copy the numbers from array.
    std::vector<int> sortedNumbers(array, array + size);
    //indexFin(size - 1) is the last array element
    for (int indexfin(size - 1); indexfin < size; --indexfin)
    {
        /*The last step compares fin-1 and fin therefore
        < is used instead of <= which would go out of range*/
        for (int index(0); index < indexfin; ++index)
        {
            //Compares index and index + 1
            if (sortedNumbers[index] > sortedNumbers[index + 1])
                std::swap(sortedNumbers[index], sortedNumbers[index + 1]);
            //else: no changes are needed
        }
    }
    //return the vector of sorted integers.
    return sortedNumbers;
}

int main()
{
    //Given array
    const int size(10);
    int array[size] = {77, 99, 33, 44, 55, 66, 11, 88, 22,110};
    
    
    cout << "Unsorted integers :" << endl ;
    for(int i = 0; i < size ; i++ )
    {
        cout << array[i];
        cout << " ";
    }
    
    cout << "\n" << endl;
    
    
    cout << "Sorted integers :" << endl ;
    auto sortedNumbers = sortArray(size, array);
    
    //Prints result:
    for (auto num : sortedNumbers)
        std::cout << num << " ";
    
    return 0;
}
Topic archived. No new replies allowed.