EDIT: I messed up the time for this. It should only have a 24 hour window. So fo
ID: 3546029 • Letter: E
Question
EDIT: I messed up the time for this. It should only have a 24 hour window.
So for this problem, I simply need to replace arrays in this program with vectors. There's only a very small example of vectors in my textbook, so I'm not sure how to do this. Help is very much appreciated!!
Here is the code that uses arrays:
#include <iostream>
#include <vector>
void fill_array(int a[], int size, int& number_used);
void sort(int a[], int number_used);
void swap_values(int& v1, int & v2);
int index_of_smallest(const in a[], int start_index, int number_used);
int main ()
{
using namespace std;
cout << "This program sorts numbers from lowest to hightst. ";
int sample_array[10], number_used;
fill_array(sample_array, 10, number_used);
sort(sample_array, number_used);
cout << "In sorted order the numbers are: ";
for (int index = 0; index < number_used; index++)
cout << sample_array[index] << " ";
cout << endl;
system ("pause");
return 0;
}
void fill_array(int a[], int size, int& number_used)
{
using namespace std;
cout << "Enter up to " << size << " nonnegative whole numbers. "
<< "Mark the end of the list with a negative number. ";
int next, index = 0;
cin >> next;
while ((next >= 0) && (index < size))
{
a[index] = next;
index++
cin >> next;
}
number_used = index;
}
void sort(int a[], int number_used)
{
int index_of_next_smallest;
for (int index = 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]);
}
}
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;
}
Explanation / Answer
#include <iostream>
#include <vector>
using namespace std;
void fill_array(vector<int>& a,int size, int& number_used);
void sort(vector<int>& a, int number_used);
void swap_values(int& v1, int & v2);
int index_of_smallest(const vector<int> a, int start_index, int number_used);
int main ()
{
using namespace std;
cout << "This program sorts numbers from lowest to hightst. ";
vector<int> sample_array;
int number_used;
fill_array(sample_array, 10, number_used);
for (int index = 0; index < number_used; index++)
cout << sample_array[index] << " ";
cout << endl;
sort(sample_array, number_used);
cout << "In sorted order the numbers are: ";
for (int index = 0; index < number_used; index++)
cout << sample_array[index] << " ";
cout << endl;
//system ("pause");
return 0;
}
void fill_array(vector<int>& a, int size, int& number_used)
{
using namespace std;
cout << "Enter up to " << size << " nonnegative whole numbers. "
<< "Mark the end of the list with a negative number. ";
int next, index = 0;
cin >> next;
while ((next >= 0) && (index < size))
{
a.push_back(next);
//a[index] = next;
index++;
cin >> next;
}
number_used = index;
}
void sort(vector<int>& a, int number_used)
{
int index_of_next_smallest;
for (int index = 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]);
}
}
void swap_values(int& v1, int& v2)
{
int temp;
temp = v1;
v1 = v2;
v2 = temp;
}
int index_of_smallest(const vector<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;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.