C/C++ Need help in putting data into an array in c++

  • Thread starter Thread starter googled123
  • Start date Start date
  • Tags Tags
    Array C++ Data
AI Thread Summary
To create an array in C++ based on user input for the number of elements, the discussion highlights the use of the `new` operator for dynamic memory allocation. However, it suggests considering the C++ `vector` structure as a more flexible alternative. A vector can be initialized with zero size and allows for dynamic expansion as values are added, eliminating the need to predefine the number of elements. The provided code example demonstrates how to declare a vector, prompt the user for input, and store the entered values. The `push_back` method is used to append values to the vector, and the `size()` function retrieves the current number of elements. This approach simplifies handling user input and managing memory compared to traditional arrays.
googled123
Messages
1
Reaction score
0
Hi guys,
I need to create an array in c++. But before I declare how many elements are in the array I want to prompt the user for input. I want to ask the user how many elements do they want in the array.
What ever the user said, 5 or 20. I then want to enter these 5 values or 20 values myself.
hope you can help.
 
Technology news on Phys.org
Do you know what the new operator is, and how to use it with an array?
 
You may want to consider using the c++ structure "vector" rather than an array. A vector v (of double values in this example) of size N is created by
Code:
  // ... determine the value of N here ...

  // create a vector of N double values.
  std::vector<double> v(N);
The usage is the same as for an array (except for extra options that you may find out some day).
 
I'm guessing this is a class assignment, so you need to use an array instead of a vector. But for future reference...

Going further from Timo's example, you can declare the vector to have zero size initially, and then let it automatically expand as you add data to it. That way, you don't have to ask the user how many numbers he's going to enter.

Code:
#include <iostream>
#include <vector>

using namespace std;

int main ()
{
    vector<int> numbers;  // has length zero, initially

    cout << "Enter some numbers, terminating with control-D:" << endl;

    int n;
    while (cin >> n)
    {
        numbers.push_back(n);  // append n to the vector and expand
                               // the vector as necessary
    }

    // the size() member function tells you how big the vector is

    cout << "You entered:" << endl;
    for (int k = 0; k < numbers.size(); ++k)
    {
        cout << numbers[k] << endl;
    }

    return 0;
}

Input/output:

Code:
jtbell$ g++ vector_demo.cpp
jtbell$ ./a.out
Enter some numbers, terminating with control-D:
12
23
34
45
[I](control-D entered here)[/I]
You entered:
12
23
34
45
jtbell$
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top