Dynamic array value doesn't change

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
What is the value of eil and stl?
I get it with function ivestis(eil, stl); Forgot to include it here.
You are sure it function properly and that eil >= 2 and stl >= 3?
Your assert at line 39 is wrong. Also, dealoc2d() doesn't delete mass itself.
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.
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]
compiler wrote:
line 21: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
Ohh, wow :D. What a mistake
Topic archived. No new replies allowed.