Need help for two dimensional array with vector

#include <iostream>
#include <cassert>
#include <vector>

using namespace std;

vector<int> flatten(int a[100][200]) {
vector<int> v;
for (int i = 0; i < 100; ++i) {
for (int j = 0; j < 200; ++j) {
v.push_back(a[i][j]);
}
}
return v;
}

int main() { //test code

}

I made a function for two dimensional arrays with vector, but I don't know how to make the test code for it.
Well, you could make an array of ints that is 100 by 200 with some known values (say 0 and incrementing upwards), call the flatten function on it, then see if the resulting vector has 0 and up in it.
Exercise 10: Flatten (5 points)

vector flatten(int a[100][200]);
Write a function named flatten that takes a 2­dimensional array of integers with 100 rows and 200 columns and returns a vector that contains all of the array’s elements. Copy the values a row at a time. In other words, first copy row 0 into the vector, then row 1, then row 2, and so on. The declaration of flatten is given above.

This is whole question for that
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>

constexpr std::size_t ROWS = 100 ;
constexpr std::size_t COLS = 200 ;

std::vector<int> flatten( const int array[ROWS][COLS] ) { // const added

    // copy all the values at one go
    // return { array[0], array[ROWS-1] + COLS };

    std::vector<int> result ;

    // Copy the values a row at a time.
    for( std::size_t i = 0 ; i < ROWS ; ++i )
           result.insert( result.end(), array[i], array[i] + COLS ) ;

    return result ;
}

bool equal( const std::vector<int>& vec, const int array[ROWS][COLS] ) {

    if( vec.size() != ROWS*COLS ) return false ;

    // test equality of all the values at one go
    // return std::equal( vec.begin(), vec.end(), array[0] ) ;

    // test equality one value at a time
    for( std::size_t i = 0 ; i < ROWS ; ++i )
           for( std::size_t j = 0 ; j < COLS ; ++j )
               if( array[i][j] != vec[ i*COLS + j ] ) return false ;

    return true ;
}

int main() {

    const int NTESTS = 5 ;
    int a[ROWS][COLS] ;

    for( int n = 0 ; n < NTESTS ; ++n ) {

        for( std::size_t i = 0 ; i < ROWS ; ++i )
               for( std::size_t j = 0 ; j < COLS ; ++j ) a[i][j] = std::rand() ;

        std::cout << ( equal( flatten(a), a ) ? "ok" : "not ok" ) << '\n' ;
    }
}

http://rextester.com/XVC26554
Inspired by a thread
http://www.cplusplus.com/forum/general/147137/
that JLBorges and I participated in yesterday, here is my generalization to an n-dimensional vector/array combination for any depth n:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <vector>
#include <array>

template <typename CONTAINER, typename E> void store (const E&, CONTAINER&);
template <typename CONTAINER, typename E, std::size_t N> void store (const std::array<E, N>&, CONTAINER&);
template <typename CONTAINER, typename E> void store (const std::vector<E>&, CONTAINER&);

template <typename CONTAINER, typename E>
void store (const std::vector<E>& container, CONTAINER& storage) {
    for (const auto& e : container)
	store<CONTAINER> (e, storage);
}

template <typename CONTAINER, typename E, std::size_t N>
void store (const std::array<E, N>& container, CONTAINER& storage) {
    for (const auto& e : container)
	store<CONTAINER> (e, storage);
}

template <typename CONTAINER, typename E>
void store (const E& element, CONTAINER& storage) {storage.emplace_back(element);}

int main() {
	std::vector<std::vector<std::vector<int>>> v = {
		{ {1, 2}, {3, 4, 5} },
		{ {6, 7}, {4, 8, 9, 10} },
		{ {11, 12, 13}, {}, {14, 15, 16, 17} }
	};
	std::vector<int> storage;
	store (v, storage);
	for (int x : storage) std::cout << x << ' ';  // 1 2 3 4 5 6 7 4 8 9 10 11 12 13 14 15 16 17
	storage.clear();  std::cout << "\n------------------------------\n";

	std::vector<std::array<std::vector<int>, 2>> a = {
		{{ {1, 2}, {3, 4, 5} }},
		{{ {6, 7}, {4, 8, 9, 10} }},
		{{ {11, 12, 13}, {14, 15, 16, 17} }}
	};
	store (a, storage);
	for (int x : storage) std::cout << x << ' ';  // 1 2 3 4 5 6 7 4 8 9 10 11 12 13 14 15 16 17 
}


I will try to generalize this further to any type of STL container in any nesting combination to any depth, and to any type of STL container for the storage.
Last edited on
Topic archived. No new replies allowed.