I fill vector with random numbers without repeating ones, but when I try to output them, the vector contains only 0s. I tried to clear it before the filling with func .clear() but then the program can't access particular elements in it. When I run the program with debugger it shows that vector contains some numbers, but when it run the func printing() it prints only 0s. I attached screenshots of my tries after code listing.
#include <iostream>
#include <vector>
void filling(std::vector<int> array, int size);
void printing(std::vector<int> array, int size);
int main()
{
srand(time(0));
int sizeA;
printf("Enter size of vector A: ");
std::cin >> sizeA;
std::vector<int> A (sizeA);
filling(A, sizeA);
printf("Vector A: \n\t");
printing(A, sizeA);
}
void filling(std::vector<int> vector, int size)
{
//filling vector without repeating numbers
int temp;
bool flag;
vector[0] = rand() % 31;
for (int i = 1; i < size; )
{
temp = rand() % 31;
flag = true;
for (int j = 0; j < size; j++)
{
if (vector[j] == temp)
{
flag = false;
break;
}
else
{
continue;
}
}
if (flag)
{
vector[i] = temp;
i++;
}
}
}
void printing(std::vector<int> vector, int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ", vector[i]);
}
}
This is the result I get but it's not what I expect:
Elements that debugger shows in vector after filling in the func:
Elements that shows after executing the func filling()