0

I am working on an exercise which entails feeding a vector of int values into a second function to be rearranged so that they are then in reverse order (so if I was feeding in 1, 2, 3, 4 the resulting output would be 4, 3, 2, 1)

// Workshop3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>

int reverse(std::vector<int> a, std::vector<int> b)
{
int count = 0;
int end = a.size() - 1;

while (count < a.size())
{
    b.push_back(a[end]);
    count++;
    end--;
}

    return 1;
}

int main()
{

char blank;

//A simple int value for use in outputting the entire vector
int count = 0;

//Creates the initial vector
std::vector<int> vec{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

std::vector<int> rev;

std::cout << "Initial Vector \n";

//Outputs the vector onto the console
while (count < vec.size())
{
    std::cout << vec[count] << " ";

    count++;
}

std::cout << "\n";
std::cout << "Reversed";
std::cout << "\n";

reverse(vec, rev);

//Outputs the reversed vector
count = 0;

while (count < rev.size())
{
    std::cout << rev[count] << " ";

    count++;
}

std::cin >> blank;

return 0;
}

While the vectors are passed to the reverse function the information is not then returned to the main function after the reversal is completed. When ran the reverse function works as expected with the b vector being filled with the correct values in the correct order but this information is not being passed back to the rev vector in the main function as I would have expected.

I'm pretty sure its something I just haven't added to the code required to actually do this. Could anyone explain what I have done wrong.

If any more clarification is needed please let me know and I will try to elaborate as much as I can.

GiggyLapisar
  • 105
  • 3
  • 10
  • 4
    int reverse(std::vector const & a, std::vector & b) – Alexander Oct 19 '17 at 11:06
  • 4
    Study up on passing by reference. – acraig5075 Oct 19 '17 at 11:07
  • 3
    Except for punctuation and the spelling (but not necessarily meaning) of some keywords, C++ is very different from C#. There is a good book list [here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Don't skip over the things you think you already know. – molbdnilo Oct 19 '17 at 11:14

0 Answers0