vector + array

Hello,

a better title would be "store partition number in a 2D array"

I have two small portions of codes which are working fine separatlly CODE 1 and CODE 2, I've posted them for a better understanding.

The problems comes on CODE 3, when I am trying to combine them.

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
// CODE 1 ______  2D ARRAY ______ working fine
#include<iostream>
#include<stdio.h>
#include<stdlib.h>

void part(int col_1, int &row_1, int **Res)
{
    int i;
    
    row_1++;
    for (i = 0; i < col_1; i++)
    {
        Res[i] = (int*)realloc(Res[i], row_1*sizeof(int));
        Res[i][row_1-1]=row_1+i;
    }
    
    if (row_1 < 5)
        part(col_1, row_1, Res);    
}
 
int main()
{
    int col_1, row_1, i, j, **Res;
    	
    col_1 = 15;
    row_1 = 0;
	
    Res = (int**)calloc( col_1, sizeof(int*));
	part(col_1, row_1, Res);
	
    for(i = 0; i < row_1; i++)
    {
        printf("\n");
        for(j = 0; j < col_1; j++)
            printf("%d ", Res[j][i]);
    }   
	
    return 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
// CODE 2 ___ NUMBER PERTITION ___ working fine
#include<iostream>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
 
int part(int col_2, int row_2, vector<int>& v)
{
    int i, f;
    
    if(col_2<1) return 0;        
    v[row_2]=col_2;        
    
    for(i=0; i<=row_2; i++)
        printf("%d ", v[i]);        
    printf("\n");
    
    f=(row_2==0) ? 1 : v[row_2-1];
    
    for(i=f; i<=col_2/2; i++)
    {
        v[row_2] = i;
        part(col_2-i, row_2+1, v);
    }
}

int main()
{
	int col_2, row_2;
		
	col_2 = 15;
	row_2 = 0;            
	vector<int> v(col_2);
	part(col_2, row_2, v);
}


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
// CODE 3____ problem when I combine them _________
#include<vector>
#include<stdio.h>
#include<stdlib.h>
using namespace std;

int part(int col_1, int &row_1, int **Res, int col_2, int row_2, vector<int>& v)
{
    int i, f;

    if(col_2<1) return 0;
    
    v[row_2]=col_2;            
    row_1++;
    
    for (i = 0; i < col_1; i++)
    {
        Res[i] = (int*)realloc(Res[i], row_1*sizeof(int));    
        Res[i][row_1-1]=0;
    }

    for (i = 0; i < col_2; i++)
        Res[i][row_1-1]=v[i];

    f=(row_2==0) ? 1 : v[row_2-1];
    
    for(i=f; i<=col_2/2; i++)
    {
        v[row_2] = i;
        part(col_1, row_1, Res, col_2-i, row_2+1, v);
    }
}

int main()
{
    int col_1, row_1, col_2, row_2, i, j, **Res;
        
    col_1 = col_2 = 15;
    row_1 = row_2 = 0;            
	
    Res = (int**)calloc( col_1, sizeof(int*));
    vector<int> v(col_2);

    part(col_1, row_1, Res, col_2, row_2, v);

    for(i = 0; i < row_1; i++)
    {
        printf("\n");
        for(j = 0; j < col_1; j++)
            printf("%d ", Res[j][i]);
    }   
    return 0;
}


The result on CODE 3 looses some information from here on
15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
1 14 0 0 0 0 0 0 0 0 0 0 0 0 0 
1 1 13 0 0 0 0 0 0 0 0 0 0 0 0 
1 1 1 12 0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 11 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 10 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 9 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 8 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 
........................ and so on


It should look like this, as on CODE 2

15 
1 14 
1 1 13 
1 1 1 12 
1 1 1 1 11 
1 1 1 1 1 10 
1 1 1 1 1 1 9 
1 1 1 1 1 1 1 8 
1 1 1 1 1 1 1 1 7 
........................ and so on


Please be so kind and help me to correct CODE 3.

ps:
a simplified solution to get rid of the vector would be better
Last edited on
Could you elaborate your problem ?

why are there multiple mains in this code.? If they are two different code bases, post separate threads for each specific problem you have.
Last edited on
Hello again,

I have managed to solve the problem, everything is fine now.
It was a mistake of limits

1
2
    for (i = 0; i <= row_2; i++)
        Res[i][row_1-1]=v[i];
Topic archived. No new replies allowed.