Results 1 to 9 of 9

Thread: C++

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date: Jul:2014
    Location: Dobrich
    Posts: 10

    C++

    . 1,2,3,4,5,6,8,10, 2,4,6,8,10. 100% .

    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    vector<int> copyElements(vector<int> array, vector<int> array2, int n, int i)
    {
    
        if (n == array.size())
        {
            return array;
        }
        else
        {
            if (array[n] % 2 == 0)
            {
                array2[i] = array[n];
                i++;
            }
    
            return copyElements(array, array2, n + 1, i);
        }
    
    }
    
    int main()
    {
        //initialize the two vectors
        vector<int> array;
        for(int i = 0; i < 8; i++)
        {
            array.push_back(i);
        }
        vector<int> array2;
    
        copyElements(array, array2, 0, 0);
    
        return 0;
    }

  2. #2
    Registered User
    Join Date: Jan:2005
    Location: BG
    Posts: 4,903
    . , ... ?
    , , ... ...

  3. #3
    Registered User hateras's Avatar
    Join Date: Jan:2011
    Location: Kronos III
    Posts: 1,028
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    vector<int> copyElements(vector<int> array1, vector<int> & array2, int n, int i)
    {
        if (n == array1.size())
        {
            return array1;
        }
        else
        {
            if (array1[n] % 2 == 0)
            {
                array2.push_back(array1[n]);
                i++; //
            }
    
            return copyElements(array1, array2, n + 1, i);
        }
    
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	vector<int> array1;
        for(int i = 0; i < 8; i++)
        {
            array1.push_back(i);
        }
        vector<int> array2;
    
        copyElements(array1, array2, 0, 0);
    
    	//print out vector2
    	for(int i = 0; i < array2.size(); i++) {
    		std::cout << "vector2[" << i << "]=" << array2[i] << endl;
    	}
    	return 0;
    }
    :
    - array array1 /array - , .../
    - array2 , . /reference/ . ,
    - vector2 , , .. array2[i] / - /

    @flancer
    , , / /
    ASRock B550M Pro 4; Ryzen R5 3600; 2x16 GiB G.SKILL Aegis 3200; 1TB Samsung QVO 960 + 3TB Seagate IronWolf; Zalman Z1

  4. #4
    Registered User
    Join Date: Jan:2010
    Location: localhost:502
    Posts: 153
    .. -!
    array2[i] = array[n]; !? !?
    ...

  5. #5
    Registered User hateras's Avatar
    Join Date: Jan:2011
    Location: Kronos III
    Posts: 1,028
    Quote Originally Posted by uf32 View Post
    .. -!
    array2[i] = array[n]; !? !?
    ...
    /, ... / , vector .
    ASRock B550M Pro 4; Ryzen R5 3600; 2x16 GiB G.SKILL Aegis 3200; 1TB Samsung QVO 960 + 3TB Seagate IronWolf; Zalman Z1

  6. #6
    Registered User
    Join Date: Jan:2005
    Location: BG
    Posts: 4,903
    , " ". ... , . ,
    Last edited by flancer; 26th January 2015 at 14:48.
    , , ... ...

  7. #7
    Registered User ined's Avatar
    Join Date: Nov:2006
    Location: Varna
    Posts: 1,078
    .
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    void copyElements(vector<int> array1, vector<int> & array2, int n)
    {
        if (n < array1.size()) {
           if  (array1[n]%2==0) array2.push_back(array1[n]);
           copyElements(array1, array2, n + 1);
        }
    }
    
    int main()
    {
        vector<int> array1;
        for(int i = 1; i < 11; i++)
        {
            array1.push_back(i);
        }
        vector<int> array2;
    
        copyElements(array1, array2, 0);
    
        //print out vector2
        for (int i = 0; i < array2.size(); i++) {
    	cout << "vector2[" << i << "]=" << array2[i] << endl;
        }
        return 0;
    }
    Last edited by ined; 26th January 2015 at 00:51.
    ASRock B85 Pro4, Pentium G3240, DD3 8GB/1333MHz, 120GB SSD + 1TB + 640GB HDD
    Gigabyte GV-R657OC-1GI, CM B500, ASUS 24B1S1, LCD SAMSUNG SM931BW, 173

  8. #8
    Registered User hateras's Avatar
    Join Date: Jan:2011
    Location: Kronos III
    Posts: 1,028
    @ined , .
    , . .
    ASRock B550M Pro 4; Ryzen R5 3600; 2x16 GiB G.SKILL Aegis 3200; 1TB Samsung QVO 960 + 3TB Seagate IronWolf; Zalman Z1

  9. #9
    -==-
    Join Date: Sep:2003
    Location:
    Posts: 6,444
    Quote Originally Posted by Simeon Angelov View Post
    ...
    . .
    He who asks is a fool for five minutes, but he who does not ask reamins a fool forever. Old Chinese saying
    Quote Originally Posted by worm4 View Post
    a .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Copyright © 1999-2011 . .
iskamPC.com | mobility.BG | Bloody's Techblog | | 3D Vision Blog |