How to set a range of values from 1 to n?

closed account (j60jNwbp)
Hello!

I was experimenting with setting a range and getting a range of values to and from an array where I wrote this procedure.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>

using namespace std;

int main()
{
    int arr[100];

    // Setting values
    for (int i = 0; i < 100; i++)
    {
        arr[i] = i + 1;
    }

    // Getting values
    for (int i = 0; i < 100; i++)
    {
        cout << arr[i] << endl;
    }
    return 0;
}


I want to modify this program now, so that it asks for user input. The input will be the upper bound in the range. In other words the lower bound will be fixed at 1 and the user will be able to tell the program to list all integers from 1 to 234, or any other number.

It's essentially a list I want. The length of the list will be defined by the user. I thought of something like this:

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
#include<iostream>

using namespace std;

int main()
{
    int lowerBound = 1;
    int upperBound;
    
    cout << "Enter upper bound: " << endl;
    
    cin >> upperBound;
    
    int arr[upperBound];

    // Setting array values
    for (int i=0; lowerBound < upperBound; i++)
    {
        arr[i] = i+1;
        lowerBound++;
    }

    // Getting array values
    for (int i = 0; i < upperBound; i++)
    {
        cout << arr[i] << endl;
    }
    
    return 0;
}


Does this make any sense? It appears to be working but there is something off when I get the last index. If I input 5 for example, I get the numbers 1, 2, 3, 4, and then 16 instead of 5.
Last edited on
closed account (LA48b7Xj)
Maybe use a vector since you don't know the size of the array at compile time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    cout << "Enter number of elements: ";

    int vec_size;
    cin >> vec_size;

    vector<int> v(vec_size);

    for(int i=0; i<v.size(); ++i)
    {
        v[i] = i+1;
        cout << v[i] << '\n';
    }
}

Last edited on
This is when you need dynamic memory. C++ allows declaring static arrays only when the size of the array can be determined at compile time.

Here is a link to the tutorial on this site concerning dynamic arrays:
http://www.cplusplus.com/doc/tutorial/dynamic/
I was wondering about it for an hour and I can't get a solution. Please help cause I am curious!
closed account (j60jNwbp)
I almost had the solution. I just made a single edit and now I have it working. But let me give you my paper debug table first. (I debug my programs with a pen and paper... for now.)

// Setting values
lowerBound  upperBound  i   arr[0]  arr[1]  arr[2]
1           3           0   1
2           3           1           2
3           3           2

// Getting values
lowerBound  upperBound  i   arr[0]  arr[1]  arr[2]
            3           0   1
            3           1           2
            3           2                   ?


I used 3 as input here so I don't have to work all night with this. You can see that the second table has a question mark. This is to indicate that index 2 had no value assigned to it in the first loop. So what can I possibly get by reading from it? I get some random number that just happened to be at that location in memory. In my first example above with 5 as input that number happened to be 16. For input of 3 this time I actually got a huge number...

1
2
7012072


Now that I look at it I see two edits. Anyway! Here is the solution:

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
#include<iostream>

using namespace std;

int main()
{
    int lowerBound = 1;
    int upperBound;

    cout << "Enter upper bound: " << endl;

    cin >> upperBound;

    int arr[upperBound];

    // Setting array values
    for (int i=0; i < upperBound; i++)
    {
        arr[i] = i+1;
    }

    // Getting array values
    for (int i = 0; i < upperBound; i++)
    {
        cout << arr[i] << endl;
    }

    return 0;
}


Try it James! It should work as expected now.
Last edited on
closed account (LA48b7Xj)
a vector is a class template that models a dynamic array.
The pointer syntax is only used when creating the array and when you are done with the array.
1
2
3
4
5
int *arr = new int[size]; //size can now come from an earlier cin! :)

//use arr as you usually would

delete[] arr; //just after you are done with arr 


With vectors this pointer syntax is completely masked behind the implementation of vector.
1
2
3
4
5
6
7
8
9
10
#include <vector>
using namespace std;

//eventually in int main
    vector<int> arr;
    //declare/cin size
    arr.resize(size);

    //use arr as normal now
    //also arr.size() would be the same as the variable size at this point 
Last edited on
There are multiple ways
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>     // std::cout
#include <numeric>      // std::iota
#include <vector>       // std::vector

int main () {
  int lowerBound = 1;
  size_t upperBound = 0;
  std::cin >> upperBound;
  if ( upperBound < 1 ) return 1;

  // dynamic array for upperBound elements
  std::vector<int> numbers( upperBound );
  // fill with [lowerBound..upperBound+lowerBound)
  std::iota( numbers.begin(), numbers.end(), lowerBound );
  // show
  for ( int& i : numbers) std::cout << i << '\n';
  return 0;
}

Topic archived. No new replies allowed.