Changing Arrays To Vectors!

This is a discussion on Changing Arrays To Vectors! within the C++ Programming forums, part of the General Programming Boards category; Hi all, I'm teaching myself C++ from a textbook ( C++ HOW TO PROGRAM by Deitel&Deitel ) but after successfully ...

  1. #1
    Registered User JM1082's Avatar
    Join Date
    Mar 2011
    Posts
    51

    Question 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

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,010
    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.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    5,827
    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.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    @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);
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,960
    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;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    @Laser: Thanks for the note on prefix operators. As for your solution, that honestly didn't even cross my mind; thanks for the improvements.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors in Arrays
    By Osiris990 in forum C++ Programming
    Replies: 5
    Last Post: 02-25-2008, 01:48 PM
  2. Arrays vs Vectors
    By swgh in forum C++ Programming
    Replies: 5
    Last Post: 05-04-2006, 02:06 AM
  3. byte arrays & vectors
    By kasun in forum C++ Programming
    Replies: 1
    Last Post: 02-29-2004, 08:10 AM
  4. arrays or vectors
    By Geo-Fry in forum C++ Programming
    Replies: 26
    Last Post: 04-17-2003, 07:08 PM
  5. arrays and vectors
    By volk in forum C++ Programming
    Replies: 1
    Last Post: 03-30-2003, 02:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21