Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

Tablice dynamiczna wielowymiarowe

Ostatnio zmodyfikowano 2017-11-01 15:23
Autor Wiadomość
fifi_xd
Temat założony przez niniejszego użytkownika
Tablice dynamiczna wielowymiarowe
» 2017-10-30 20:49:23
Witam mam do wykonania zadanie, w którym muszę stworzyć tablice dynamiczną wielowymiarową metodą: "double (*tablica)[] = new double [][]" . Problem polega iż nie mogę wpisać tablicy do funkcji, która jest w pliku nagłówkowym. Ponadto chciałbym aby funkcja mogła wczytywać różne rozmiary tablic nie tylko 5x10 ale np. 10x10, 5x10 itp.
Program: 
main.cpp
C/C++
#include <iostream>
#include "tablica.h"
using namespace std;

int main()
{
    const int W = 5, K = 10;
    double( * tablica )[ W ] = new double[ W ][ W ];
    wpisanie_d( tab_newm );
   
   
   
    return 0;
}

tablica.h

C/C++
#ifndef TABLICA_H_INCLUDED
#define TABLICA_H_INCLUDED
using namespace std;

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <math.h>
#include <new>


template < int W, int K >
void wpisanie_d( double tab[][ K ] ) {
    srand(( unsigned ) time( 0 ) );
    cout << "Tablica dynamiczna " << W << "x" << K << " metoda A wpisana do pliku." << endl;
    for( int x = 0; x < W; ++x ) {
        cout << '\n';
        for( int y = 0; y < K; ++y ) {
            tab[ x ][ y ] =( double ) rand() / RAND_MAX;;
            cout << setprecision( 2 ) << tab[ x ][ y ] << '\t';
        }
    }
}


#endif // TABLICA_H_INCLUDED


Błąd:
[img http://www.imagebam.com/image/52d4b6641961023]
P-166267
Kinexity
» 2017-10-30 21:21:10
Nie podałeś argumentów szablonu. I tak wgl to co to jest  to "tab_newm" przekazywane jako argument?
P-166269
6675636b206f6666
» 2017-11-01 15:23:25
Chodzi o coś takiego?
C/C++
// File: tablica.hpp

#ifndef TABLICA_HPP_INCLUDED
#define TABLICA_HPP_INCLUDED

#include <cstddef>
#include <iomanip>
#include <iostream>
#include <random>

template < typename T >
void fill( T ** arr, size_t x, size_t y )
{
    std::random_device rd;
    std::mt19937 gen( rd() );
    std::uniform_real_distribution < T > dis( T( 0 ), T( 1 ) );
   
    for( size_t ix = 0; ix < x; ++ix )
    for( size_t iy = 0; iy < y; ++iy )
         arr[ ix ][ iy ] = dis( gen );
   
}

template < typename T >
void print( T ** arr, size_t x, size_t y )
{
    for( size_t ix = 0; ix < x; ++ix ) {
        for( size_t iy = 0; iy < y; ++iy )
             std::cout << std::setprecision( 2 ) << arr[ ix ][ iy ] << '\t';
       
        std::cout << "\n";
    }
}

template < typename T >
T ** create( size_t x, size_t y )
{
    T ** arr = new T *[ x ];
   
    for( size_t i = 0; i < x; ++i )
         arr[ i ] = new T[ y ];
   
    return arr;
}

template < typename T >
void destroy( T ** arr, size_t x, size_t /* y */ )
{
    for( size_t i = 0; i < x; ++i )
         delete arr[ i ];
   
    delete arr;
}

#endif // TABLICA_HPP_INCLUDED

C/C++
// File: main.cpp

#include "tablica.hpp"

#include <cstddef>
#include <iostream>

int main()
{
    {
        const size_t x = 5;
        const size_t y = 10;
       
        double ** arr = create < double >( x, y );
        fill( arr, x, y );
        print( arr, x, y );
        destroy( arr, x, y );
    }
   
    std::cout << "\n\n";
   
    {
        const size_t x = 100;
        const size_t y = 20;
       
        double ** arr = create < double >( x, y );
        fill( arr, x, y );
        print( arr, x, y );
        destroy( arr, x, y );
    }
   
    return 0;
}
P-166329
« 1 »
  Strona 1 z 1