Print three-dimensional valarray line-by-line : valarray « Valarray « C++

C++
1. Bitset
2. Class
3. Console
4. Data Structure
5. Data Type
6. Deque
7. Development
8. File
9. Function
10. Generic
11. Language
12. List
13. Map Multimap
14. Overload
15. Pointer
16. Queue Stack
17. Set Multiset
18. STL Algorithms Binary search
19. STL Algorithms Heap
20. STL Algorithms Helper
21. STL Algorithms Iterator
22. STL Algorithms Merge
23. STL Algorithms Min Max
24. STL Algorithms Modifying sequence operations
25. STL Algorithms Non modifying sequence operations
26. STL Algorithms Sorting
27. STL Basics
28. String
29. Valarray
30. Vector
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++ Tutorial
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C++ » Valarray » valarrayScreenshots 
Print three-dimensional valarray line-by-line
 
 

/* The following code example is taken from the book
 * "The C++ Standard Library - A Tutorial and Reference"
 * by Nicolai M. Josuttis, Addison-Wesley, 1999
 *
 * (C) Copyright Nicolai M. Josuttis 1999.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
#include <iostream>
#include <valarray>
using namespace std;

// print three-dimensional valarray line-by-line
template<class T>
void printValarray3D (const valarray<T>& va, int dim1, int dim2)
{
    for (int i=0; i<va.size()/(dim1*dim2); ++i) {
        for (int j=0; j<dim2; ++j) {
            for (int k=0; k<dim1; ++k) {
                cout << va[i*dim1*dim2+j*dim1+k<< ' ';
            }
            cout << '\n';
        }
        cout << '\n';
    }
    cout << endl;
}

int main()
{
    /* valarray with 24 elements
     * - two groups
     * - four rows
     * - three columns
     */
    valarray<double> va(24);

    // fill valarray with values
    for (int i=0; i<24; i++) {
        va[i= i;
    }

    // print valarray
    printValarray3D (va, 34);

    // we need two two-dimensional subsets of three times 3 values
    // in two 12-element arrays
    size_t lengthvalues[] {  2};
    size_t stridevalues[] 12};
    valarray<size_t> length(lengthvalues,2);
    valarray<size_t> stride(stridevalues,2);

    // assign the second column of the first three rows
    // to the first column of the first three rows
    va[gslice(0,length,stride)]
        = valarray<double>(va[gslice(1,length,stride)]);

    // add and assign the third of the first three rows
    // to the first of the first three rows
    va[gslice(0,length,stride)]
        += valarray<double>(va[gslice(2,length,stride)]);
    
    // print valarray
    printValarray3D (va, 34);
}

/* 
0 1 2
3 4 5
6 7 8
9 10 11

12 13 14
15 16 17
18 19 20
21 22 23


3 1 2
9 4 5
15 7 8
9 10 11

27 13 14
33 16 17
39 19 20
21 22 23



 */        
  
Related examples in the same category
1. Create valarray of bool value by using comparison operator on another valarray
2. Minus 10 from each element in a valarray
3. Use valarray + valarray to double the value for each element in valarray
4. Do sqrt for all elements in a valarray
5. Print valarray as two-dimensional array
6. valarray with double value inside
w__ww._j__a_v___a2s_.co_m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.