Create a 2D array with random numbers of rows. C++ [duplicate] - c++

This question already has answers here:
How do I declare a 2d array in C++ using new?
(29 answers)
Closed 1 year ago.
I am trying to create an 2D array. The number of columns is already defined, it will be a constant equal to 20. On the other side, I want the program will generate a number between 30 and 40, which will be the number of rows. I have already created the function to generate numbers between two values. The problem is that it won't allow me declare the array since one of the number of rows is not a constant value and the program can't figure out the space needed to reserve. How can I do it without using vectors or pointers?
const int COLUMNS = 20; /
int numOfRows = genNumInRange(30, 40), //Gets the random number from the function.
array[numOfRows][COLUMNS]; //
ERROR : Expression must have a constant value.
Any possible way I can do that?

You can malloc as many array[COLUMNS] as you need.

Related

How to find the exact size of an array in C++? [duplicate]

This question already has answers here:
How can I find the number of elements in an array?
(16 answers)
Closed 1 year ago.
I declare an array that can hold 10 parameters:
int a[10] = {2,3};
But I actually have 2 parameters, when I use sizeof() :
int n = sizeof(a) / sizeof(int);
It showed the length of the array is 10, but I want the result is: 2 as I only got 2 parameters.
How can I do that ? Thx.
The actual length of the array is 10, not 2. When you provide fewer elements than the array holds, the rest will be filled with zeros.
If you’d like to size the array based on how many values it’s initialized with, just leave the size out of the declaration: int a[] = {2,3};.

How to create a 2D array in C++? [duplicate]

This question already has answers here:
how to create a contiguous 2d array in c++?
(7 answers)
Closed 3 years ago.
OK, this question seems to be silly but bear with me. When I trying to create a 2D array in C++, it gave me some warnings (len is an integer):
double a[len][len];
// warning: variable length arrays are a C99 feature
// warning: variable length array used
So I tried another:
double **a = new double[len][len];
// error: only the first dimension of an allocated array may have dynamic size
// read of non-const variable 'len' is not allowed in a constant expression
How can I do it correctly in C++11?
double** a=new double*[len];
for(int i=0;i<len;++i)
{
a[i]=new double[len];
}
Are there any restrictions on what you can use? If you're planning to do array manipulations I'd say just use [Eigen] (http://eigen.tuxfamily.org/index.php?title=Main_Page)

Determine the size of a array of structs that it used as a parameter [duplicate]

This question already has answers here:
when do we need to pass the size of array as a parameter
(8 answers)
Closed 6 years ago.
I'm currently working on a project where I want to convert the entries of a CSV file into a vector of Objects. Therefore I have written a function, which converts an array of structs in a vector. The problem is that right now my function only works if the user enters the right size of the array as an additional parameter but if he enters a higher number an exception is thrown because the function is trying to read from an array entry that doesn't exist. Now I want to know if there is anyway that I can determine the size of the array of structs in my function. I have already tried sizeof(array)/sizeof(array[0]) but that doesn't work.
Here is the function I'm talking about:
BANKMANAGEMENT_API int initAccounts(ACCOUNT accArray_[], const int numOfAcc_)
{
BankmanagementClass *myBankmanagement = BankmanagementClass::createBankmanagementClass();
for (int i = 0; i < numOfAcc_; i++)
{
ACCOUNT acc = accArray_[i];
Account* newaccount = Account::accountStruct2Obj(&acc);
myBankmanagement->setNextAccountId(myBankmanagement->determineNextId(newaccount->getAccountId(), myBankmanagement->getNextAccountId()));
myBankmanagement->addAccount(newaccount);
}
LogInfo("Account Vector was initialized with data from Csv File.");
return 0;
}
I want to get rid of the numOfAcc_ parameter so that the user can't enter the wrong size.
It's for a dll with C interface.
There is no way to determinate the size of the array. If you have under control how accArray is filled you could use an end marker and check this condition in the for loop.

C++ - Arrays of the integer type [duplicate]

This question already has answers here:
How do I use arrays in C++?
(5 answers)
Closed 8 years ago.
I was just curious to know how to find the number of elements of an array of integers.
For character arrays we can loop thorough the array till we reach the null character,but how can I do it for integer array?
#include <iostream.h>
void main()
{
int a[] = {1,2,3,4};
for ( k = 0 ; a[k] ; k++)
cout<<k<<endl;
}
The above code counts from 0 to 8.
-A C++ noob with an open mind
A char array is terminated by 0 by convention. Such an array is called a C-style string, because it's used as a string of characters.
For integers, there is no termination value by convention, and you need to know the length by some other means. If it's your own array, store the length in a variable. If you receive the array from an API, there will be typically be a parameter receiving the length of the array, that you can use.
If the array is a global, static, or automatic variable (int array[10];), then sizeof(array)/sizeof(array[0]) works.
If it is a dynamically allocated array (int* array = malloc(sizeof(int)*10);) or passed as a function argument (void f(int array[])), then you cannot find its size at run-time. You will have to store the size somewhere.
Note that sizeof(array)/sizeof(array[0]) compiles just fine even for the second case, but it will silently produce the wrong result.

How to create an array with number of elements from a variable? [duplicate]

This question already has answers here:
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Closed 10 years ago.
I have an integer num that was read from a file. I want to create an array with the number of elements being num.
A sample code of what I want to do but doesn't work:
int num;
cin >> num;
int iarray[num];
Arrays in C++ have compile-time bounds.
Use dynamic allocation instead, or a healthy std::vector wrapper around the same process.
dynamic allocation being int * iarray = new int[num];
Just make sure to call delete[] iarray; at some point to free the memory.