Vector help!

To start off im new to this forum and im a noob in c++. im currently taking college courses. So to begin.. i have a homework assignment that i turned in last night. BUT i want to understand vectors, the assignment that i turned in was throwing an error that i could not figure out so just for my benefit i want to start fresh on this one so i can understand! so my assignment asks:"Write a sorting function that is similar to Display 7.12, except that is has an argument for a vector of ints rather than an array. This function will not need a parameter like number_used as in display 7.12, since a vector can determine the number used with the member function size(). This sort function will have only this one parameter, which will be a vector type. Use the selection sort algorithm(which was used in Display 7.12)" i posted the program with the original display 7.12 function but i commented that out, and the program is ready for vectors, i just dont know if my function is right. so can you guys help me understand how to use vectors correctly!? Thanks! =D

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
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <vector>
using namespace std;

void fill_array(int a[] ,int size, int& number_used);
// PRECONDITION: number declared size of array a.
// POSTCONDITION: number_used is the number of values stored in a
//a[0] through a[number_used-1] have been filled with nonnegative int.

//void sort(a[], int number_used); //<------- original display 7.12 sort function.

void sort(vector<int> aVector); 
// PRECONDITION: takes the declared size .
// the vector elements in the vector all have values.
// POSTCONDITION: rearranges the vector or array so that a[0] is <= a[1].... <= a[size -1],
void swap_values(int &v1, int &v2);
//interchanges the valies of v1 and v2
int index_of_smallest(const int a[], int start_index, int number_used);
//PRECONDITION: 0<= start_index < number_used. refferenced array elements have values.
// returns te index i such that a[i] is the smallest of the values.
int main()
{
    using namespace std;
    
    cout << "This program sorts number from lowest to highest.\n";
    
    int sample_array[10], number_used;
    fill_array(sample_array, 10, number_used);
   
    
    cout << " In sorted order the numbers are:\n";
    for (int index = 0; index < number_used; index++)
    cout << sample_array[index] << " ";
    cout << endl;
    
    system("PAUSE");
    return 0;
}// end main

void fill_array(int a[], int size, int& number_used)
{
     using namespace std;
     cout << "Enter up to " << size << " nonnegative whole numbers.\n"
          << "Mark the end of the list with a negative number.\n";
          
     int next, index = 0;
     cin >> next;
     while ((next >= 0) && (index < size))
     {
           a[index] = next;
           index++;
           cin >> next;
     }
     number_used = index;
}

void sort (vector<int> aVector) // sorts the list of numbers using vectors
{
     
     
      
     // Sort function using vector coding goes here
     
     
     
}
// Display 7.12 original function using ARRAY.
/*void sort(int a[], int number_used)
{
     int index_of_next_smallest;
     for (intindex = 0; index < number_used - 1; index++)
     {
         index_of_next_smallest = index_of_smallest(a, index, number_used);
         swap_values(a[index], a[index_of_next_smallest);
     
     }// end for
}// end sort */

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

int index_of_smallest(const int a[], int start_index, int number_used)
{
    int min = a[start_index], index_of_min = start_index;
    for (int index = start_index + 1; index < number_used; index++)
        if (a[index] < min)
        {
           min = a[index];
           index_of_min = index;
        }
    
    return index_of_min;
}
Topic archived. No new replies allowed.