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?
#include <vector>
#include <iostream>
usingnamespace 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 (unsignedint i = 0; i < v.size( ); i++)
cout << v[i] << " ";
cout << endl;
sort(v);
cout << "In sorted order the numbers are:\n";
for (unsignedint 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;
}
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