Javatpoint Logo
Javatpoint Logo

C++ Program to Perform Mathematical Operation on Valarray Elements

The use of the C++ Standard Library's valarray container and the several arithmetic operations that may be performed on its elements are demonstrated by a C++ program that manipulates valarray elements mathematically. It is the underlying theory of the program:

  1. Std::valarray: Std::valarray is a container class from the C++ Standard Library that functions similarly to a dynamic array by representing an array of values. It is made to perform numerical and mathematical tasks effectively.
  2. Include Headers: If you want to operate with std::valarray, the program usually starts by including the headers that are required, such as <iostream> for input/output operations and <valarray>.
  3. Creation of valarray: The formation of valarray after defining the data type and initializing it with elements, you can create a std::valarray. You can initialized myValArray with a series of integers in your program.
  4. Mathematical Operations: Standard C++ arithmetic operators can be used to perform mathematical operations directly on the std::valarray You can use the += operator to add 5 to each element in your program, and *= to multiply each element by 2.
  5. Iterating Through Elements: Usually, you can use a loop, such as a range-based for loop, to iterate through the elements and print them to display the changed valarray.
  6. Use Cases: When you need to quickly and efficiently conduct element-wise mathematical operations on a sequence of values, std::valarray comes in handy. It is frequently utilized in numerical and scientific computing, where vectorized operations can greatly increase performance.
  7. Efficiency: Single instruction, multiple data, or SIMD, is a hardware capability that std::valarray may utilize to execute operations in parallel. It is designed for efficient numerical computations.

Example:

Output:

Original valarray: 1 2 3 4 5 
Modified valarray: 12 14 16 18 20

Explanation:

  1. This program contains the two #include <valarray> and #include <iostream>. In addition to the valarray class from the C++ Standard Library, these lines provide the headers required for input/output functions.
  2. std::valarray<int> myValArray = 1, 2, 3, 4, 5; This line inserts an integer sequence into a valarray called myValArray. First, the values 1, 2, 3, 4, and 5 are initialized in myValArray.
  3. std::cout << "Original valarray: "; "Original valarray:" This line writes a message to the console to show that the next integers are from the original valarray.
  4. regarding (auto const &element: myValArray): The elements of myValArray are iterated over by a loop that is started with this line. Throughout the loop, the elements are not changed because the element is a constant reference to every element in the valarray.
  5. std::cout element " "; This line outputs each value in the array to the console inside the loop, separated by a space.
  6. std::cout \\ std::endl; This line sends an end-of-line character to the console to signal the end of the loop and go on to the following line.
  7. These lines apply a mathematical operation to the valarray (myValArray += 5;). Every element in the valarray is increased by 5. As a result, the elements in myValArray are adjusted of this in-place action.
  8. myValArray *= 2; There's another mathematical operation going on here. Every element in the array is multiplied by two. Once more, this procedure is carried out on-site.
  9. std::cout \\ "Modified valarray: "; The following numbers come from the changed valarray, as this line informs the console.
  10. for (auto const &element: myValArray): The changed valarray elements are iterated over using a different loop.
  11. std::cout << element << " "; Inside the loop, this line prints each modified element of the valarray followed by a space to the console.
  12. std::cout << std::endl; After the loop, this line prints an end-of-line character to the console, moving to the next line.
  13. return 0; The operating system receives 0 in response to this line, which shows that the program has been executed successfully.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA