0

I have to create a class template for 2 dimension array; every variable in the array would get a random value [65;90], I need to find the MAX value in the array. But in class template data type should be unspecified, so later I could use it with any - int/char etc in main().

I have tried several ways to initialize the array, however it still doesnt work. Now here is one part to put the values in the array and 2nd to print().

#include <iostream> 
#include <cstdlib>
using namespace std; 
  
template <typename T> 
class Array { 
private: 
    T **ptr; 
public: 
    Array(T arr[7][12]); 
    void print(); 
}; 
  
template <typename T> 
Array<T>::Array(T arr[7][12]) { 
    ptr = new T[7][12]; 
    for(int i = 0; i < 7; i++) {
        for (int j=0;j<12;j++) {
            arr[i][j]=rand()%26+65;
            ptr[i][j]=arr[i][j];
        }
    }
} 
  
template <typename T> 
void Array<T>::print() { 
    for(int i = 0; i < 7; i++) {
        for (int j=0; j<12; j++) {
            cout<<*(ptr+j)<<" ";
        }
        cout<<"\n";
    }
    cout<<endl; 
} 
  
int main() { 
    int arr[7][12];
    Array<int> a(arr[7][12]); 
    a.print(); 
    return 0; 
} 
Eli
  • 7
  • 1
  • 3
    " it still doesnt work." is not a description of the problem. – Quimby Dec 14 '20 at 18:58
  • 1
    In `Array a(arr[7][12]);` you're passing an `int` from the `arr` array, not the entire array. Additionally, that integer is out of the bonds of the array so it will result in UB. – GaryNLOL Dec 14 '20 at 19:01
  • 2
    If your array is supposed to be hardcoded to the size `[7][12]`, why are you using `new T[7][12]`? Just change `T **ptr` to `T ptr[7][12];` and skip `new[]` and `delete[]`. – Ted Lyngmo Dec 14 '20 at 19:05

2 Answers2

2

If the dimensions are known at compile time and you can use std::array then you don't need to dynamically allocate the array:

#include <array>

template <typename T,unsigned M,unsigned N>
struct my_2d_array {
    using row_type = std::array<T,N>;
    std::array< row_type, M> data;
};

If you cannot use std::array for some reasons:

template <typename T,unsigned M,unsigned N>
struct my_2d_array {
    T data[M][N];
};

Here

int arr[7][12];
Array<int> a(arr[7][12]); 

you declare arr to be of size 7x12 and then access it of of bounds. The last valid element is arr[6][11] and arr[i][j] is a single element (an int) from the array.

463035818_is_not_a_number
  • 64,173
  • 8
  • 58
  • 126
0

I think that this is what you want

#include <iostream> 
#include <cstdlib>
using namespace std; 
  
template <typename T> 
class Array { 
private: 
    T **ptr; 
public: 
    Array(T arr[][12]); 
    void print(); 
}; 
  
template <typename T> 
Array<T>::Array(T arr[][12]) { 
    ptr = new T *[7];
    for(int i = 0; i < 7; i++) {
        ptr[i] = new T[12];
        for (int j=0;j<12;j++) {
            arr[i][j]=rand()%26+65;
            ptr[i][j]=arr[i][j];
        }
    }
} 
  
template <typename T> 
void Array<T>::print() { 
    for(int i = 0; i < 7; i++) {
        for (int j=0; j<12; j++) {
            cout<<ptr[i][j]<<" ";
        }
        cout<<"\n";
    }
    cout<<endl; 
} 
  
int main() { 
    int arr[7][12];
    Array<int> a(arr); 
    a.print(); 
    return 0; 
} 

Here is ways that you can pass 2D array Passing a 2D array to a C++ function

ngan ngo
  • 41
  • 2