Listado de la etiqueta: Length


“The length of an array indicates the number of elements of the particular array. Many ways exist in PERL to calculate the array length. When the length of the array is calculated by using the “@” symbol, then it is called an implicit scalar conversion. When the length of the array is calculated by using the scalar keyword, then it is called an explicit scalar conversion. The “#” symbol can also be used to count the length of the array. Different ways of counting the array length and using it in the PERL scripts have been shown in this tutorial.

Different ways of counting the array length have been described in the next part of this tutorial by using examples.”

Example-1: Count the Array Length Using the “@” Symbol

Create a PERL file with the following code that will count the length of an array by using the “@” symbol. An array of 5 string values has been defined in the code. The total number of array values has been stored into a variable named $length that has been printed later.

# Declare an array of strings
@myArr = (‘php’, ‘java’, ‘perl’, ‘bash’, ‘python’);
# Count the array length
$length = @myArr;
# Display the array length
print «The array contains $length elements.n«;

Output

The following output will appear after executing the above code.

Example-2: Count the Array Length Using the Scalar Keyword

Create a PERL file with the following code that will count the length of an array by using the scalar keyword. An array of 6 numbers have been defined in the code. The scalar keyword has been used to count the total number of elements of the array. Next, two values were added to the array and again counted, the total number of elements of the array. The join() function has been used to print the array values with the space.

# Define an array of numbers
@numbers = (6, 5, 3, 1, 2, 8);
# Print the array values
print «Array values are:n«, join(‘ ‘,@numbers), «n«;
# Print the total number of elements of the array
print «Total elements of the array is «, scalar @numbers, «n«;
#Add two new elements
$numbers[@numbers] = 9;
$numbers[@numbers] = 4;
# Print the array values after adding two values into the array
print «Array values after adding two elements are:n«,join(‘ ‘,@numbers), «n«;
# Print the length of the array after insertion
print «Total elements of the array is «, scalar @numbers, «n«;

Output

The following output will appear after executing the above code. The total number of elements of the array after adding two elements is 6+2 = 8.

Example-3: Count the Array Length Using the “#” Symbol

Another way of counting the total number of elements of the array is using the “#” symbol. The last index of the array is counted by using the “#” symbol. So the total array elements can be counted by adding 1 with the last index value. Create a PERL file with the following code that will count the array length by using the “#” symbol. An array of 5 float numbers has been defined in the code. The pop() function has been used to remove 3 elements from the array. The join() function has been used to print the array values with the space.

# Declare an array of float numbers
@float_num = (45.89, 34.12, 56.34, 90.34, 45.23);
# Print the array values
print «Array values:n«, join(‘ ‘,@float_num), «n«;
# Print the length of the array
print «Total number of array elements : «, $#float_num+1, «n«;
# Remove three elements from the array
pop @float_num;
pop @float_num;
pop @float_num;
# Print the array values
print «Array values after removing three elements:n«, join(‘ ‘,@float_num), «n«;
# Print the length of the array after removing three values
print «Total number of array elements after removing the elements: «, $#float_num+1, «n«;

Output

The following output will appear after executing the above code. The total number of elements of the array after removing 3 elements is 5-3 = 2.

Example-4: Print the Array Values Using Array Length

Create a PERL file with the following code that will use the length of the array to iterate the values of the array using for loop and print the array values in each line. The last index value of the array has been used here to count the length of the array.

# Declare an array of strings
@myArr = (‘php’, ‘java’, ‘perl’, ‘bash’, ‘python’);
# Iterate the array values based on the array length
for($i = 0; $i < $#myArr+1; $i++)
{
# Print the array values
print $myArr[$i], «n«;
}

Output

The following output will appear after executing the above code.

Example-5: Count the Array Length Using Loop

Another simple way to count the array length is by using any loop. Create a PERL file with the following code that will count the array length by using the foreach loop. An array of 5 string values has been defined here. A counter variable has been used in the code to count the length of the array by incrementing the counter value by 1 in each iteration of the loop.

# Declare an array of strings
@myArr = (‘php’, ‘java’, ‘perl’, ‘bash’, ‘python’);
# Initialize the counter
$counter = 0;
print «Array values:n«;
# Iterate the array values based on the array length
foreach $val (@myArr)
{
# Print the array values
print $val, «n«;
$counter++;
}

# Print the length of the array
print «nTotal number of array elements : «, $counter, «n«;

Output

The following output will appear after executing the above code.

Conclusion

The way of counting the array values in PERL has been shown in this tutorial by using multiple examples. The PERL user can use any of the ways to find out the array length and use it for solving PERL programming problems.



Source link


Variable size arrays (VLAs) are not forbidden in C++; the iso error is correct. Runtime-sized or variable-sized arrays are the other names for variable-length arrays. The size of these arrays is set at runtime. Among the kinds that can be updated are variable-length arrays and pointers to variable-length arrays. Variably modified types should be defined at either the block or function prototype level. Variable-length arrays are a feature that allows us to allocate a variable-size automóvil array on the stack. In a typedef statement, it can be utilized. From the C++ standard onwards, C enables variable-size arrays. The program below, for example, compiles and runs perfectly in C.

void PrintArray(int n)
{
 int Array[n];
 // ……
}  
int main()
{
PrintArray(8);
}

However, variable-sized arrays are not supported by the C++ standard (until C++11). Array size is a constant expression in the C++11 standard. As a result, the above program may not be an acceptable C++ program. As the GCC compiler has an extension to accommodate them, the program may function with the GCC compiler. As little more, the array size is now a simple expression in C++14 (not constant-expression).

It’s not desirable to have to generate a potentially big array on a stack with limited space. If you are not aware ahead of time, we will write damaging code. Variable-length arrays are not natively supported in C++ because they would necessitate significant type system changes.

Here, in the C++ article, we will show how to overcome the C++ iso forbidden variable-length array error at the compilation time.

Example 1: Program to Implement a variable-length array in C++ With GCC Compiler

Variable-length arrays can choose any size that the user desires, i.e., they can be variable in size. The following is a C++ program for creating variable-length arrays:

We have C++ header files in the first step and the namespace file. After that, we have the program main method, and the main body has the pointer variable declaration as “Array” and the other variable “Arr_Size” declaration. The cout prints the statement that asks the user a number for array size. Then, cin will take the number value from the user. The variable “Array” is called where we have set the size of the array.

Now, we have also requested the array values from the user. The array value will iterate by the for loop till it reaches the array size. Then, the second for loop is used to print the values inside the array, and at the same time, we have deleted the array by using the delete[] expression in the program.

#include <iostream>

#include <string>

 

using namespace std;

int main() {

   int *Array, Arr_size;

   cout<<«Enter array size: «<>Arr_size;

   Array = new int [Arr_size];

   cout<<«Enter array values: «<<endl;

   for (int i = 0; i>Array[i];

   cout<<«Array: «;

   for(int i = 0; i<Arr_size; i++)

   cout<<Array[i]<<» «;

   cout<<endl;

   
   return 0;

}

The shell displays a message for inputting the array size after compilation. When the user input the size of the array then, the shell asks the user to set the values for the array. The size of the array and its elements are accessible as follows. Hence, we can implement a variable-length array in C++ without a forbidden exception.

Example 2: Program to Implement a variable-length array in C++by Using Vectors

The C++ STL provides a vector as an alternative to variable-length arrays. It’s suitable for a variety of applications. It will be more clear with the C++ program, which we have implemented below.

As we have to use vectors in our program. So the most important part is to define the vector as a header file at the top of the code implementation. We have added the vector file in the section of the header.

Within the program main, we have a vector class with type int, and the vectors class has a variable “v.” We have added five elements of type integer in the vector. After that, we have a for loop iteration cycle. Inside the for loop, we have declared an iterator to a vector with a new variable “it.” Then, the “it” variable has a begin and end function for displaying the elements of the vectors.

#include<iostream>
#include<vector>
using namespace std;

int main() {

   vector v;

   v.push_back(10);

   v.push_back(20);

   v.push_back(30);

   v.push_back(40);

   v.push_back(50);

   
   for(vector::iterator it = v.begin(); it != v.end(); it++) {

      cout<< *it <<endl;

   }

   return 0;

}

The above program gives the output like this.

Example 3: Program to Implement a variable-length array in C++ by Using std:: vector

Vectors are used to carry comparable data types in C++. The size of a vector, unlike arrays, can grow dynamically. We can adjust the vector size as needed throughout the program execution. The vector header file must be included in our program to utilize vectors. Merienda the vector library is included in the header file, we can utilize vector as std::vector in the program.

After including the vector library at the top, we have called the std::vector inside the program’s main function. The vector is declared as “numbers” and initialized with the five random numeric values. The variable “number” is again defined, which has three vector elements from the above-given vector container. The std::cout is used to display the length of the vector inside the variable “number” by using the size function.

#include <iostream>

#include <vector>

 

int main()

{
    std::vector numbers = {10, 20, 30, 40, 50};

    numbers = {30, 40, 50};

    std::cout<< «Array Length : « <<numbers.size() << std::endl;

    return 0;

}

The output shows the length of the specified vector array as follows.

Conclusion

Summing up! Here, we have a detailed discussion about variable-length arrays in the introduction. Thus, we learned that C++ forbids variable-length arrays(VLA). We have specified some ways above to implement the variable-length array in C++ and alternative ways of the variable-length array. These might be handy when interacting with the variable-length arrays in C++.



Source link