Logical Size (number of elements) in array - c++

I have an array (defined below) and I want to find the number of elements in it and send in mehtod call.
So, I have this:
const int MAX_SIZE = 20; // Maximum size of data array
double givenDataPoints[MAX_SIZE] = {0, 2, 3.8, 5, 9, 16, 16.2, 17, 18, 19, 19.5};
And i want to get
int logicalSize = //this should be 10 because I only put in 10 numbers, not 20
How do I do this?

I highly recommend using the standard library for that:
http://www.cplusplus.com/reference/array/array/size/
But you can also do:
sizeof( givenDataPoints ) / sizeof( givenDataPoints[ 0 ] );
But you are going to get 20 because you told the compiler to allocate space for 20 elements.
You need to keep track of what is considered to be an empty element.
Here is a similar question: Find the count of elements in array in C++
I hope it helps.

Related

Calculating an array's number of elements... Able to refer to its index values by just stating the return type? What? How? Why? (C++)

I am learning how to calculate the size and number of elements in an array and came across a question. In a tutorial I see
we can calculate the number of elements in an array as follows ...
int array_variable [] = {1, 5, 8, 10};
int array_variable_number_of_elements = sizeof(array_variable) / sizeof (int);
I am aware and fully understand why you could replace
sizeof (int);
with
sizeof (array_variable[0]); // or use any index value from the array_variable
The video appears to suggest using just int is good practice which I don't understand. The computer obviously isn't psychic and int is simply a data type. What happens when there are two int type arrays in the same function? Why does this work? Does this work because it is in the same line as the following?
int array_variable_number_of_elements = sizeof(array_variable)
Thanks :)
Second approach is better and more generic, when you need to change the type of array int to char, you don't have to edit second line at all.
char array_variable [] = {'a', 'b', 'c' }; //changed this line only
int array_variable_number_of_elements = sizeof(array_variable) / sizeof
(array_variable[0]);
To answer your below comment;
above lines you can interpret like below;
int array_variable = {1, 2, 3, 4, 5 };
int array_variable_number_of_elements = sizeof(array_variable) / sizeof ( int);
// ^^^^^^^^^ ^^^^ ^^^
// array_variable_number_of_elements = 20 / 4 ; // 20 / 4 = 5
total array size in bytes 20 and size of int is 4 bytes in my system;
total number of elements in array is 5;

confusion determining element number of an array

I've heard from my lecturer, sizeof(some pointer) always equal in a machine. For example, if it is 64 bit all pointers are 8 byte, in 32 bit 4 byte. So, I like to use sizeof when I want to specify size of an array like
int arr[] = {2, 5, -1, 10, 9};
int size = sizeof(arr) / sizeof(int)
I assume that above code an int takes 4, so 20/4 gives number of elements for arr.
int arr[] = {2, 5, -1, 10, 9};
int size = sizeof(arr) / sizeof(int*)
I also assume that sizeof(some pointer) takes 4, so 20/4 again gives number of elements for arr.
I'm confused that when I apply it for a double array. I think it shouldn't work. But, it works.
double arr[] = {2, 5, -1, 10, 9};
int size = sizeof(arr) / sizeof(double*);
If every pointers take same size in a machine, how can be the result right? Can be the question explained?
Edit
I mixed up division by *arr and double pointer
Size of a pointer is not the size of the type of the elements in the double array, because the elements are doubles not pointers.
When you apply sizeof to an array it gives the size of the array in bytes, in case of
double arr[] = {2, 5, -1, 10, 9};
it's
5 * sizeof(double)
and sizeof(double) != sizeof(double *). So to actually get the number of elements in the array you need
sizeof(arr) / sizeof(double);
but to make it more maintainable, you can just ask for the size of the type of the elements like this
sizeof(arr) / sizeof(*arr);
or
sizeof(arr) / sizeof(arr[0]);
Also, do not rely on this method to determine the number of elements in an array. Note that at runtime there is no way to know that from the array itself in c, since it's not stored with the array. You need to store the value if you want to keep track of the number of elements.
While this might seem annoying when you first think of it, with the experience you should learn that, it's better this way. Because it gives you complete control over what data your program has to handle, and once you get used to it, it will become very hard to trust languages that just give you the length of the array like python's len operator.

c++ arrays, how to add a new row of values in the same array?

how do you create a new row of values in an array from user input or cin?
say theres a row of values already in the array and you need to add a second row of values
but not added to the first row, and how would you put the braces and the comma in, does the user put it in or is there something that will automatically put the bracers and comma in
int test [] = { 1, 21, 771, 410, 120711 },
{ 1, 2, 3, 4, 5 };
Without very bad and dirty tricks this is not possible. Better use list or vector (which is the nearest to an array). The other possibility is to use pointers and to extend it create a temporary memory, copy the old data and then add the new.
There is no way to change the size of array while still preserving its contents. The only way to change the size of an array at all is to use the new operator to allocate dynamic memory to a pointer, but this will destroy any data the array previously held. If you want to have a re-sizable array, you should probably use std::vector.
If you're keen on using c++11 you can keep your initialiser lists with std::vector like so:
#include <vector>
int main()
{
// initialise
std::vector<std::vector<int>> test = { { 1, 21, 771, 410, 120711 },
{ 1, 2, 3, 4, 5 } };
// add new data from user
test.push_back({9, 8, 7, 6, 5, 4, 3, 2, 1});
}
You're asking for a two-dimensional array. This is declared like this:
int test[][5] = {
{1, 21, 771, 410, 120711},
{1, 2, 3, 4, 5 },
// Add more if you want.
};
The first array is accessed through test[0], the second through test[1], etc. The first element of the first array is test[0][0], the second test[0][1] and so forth.
Note that this is an array with a static size. You can't change it at runtime. If you know in advance how many rows you need, you just declare it as:
int test[NUMBER OF ROWS][NUMBER OF COLUMNS];
and then fill it with values later. But you cannot change the size. If you want a fully dynamic array, then you should use a vector of vectors:
std::vector< std::vector<int> > test;
You then add rows with:
test.push_back(std::vector<int>());
and add elements to each row with:
// Adds a number to the first row.
test[0].push_back(some_int);
Access happens the same way as with the static array (test[0], test[0][0], etc.)

How do i select the other column in a 2-dimensional array?

How do i select the second column of a 2-dimensional array.
I have this array with about 30 values (LKT) and from there I have a 2-dimensional array (ScaledValues). The 2nd column of this 2-dimensional array will be filled with a scaled version of the original LKT array.
Initially, ActiveArray variable points to the LKT array. However, when I populate the 2nd column of the array in ScaledValues with scaled values of the first LKT array, how do I move ActiveArray to now point to the second column as the active array that i'll be using? i.e. After i fill the second column with the desired scale values, I'd like to work with those values and I want to use ActiveArray variable to denote that this new column is the active array.
I know there are other ways to do this i.e. I could create 2 separate individual arrays but I have to use the format that you see below. please assist. thanks.
Please let me know if i need to make my question clearer.
Thank you very much.
static const unsigned int LKT[30] = {
30, 29, 28, 27, 26, 25, 24, 23, 22,
21, 20, 19, 18, 17, 16, 15, 14, 13, 12,
11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
static unsigned int ScaledValues[30][2];
static volatile unsigned char ActiveArray = 0;
Reverse the ScaledValues array declaration:
unsigned int ScaledValues[2][30];
for (int i = 0; i < 30; i++) {
ScaledValues[0][i] = LKT[i];
ScaledValues[1][i] = scale(LKT[i]);
}
// Also need to make this a pointer
unsigned int * ActiveArray = ScaledValues[0]; // Original values
ActiveArray = ScaledValues[1]; // Scaled values
the easier way to manipulate this array could be done as following
first the definition should be in this way
static unsigned int ScaledValues[2][30];
then to copy LKT to the first line (and not column)
memcpy(ScaledValues[0], LKT, 30*sizeof(unsigned int));
to access to the second line of the ScaledValues array is
ScaledValues[1]

Accessing values from a two dimensional initialized array

int a[][30]={{2, 16},{4, 8},{5, 16, 21},{2,6,3,5,6}};
Since the size of the second dimension is varying. If I want to access something like for a particular value of i(first dimension), access all j values(2nd dimension), how do I write that statement?
What I thought was:
for(int j=0;j<30;j++)
a[i][j]=some operation;
but its unnecessarily looping till 30 which is the max value. What's the efficient way to do it?
Thanks.
The compiler does not keep any information about how many values were in the braced initializer. Instead, it fills the "missing" values with zeros.
So these two are equivalent:
int a[][4] = {{2, 16},{4, 8, 5}};
int a[][4] = {{2, 16, 0, 0}, {4, 8, 5, 0}};
If you know that none of the "actual" data elements are zero, you can stop your loop when you find a zero element. Otherwise, you'll need to set up the data differently.
The size of both dimensions is fixed. The outer dimension has size 4 and the inner has size 30.
If you iterate over the inner dimension, then you will print lots of zeros, as that is what you initialize the remaining integers that aren't explicitly initialized to.
You seem to want this:
std::vector<int> a[] = {
boost::list_of(2)(16),
boost::list_of(4)(8),
boost::list_of(5)(16)(21),
boost::list_of(2)(6)(3)(5)(6)
};