# Thread: Size of Array, user inputted..

1. ## Size of Array, user inputted..

I am familiar with basic arrays...
Code:
`int list[50]`
Creates an array named list, that is 50 long.

How do I go about coding an array that is the length that the user inputs?

I thought about declaring a variable length, and then doing this:
Code:
`int list[length];`
So when the program would ask, "How long do you wish to make the array?", if the user inputted 27, the array would be 27 long..

...but that of course didn't work. Whats the best way to achieve this?

thanks all =)

2. Dynamically allocate the array
Code:
`int * list = new int[length];`
and dont forget to
Code:
`delete [] list;`
when youre done with the array
Kurt

3. I think vectors would be best fit for the job
http://www.cplusplus.com/reference/stl/vector/

4. You should read a few tutorials about pointers/memory allocation, before using them

5. Originally Posted by ZuK
Dynamically allocate the array
Code:
`int * list = new int[length];`
and dont forget to
Code:
`delete [] list;`
when youre done with the array
Kurt
Im having trouble figuring out how to take an int input from a user and create the array with that amount..

Code:
```#include <iostream>
#include <string>
#include <algorithm>

int length;
int * list = new int[length];

int main()
{

std::cout << "How many numbers do you wish to input?: ";
std::cin >> length;

return (0);
}```

6. >Im having trouble figuring out how to take an int input from a user and create the array with that amount..
You're having trouble because C++ doesn't support arrays with a non-const size. Read the other posts again for alternative solutions.

7. Originally Posted by Prelude
>Im having trouble figuring out how to take an int input from a user and create the array with that amount..
You're having trouble because C++ doesn't support arrays with a non-const size. Read the other posts again for alternative solutions.
I am new to C++ and I know nothing about vectors, etc.. Also, I haven't been taught anything about them in my class, so I'm guessing that they shouldn't be required to code the program I am trying to code..

I have to create a function named count, with the 3 inputs, number, array and length.. The program must count the number of times number appears in the array. The array has length elements, so I was thinking of some how defining the array from an integer user input..

I'm not asking for the exact program, I want to learn. But I've been staring at this exercise for 2 days and I have no idea where to begin..

Thanks for all who help push me in the right direction =)

8. There are three options. The best option is to use vector. IMO vector should be taught before any other option for user defined sized arrays. However, if you haven't learned it in your class, then you might not want to use it for an assignment.

The second option is to use a dynamic array with new [] and delete [] as in ZuK's example. I don't like this option, and you will rarely (if ever) find a situation where it is a better choice than vector. However, C++ classes still teach it, so if this is what your class is teaching this is what you should use.

If your class is not teaching dynamic arrays with new[]/delete[] and also not teaching vectors, then perhaps you aren't supposed to use either. A third option would be to have a maximum size. For example, you get the length from the user but they cannot input more than 1000 (or some other number). What you would do is create your array with the MAXIMUM number of values, but only use the number of values that the user tells you they want to use.

In this case, you aren't creating a dynamic array, but you can still accomplish your task of only using how many numbers the user specifies.

9. Originally Posted by otchster
I am new to C++ and I know nothing about vectors, etc.. Also, I haven't been taught anything about them in my class, so I'm guessing that they shouldn't be required to code the program I am trying to code..
It helps to know something despite it not being covered in class. In a good C++ class they are taught as part of the curriculum later. Arrays ususally are to get you started with the concept of sequential data. Arrays are just an improvement and encapsulation of arrays.

It's also part of the Standard Template Library, which is a large part of the C++ standard.

10. I have to create a function named count, with the 3 inputs, number, array and length..
So just forget about CREATING the array.
Just suppose it is ALREADY CREATED outside the function.
And concentrate on your task at hand: write the FUNCTION THAT ACCEPTS the array and legth

11. In a good C++ class they are taught as part of the curriculum later.
Like Daved, I would say that in a good C++ class vectors should be taught before dynamic arrays created with new[], and possibly even before statically sized arrays.

otchster, what were you taught concerning arrays? Based on that you can choose the best option as suggested by Daved.

12. I wish to go the route of a maxinum array..

Code:
```#include <iostream>
#include <string>
#include <algorithm>

int length;
int list[100];
int counter;

int main()
{

std::cout << "How many numbers do you wish to input?: ";
std::cin >> length;

for (counter = 0; counter < length; ++counter)
{
std::cout << "Number? ";
std::cin >> //***ARRAY ELEMENT X

}

//***   ONCE THE ARRAY IS FILLED WITH THE USER DEFINED AMOUNT, I NEED
TO SEARCH THE ARRAY FOR A USER INPUTTED NUMBER, AND OUTPUT HOW
MANY TIMES IT IS IN THE ARRAY
***//

return (0);
}```
Here, I'm hoping to have a user input length, and then use a FOR statement to prompt the user length times to input numbers into the array... How do I code a FOR statement to fill an array element with the next slot each time?

13. How do I code a FOR statement to fill an array element with the next slot each time?
You could replace your "//***ARRAY ELEMENT X" with list[counter], thus accessing the current element in the array.

I suggest that you move these lines into the main() function:
Code:
```int length;
int list[100];
int counter;```
There is no need to have them as global variables.

14. If you really want to go the dynamic memory allocation route then you've got some switching around of code to do to get it working properly:
Code:
```#include <iostream>
#include <string>
#include <algorithm>

int main()
{
int length;

std::cout << "How many numbers do you wish to input?: ";
std::cin >> length;    // Get length from user "before" you attempt to allocate space

// Now allocate space based on user input length...
int * list = new int[length];

// Do whatever you need to with the list array you've allocated.

// Clean up the memory you've allocated.
delete [] list;

return 0;
}```
Using a vector is far better like others have mentioned... you don't need to new/delete any memory, it's automatically done for you by the vector itself.

The algorithm header you are including contains a templated function that can easily do the counting for you (not gonna tell you what it's called) although I suspect you need to learn/experience loops more.

15. Originally Posted by laserlight
You could replace your "//***ARRAY ELEMENT X" with list[counter], thus accessing the current element in the array.

I suggest that you move these lines into the main() function:
Code:
```int length;
int list[100];
int counter;```
There is no need to have them as global variables.
Thanks everyone, my program now fills an array to the amount of the users preference, to a maximum.

The next step is to have the user input a number, search the array, and then respond with an output of how many times the number was inputted in the array...

Heres where I'm at, it compiles without error:

Code:
```#include <iostream>
#include <string>
#include <algorithm>

int main()
{

int length;             //The length of the array
int list[100];          //The array Max 100
int counterfill;        //Counter for filling array
int counterfind;        //Counter for finding array
int number;             //The number to search for
int amount;             //The amount the number was found

std::cout << "How many numbers do you wish to input?: ";
std::cin >> length;

for (counterfill  = 0; counterfill < length; ++counterfill)             //For statement to fill array
{
std::cout << "Number? ";
std::cin >> list[counterfill];
}

std::cout << "What number do you wish to search for?: ";
std::cin >> number;

for (counterfind = 0; counterfind < length; ++counterfind)
{
if(list[counterfind] == number)
{
++amount;
}
}

std::cout << "The number " << number << " appeared " << amount << " times. \n";

return (0);```
The program compiles and runs correctly, EXCEPT this is what happens when I run it...

An example:

Code:
```How many numbers do you wish to input?: 5
Number? 3
Number? 9
Number? 4
Number? 26
Number? 8
What number do you wish to search for?: 26
The number 26 appeared 671472553 times.```
Code:
`The number 26 appeared 671472553 times.`
^^ Should say The number 26 appeared 1 times, obviously