This C++ program obtains the inner product of two valarrays. The inner product of two valarrays is defined as the scalar multiplication of each corresponding element of the two valarrays. The result for two valarrays with different sizes is unpredictable and varies from implementation to implementation.
Here is the source code of the C++ program obtains the inner product of two valarrays. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to Obtain Inner Product of Two Valarrays
*/
#include <iostream>
#include <valarray>
void print(std::valarray <double> v)
{
for (int i = 0; i < v.size(); i++)
std::cout << v[i] << " ";
std::cout << std::endl;
}
int main()
{
std::valarray <double> v(2, 5);
std::valarray <double> vd(1.5, 5);
std::valarray <double> vd2(10.0, 10);
std::cout << "Valarray 1 => ";
print(v);
std::cout << "Valarray 2 => ";
print(vd);
std::cout << "Inner Product of valarray 1 and 2 => ";
vd = v * vd;
print(vd);
// Behaviour of multiplying different sized valarrays is undefined
std::cout << "Inner Product of valarrays of different sizes => ";
vd = vd * vd2;
print(vd);
}
$ a.out Valarray 1 => 2 2 2 2 2 Valarray 2 => 1.5 1.5 1.5 1.5 1.5 Inner Product of valarray 1 and 2 => 3 3 3 3 3 Inner Product of valarrays of different sizes => 30 30 30 30 30
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Apply for C++ Internship
- Check Programming Books
- Check Computer Science Books
- Apply for Computer Science Internship
- Practice Computer Science MCQs