help with a code

I need write a function that takes an array of integers and its size as parameters and fill this vector with random numbers in the range 1 to the size of the vector without repetition. help me please

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  #include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;

main(){
  int N;
  cout<<"informe o tamanho do vetor: ";
  cin>>N; 
  int vet[N]; 
  int i,j,aux;
  srand(time(NULL)); 
  for(i=0;i<N;i++)
     vet[i]=rand()%N;
   
  for(i=0;i<N;i++)
     cout<<vet[i]<<"\t";
  cout<<endl;   
   for(i=0;i<N-1;i++){
     for(j=i+1;j<N;j++){
         if(vet[i]>vet[j]){
             aux=vet[i];
             vet[i]=vet[j];
             vet[j]=aux;
         }
     } 
  }
  
  for(i=0;i<N;i++)
     cout<<vet[i]<<"\t";
  cout<<endl;
  system("pause");
}
To use a vector, you need to #include <vector> .

I would then suggest reading this page twice: http://www.cplusplus.com/reference/vector/vector/

Here is an example of a vector in use:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <vector>
#include <iostream>

int main()
{
    int i = 0;
    std::vector<int> x;//Declare a vector of integers.
    x.push_back(5);//Add the number 5 to the vector.
    std::cout << x.size() << "\n"; //Output the current size of the vector.
    x.push_back(19);//Add the number 19 to the vector.
    std::cout << x.size() << "\n";//Output the current size of the vector (it now has two elements in).
    for(i = 0;i < 10;++i)//Put 10 more elements in the vector, from 1-10.
    {
        x.push_back(i + 1);
    }
    std::cout << x.size() << "\n"; //Output the current size of the vector (which is now 12).
    std::cout << "The elements stored in vector x are: \n";
    for(i = 0;i < x.size(); ++i)
    {
        std::cout << x.at(i) << "\n"; //Output the value of each element held within the vector.
    }
    return 0;
}
Topic archived. No new replies allowed.