2d array function

im having difficulties understanding how to make a function using 2d array. i just want to to call that function to show my 2d matrix where the user will input the size and it will adjust according to it. i read somewhere that there many ways and use vectors and pointers but i still havent study that since im new to c++.

what is the simplest way to do it? here is my code and i just copy the function somewhere. i think is called by array function by reference?


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
#include<iostream>
using namespace std;

template<int rows, int cols>
void print_array( int (&array)[rows][cols] )
{
    for( int r=0; r < rows; r++ ) {
        for( int c=0; c < cols; c++ )
           cout << array[r][c] << ' ';
        
       cout << '\n';
    }
}
int main(){
	int i,j,size;


	cout<<"Array size: ";
	cin>>size;

	int array[size][size];
	
	for( i=0;i<n;i++){
		for(j=0;j<n;j++){
			array[i][j] = 1;		
		}	
	}
	
	print_array(array);
}



You can't declare array size at runtime like this in standard C++:
int array[size][size];
(Some compilers allow it, partly because it is legitimate in the C99 version of C or allocatable arrays in fortran).

Either use dynamic arrays (with new and delete) or use vectors.

Personally, I prefer to map to 1-d arrays, since the storage for your 2-d array would be contiguous. However, the following would work. I've transferred the templating to the type of elements in the array and passed the sizes as function arguments.


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
#include<iostream>
using namespace std;

template<typename T>
void print_array( T *p, int rows, int cols )
{
    for( int r=0; r < rows; r++ ) {
        for( int c=0; c < cols; c++ )
           cout << *p++ << ' ';
       cout << '\n';
    }
}
int main(){
	int i,j;
	
	const int size = 5;
	//cout<<"Array size: ";
	//cin>>size;

	int array[size][size];   // Can't declare size at runtime in C++
	
	for( i=0;i<size;i++){
		for(j=0;j<size;j++){
			array[i][j] = 1;		
		}	
	}
	
	print_array<int>(&array[0][0], size, size);
}
im currently using dev c++ and didn't know that i cant use this kind of decleration int array[size][size]; , i'll keep this in mind. i see so i have to look into vectors so that i can input sizes of the 2d array. thanks again for your help.
> what is the simplest way to do it?

Use a vector of vectors. std::vector<> https://cal-linux.com/tutorials/vectors.html
Simplicity is the ultimate sophistication.

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
#include <iostream>
#include <vector>

// https://cal-linux.com/tutorials/vectors.html
void print_array( const std::vector< std::vector<int> >& array2D ) { // const: always be const correct

    // http://www.stroustrup.com/C++11FAQ.html#for
    // http://www.stroustrup.com/C++11FAQ.html#auto
    for( const auto& row : array2D ) { // for each row in the array

        for( int v : row ) std::cout << v << ' '; // print each value
        std::cout << '\n';
    }
}

int main() {

	std::size_t size = 0 ;
	std::cout << "Array size: ";
	std::cin >> size;
	std::cout << "\ncreating square matrix with size == " << size << "\n\n" ;

        // create a size x size 2d array, with all values initialised to 1
	std::vector< std::vector<int> > array( size, std::vector<int>( size, 1 ) ) ;

	// set the diagonal values to 9
	for( std::size_t i = 0 ; i < size ; ++i ) array[i][i] = 9 ;

	print_array(array);
}

http://coliru.stacked-crooked.com/a/0c89f0a91338e3a1
@JLBorges thank you for the link and will read it now. kind of surprise that creating a function for displaying 2d array so that my main function wil look clean will make things complicated. thank you and i will study more.
> im currently using dev c++

It would appear that Dev-C++ (orwell) is no longer maintained (the last release was in 2015).

Strongly consider switching to a more modern compiler and IDE.

On Windows, Visual Studio or CodeBlocks would be reasonable choices.
Visual Studio: http://www.cplusplus.com/forum/beginner/231305/#msg1044273
CodeBlocks: http://www.codeblocks.org/

On Unix/Linux, install a reasonably recent version of the GNU or LLVM compiler; precisely how to do this varies.
For example, to install g++ 7.3 on FreeBSD, as root:
cd /usr/ports/lang/gcc7/ && make install clean (bootstrap build from source)
or pkg install gcc7 (install binary)
Topic archived. No new replies allowed.