0
#include <iostream>
#include <vector>

using namespace std;

int n;

void fill_progressive(vector<int>v1)
{
    for(int i=1; i<=n; i++)
    {
        v1[i]=i;
    }
}

void print_vector(vector<int>v1)
{
    for(int i=1; i<=n; i++)
    {
        cout<<v1[i]<<", ";
    }
        cout<<"\n";
}
int main()
{      
    cin>>n;
    vector<int> v1(n);
    fill_progressive(v1);
    print_vector(v1);

    return 0;
}

So the main goal of my program is to take a user input and make a vector the size of the user input. I want the vector to contain rising numbers from 1 to n (in: 4 ; out: 1, 2, 3, 4,).

The vector has to be then printed out in order.

I've spend way too much time on making this program work and it does if the 'for' loops are in the main function, however when I use the void functions the program output is 0, 0, 0, 0, 5, (given that the input was 5).

Is there anything I can do to fix that or do I do I have to start from the beginning.

I hope there's a solution to my problem and I'm going to be very grateful if you bother helping me out.

Amadeusz
  • 1
  • 1
  • 3
    `for(int i=1; i<=n; i++)` should be `for(int i=0; i – Jeffrey Mar 13 '22 at 22:03
  • @Jeffrey I've tried that and many other variations in my for loop however none of it seems to work. After I changed my loops into the one you suggested I'm getting all zeros. – Amadeusz Mar 13 '22 at 22:16
  • yes, that's because you also need the part in the duplicate question, pass a reference – Jeffrey Mar 13 '22 at 22:17
  • @Jeffrey ohh so all i needed to do is to put that '&' in there? I'm sorry to take your time but thanks to you I feel like i've found a solution to my problem. Thanks a lot and have a nice day :D – Amadeusz Mar 13 '22 at 22:22
  • `for(int i=1; i<=n; i++)` is definitely wrong. The first element in a vector is at index 0 and the last is at size -1 – drescherjm Mar 14 '22 at 00:31

0 Answers0