Nov 4, 2014 at 8:02pm UTC
I am tryint to dynamically create two dimensional double type array. I wrote this code, but when I try to input any value for ex. mass[1][2] = 5; and printf whole array, it just shows 0.
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 51 52 53 54 55 56 57 58 59 60 61 62 63
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <assert.h>
void ivestis(int &, int &);
double ** aloc2d(int , int );
void dealoc2d(double **, int );
main(){
int eil, stl;
double **mass;
ivestis(eil, stl);
mass = aloc2d(eil, stl);
for (int k=0; k<eil; k++){
printf("\n" );
for (int j=0; j<stl; j++){
printf("%d " , mass[k][j]);
}
}
dealoc2d(mass, eil);
}
double ** aloc2d(int eil, int stl){
double **mass;
mass = new double *[eil];
assert (mass != 0);
for (int i=0; i<eil; i++){
mass[i] = new double [stl];
assert (mass[i] != 0);
}
for (int q=0; q<eil; q++){
for (int w=0; w<stl; w++) mass[q][w] = q+w;
}
return mass;
}
void dealoc2d(double ** mass, int eil){
for (int i=0; i<eil; i++){
delete [] mass[i];
}
delete [] mass;
}
void ivestis(int & eil, int & stl){
printf("Masyvo eiluciu skaicius: " );
std::cin >> eil;
printf("Masyvo stulpeliu skaicius: " );
std::cin >> stl;
}
Last edited on Nov 6, 2014 at 7:45pm UTC
Nov 4, 2014 at 11:02pm UTC
What is the value of eil and stl?
Nov 5, 2014 at 5:53pm UTC
I get it with function ivestis(eil, stl); Forgot to include it here.
Nov 5, 2014 at 6:01pm UTC
You are sure it function properly and that eil >= 2 and stl >= 3?
Nov 5, 2014 at 8:03pm UTC
Your assert at line 39 is wrong. Also, dealoc2d()
doesn't delete mass itself.
Nov 6, 2014 at 7:48pm UTC
Updated code a little to fix dhayden's pointed out mistake, but it still doesn't work for me properly and I really can't figure what is wrong.
Nov 6, 2014 at 8:26pm UTC
printf("%d " , mass[k][j]);
%d tells printf to print an integer but you're passing it a double. Use %f instead, or do cout << mass[k][j]
Nov 6, 2014 at 9:32pm UTC
Ohh, wow :D. What a mistake