C++ Program To Perform Mathematical Operation On Valarray Elements - C++ Programming Tutorial
C++ Course / C++ Programs / C++ Program To Perform Mathematical Operation On Valarray Elements

C++ Program To Perform Mathematical Operation On Valarray Elements

BLUF: Mastering C++ Program To Perform Mathematical Operation On Valarray Elements is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: C++ Program To Perform Mathematical Operation On Valarray Elements

C++ is renowned for its efficiency. Learn how C++ Program To Perform Mathematical Operation On Valarray Elements enables low-level control and high-performance computing in the tutorial below.

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:

  • 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.
  • 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>.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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:

Example

#include <iostream>
#include <valarray>
int main() 
{
 // Creating a valarray
 std::valarray<int> myValArray = {1, 2, 3, 4, 5};
 // Displaying the original valarray
 std::cout << "Original valarray: ";
 for (auto const &element : myValArray) 
 {
 std::cout << element << " ";
 }
 std::cout << std::endl;
 // Performing mathematical operations on valarray elements
 myValArray += 5; // Add 5 to each element
 myValArray *= 2; // Multiply each element by 2
 // Displaying the modified valarray
 std::cout << "Modified valarray: ";
 for (auto const &element: myValArray) 
 {
 std::cout << element << " ";
 }
 std::cout << std::endl;
 return 0;
}

Output:

Output

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

Explanation:

  • 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.
  • 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.
  • 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 .
  • 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.
  • std::cout element " "; This line outputs each value in the array to the console inside the loop, separated by a space.
  • 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.
  • 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.
  • 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.
  • std::cout \\ "Modified valarray: "; The following numbers come from the changed valarray, as this line informs the console.
  • for (auto const &element: myValArray): The changed valarray elements are iterated over using a different loop.
  • std::cout << element << " "; Inside the loop, this line prints each modified element of the valarray followed by a space to the console.
  • std::cout << std::endl; After the loop, this line prints an end-of-line character to the console, moving to the next line.
  • return 0; The operating system receives 0 in response to this line, which shows that the program has been executed successfully.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience