hi everybody!!
well iam taking this c++ class and we r doing this problem that is givin me such a headache so i trying to get some help , topic are string and vectors. ok this is what i have to far all the precondition and postcondition are given becuase we barely study vector so this is the only problem using them and i have no idea waht else to do!!!!
please any help we'll be appreciated!!

``````// **************************************************************************

#include <iostream>
#include <vector>
using namespace std;

void fillVector(vector<int> &v);
//POSTCONDITION: v[0] through v[v.size() - 1] have been filled with
//nonnegative integers read from the keyboard.

void sort(vector<int> &v);
//POSTCONDITION: The values of v[0] through v[v.size()-1] have
//been rearranged so that v[0] <= v[1] <= ... <= v[v.size() - 1].

void swapValues(int& v1, int& v2);
//PRECONDITION: v1 and v2 have values.
//POSTCONDITION: The values of v1 and v2 are interchanged.

int indexOfSmallest(const vector<int> &v, int startIndex);
//PRECONDITION: 0 <= startIndex < v.size().
//POSTCONDITION:Returns the index i such that v is the smallest
//of the values v[startIndex], v[startIndex + 1], ..., v[v.size()-1].

int main( )
{
using namespace std;
cout << "This program sorts numbers from lowest to highest.\n";

vector<int> sampleVector;
fillVector(sampleVector);
sort(sampleVector);

cout << "In sorted order the numbers are:\n";
for (int index = 0; index < sampleVector.size(); index++)
cout << sampleVector[index] << " ";
cout << endl;

return 0;
}

void fillVector(vector<int> &v)
{
using namespace std;
cout << "Enter as many nonnegative whole numbers as you like.\n"
<< "Mark the end of the list with a negative number.\n";

int next;
cin >> next;
while (next >= 0)
{
v.push_back(next);
cin >> next;
}
}
void sort(vector<int> &v)
//POSTCONDITION: The values of v[0] through v[v.size()-1] have
//been rearranged so that v[0] <= v[1] <= ... <= v[v.size() - 1].
{
int index_of_next_smallest;
for(int index=0;index<v[v.size() - 1];index++);
{
index_of_next_smallest= (indexOfSmallest( vector<int> v,startIndex);
swap_values(v[index], v[index_of_next_smallest]);
}
}

void swapValues(int& v1, int& v2)
{
int temp;
temp = v1;
v1 = v2;
v2 = temp;
}
int indexOfSmallest(const vector<int> &v, int startIndex)
{

//PRECONDITION: 0 <= startIndex < v.size().
//POSTCONDITION:Returns the index i such that v is the smallest
//of the values v[startIndex], v[startIndex + 1], ..., v[v.size()-1].

for (unsigned int i=0; i<v.size();i++)
cout <<v << "";
cout << endl;
}
// **************************************************************************
``````

thanks!!!

## All 5 Replies

I've not looked at this properly but the following appears to be where the big problems lie:-

``````void sort ( vector<int> &v )
{
int index_of_next_smallest;
for ( int index = 0; index < v[v.size() - 1]; index++ );
{
index_of_next_smallest = ( indexOfSmallest ( vector<int> v, startIndex );
swap_values ( v[index], v[index_of_next_smallest] );
}
}``````

Rogue semi colons and I don't think you're passing the vector properly either, or calling some of the functions correctly.

wazzzzuup!! sorry about the lazy title my apologizes,
and your right i did not pay attencion with the semicolon those two did not supposed to be there. but still can get it to work.
mmmmmmmm when i am doing the piece of code in void sort
change the code to this but gives a error

``````void sort(vector<int> &v)
//POSTCONDITION: The values of v[0] through v[v.size()-1] have
//been rearranged so that v[0] <= v[1] <= ... <= v[v.size() - 1].
{
int index_of_next_smallest;
for(int index=0;index<v[v.size() - 1];index++)
{
index_of_next_smallest= indexOfSmallest(  v, startIndex);
swap_values(v[index], v[index_of_next_smallest]);
}
}

and th error is:  In function `void sort(std::vector<int, std::allocator<int> >&)':``````

any idea anyone????
thanks

Another question you might like to ask yourself is where are you actually comparing one value against the other, or is that supposed to be your indexOfSmallest function? If it is, is it doing what it is supposed to be doing. Is it supposed to return something?

so, should i return istartindex from the int indexOfSmallest????

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.