help with vectors

I changed this function for arrays to accept an argument of type vector but the function does not do what it is supposed to. Can some one please help me get it running correctly?

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <vector>
#include <iostream>
using namespace std;

void sort( vector<int>& v);


void swap_values(int& v1, int& v2);
//Interchanges the values of v1 and v2.

int index_of_smallest( vector<int> v, int start_index, int number_used);
//Precondition: 0 <= start_index < number_used. Referenced array elements have 
//values.
//Returns the index i such that a[i] is the smallest of the values
//a[start_index], a[start_index + 1], ..., a[number_used - 1].

int main( )
{
    
     vector<int> v;
     int number_used = v.size();
    cout << "This program sorts numbers from lowest to highest.\n"
         << "Enter a list of positive numbers.\n"
         << "Place a negative number at the end.\n";
    int next;
    cin >> next;
    while (next > 0)
    {
        v.push_back(next);
        cin >> next;
    }

    cout << "You entered:\n";
    for (unsigned int i = 0; i < v.size( ); i++)
        cout << v[i] << " ";
    cout << endl;
     sort(v);
    cout << "In sorted order the numbers are:\n";
    for (unsigned int i = 0; i < v.size( ); i++)
        cout << v[i] << " ";
    cout << endl;
system("pause");
    return 0;
}

void sort( vector<int>& v)
{
     int number_used = v.size();
    int index_of_next_smallest;


    for (int index = 0; index < number_used - 1; index++)
    {//Place the correct value in a[index]:
        index_of_next_smallest =
                     index_of_smallest(v, index, number_used);
        swap_values(v[index], v[index_of_next_smallest]);
        //a[0] <= a[1] <=...<= a[index] are the smallest of the original array 
        //elements. The rest of the elements are in the remaining positions.
    }
}


void swap_values(int& v1, int& v2)
{
    int temp;
    temp = v1;
    v1 = v2;
    v2 = temp;
}


int index_of_smallest( vector<int> v, int start_index, int number_used)
{
    int min = v[start_index],
        index_of_min = start_index;
    for (int index = start_index + 1; index < number_used; index++)
        if (v[index] < min)
        {
            min = v[index];
            index_of_min = index;
            //min is the smallest of a[start_index] through a[index]
        }

    return index_of_min;
}


Last edited on
what kinda output do you want?
i was expecting the vector to be sorted from lowest to highest but instead the output was the same as the input.
http://www.cplusplus.com/reference/algorithm/sort/

Or consider why not passing by reference in your sort method is bad.
Last edited on
for this one i cant use /algorithm/sort i was given a function that was for an array and the assignment is to turn it into a function that will accept a vector my function does accept the vector it just doesn't do anything to it.

Just got it all i had to do was pass by reference to the sort function and it works perfectly now
Last edited on
Topic archived. No new replies allowed.