# Changing Arrays To Vectors!

• 08-14-2011
JM1082
Changing Arrays To Vectors!
Hi all,

I'm teaching myself C++ from a textbook ( C++ HOW TO PROGRAM by Deitel&Deitel ) but after successfully creating a program using C style arrays I'm having trouble converting it into vetors.

Any help much appreciated!!!

Please see both programs code below:

Code:

``` // recursiveFindMinimum.cpp ( WITH ARRAYS ) #include <iostream> using namespace std; const int arraySize = 5; void initializeArray( int[] ); // to ask the user for numbers int findMinimum( int[], int, int ); // to recursively find the smallest number int main() {     cout << "Welcome!  This program recursively locates the smallest number in an array!" << endl;     int array1[ arraySize ];     int startSubscript = 0;     initializeArray( array1 );     int smallest = array1[ 0 ];     smallest = findMinimum( array1, startSubscript, smallest );     cout << "\nYour smallest number was: " << smallest << "!" << endl; } // end main // to recursively find the smallest number int findMinimum( int array[], int counter, int smallest ) {     while ( counter < arraySize )     {         if ( smallest > array[ counter ] )         {             smallest = array[ counter ];         } // end if         counter++;         findMinimum( array, counter, smallest );     } // end while     return smallest; } // end function findMinimum // to ask the user for numbers void initializeArray( int array[] ) {     cout << endl;     for ( int i = 0; i < arraySize; i++ )     {         cout << "Enter a number into the array: ";         cin >> array[ i ];     } // end for } // end function initializeArray```
Code:

```// recursiveFindMinimumWithVectors.cpp ( WITH VECTORS ) #include <iostream> #include <vector> using namespace std; const int arraySize = 5; void initializeArray( vector < int > ); // to ask the user for numbers int findMinimum( vector < int >, int, int ); // to recursively find the smallest number int main() {     cout << "Welcome!  This program recursively locates the smallest number in an array!" << endl;     vector < int > array1( arraySize );     int startSubscript = 0;     initializeArray( array1 );     int smallest = array1[ 0 ];     smallest = findMinimum( array1, startSubscript, smallest );     cout << "\nYour smallest number was: " << smallest << "!" << endl; } // end main // to recursively find the smallest number int findMinimum( vector < int > array, int counter, int smallest ) {     while ( counter < arraySize )     {         if ( smallest > array[ counter ] )         {             smallest = array[ counter ];         } // end if         counter++;         findMinimum( array, counter, smallest );     } // end while     return smallest; } // end function findMinimum // to ask the user for numbers void initializeArray( vector < int > array ) {     cout << endl;     for ( int i = 0; i < arraySize; i++ )     {         cout << "Enter a number into the array: ";         cin >> array[ i ];     } // end for } // end function initializeArray```
• 08-14-2011
King Mir
What exactly is the problem?

It looks like you're program runs in O(n^2) time when it should be O(n), because your looping and recurring through the array. Just use a loop; no need for recursion here.
• 08-14-2011
grumpy
All you've done is change the types of the declarations from arrays (or pointers) into vector<int> without any further thought. There is much more needed than that: starting with some thought on your part. You might also actually want to read the documentation for the vector, such as this link.

If you want to pass a vector to a function, and change the vector passed, pass it by reference. Passing it by value (which you are) passes a copy of the vector, so the changes are not visible to the caller.

Finding the minimum value of an array using recursion is not a particularly good method. It is a really bad method when you are passing vectors by value around.
• 08-17-2011
AndrewHunter
@OP: If you are learning C++, you should learn about the standard library.
Code:

```#include <iostream> #include <vector> #include <algorithm> int main(void){         //this would be user input         int userints[] = {5,3,40,25,6,1,34};                 std::vector<int>myVector(userints,userints+7);         std::vector<int>::iterator ii;         std::cout<<"Before sort:\n";         for(ii=myVector.begin();ii!=myVector.end();ii++)                 std::cout<<(*ii)<<" ";         std::sort(myVector.begin(),myVector.end());         std::cout<<"\nAfter sort:\n";         for(ii=myVector.begin();ii!=myVector.end();ii++)                 std::cout<<(*ii)<<" ";         return(0); }```
• 08-17-2011
laserlight
Quote:

Originally Posted by AndrewHunter
Code:

`for(ii=myVector.begin();ii!=myVector.end();ii++)`

It probably won't matter because vector iterators may well be pointers, but generally, prefer pre-increment to post-increment since post-increment may be less efficient than pre-increment. Actually, since <algorithm> has already been included, I might write instead:
Code:

```#include <iostream> #include <vector> #include <algorithm> #include <iterator> int main() {     //this would be user input     int userints[] = {5, 3, 40, 25, 6, 1, 34};     std::vector<int> myVector(userints,                               userints + sizeof(userints) / sizeof(userints[0]));     std::cout << "Before sort:\n";     std::copy(myVector.begin(), myVector.end(),               std::ostream_iterator<int>(std::cout, " "));     std::sort(myVector.begin(),myVector.end());     std::cout<<"\nAfter sort:\n";     std::copy(myVector.begin(), myVector.end(),               std::ostream_iterator<int>(std::cout, " "));     std::cout << std::endl; }```
• 08-17-2011
AndrewHunter
@Laser: Thanks for the note on prefix operators. As for your solution, that honestly didn't even cross my mind; thanks for the improvements. :biggrin: