2-d pointer function

The greedy box function is not writing into the array and I would like to know why.

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
#include <iostream>
#include <cstdio>
#define MAX 200
using namespace std;

bool NextAmountEven(int, int );
char **M_GreedyBox(int [], int);

int main()
{  
  int coins[] = {200, 100, 50, 20, 10, 5, 2, 1};
  int *CoinPtr = coins;
  int size = sizeof coins / sizeof *coins;
  char **NumToMax = new char *[10];
  NumToMax = M_GreedyBox(CoinPtr, size);
  
  for (int A = 0; A < size - 1; ++A)
    cout << NumToMax[A] << " ";
  cout <<endl;
    
    return 0;
}

char **M_GreedyBox(int *arr, int sz)
{
  char **toRet = new char*[10];
  int temp;
  int temp2;
  for (int S = 0; S < sz - 1; ++S)
  {
    toRet[S] = new char[10];
    temp = (arr[S] / arr[S + 1]);
    temp2 = arr[S] - temp*(arr[S + 1]);
    
    if (NextAmountEven(arr[S], arr[S + 1]))
      sscanf(toRet[S], "%d R 0", &temp);
    else
      sscanf(toRet[S], "%d R %d", &temp, &temp2);
   }
    
  return toRet;
}

bool NextAmountEven (int Set, int Next)
{
  if (Set % Next == 0)
    return true;
  return false;
}
Last edited on
why don't you just use
void func(int array[])

instead of setting up a pointer to the array and then using
void func(int *pointer)
I can do that but that doesn't really solve my problem
Last edited on
you know that you are just reading from arr and not writting, right?
so that function will never write to the array
You had a number of memory leaks in your code. Your line#14 you allocated a 2d array on the heap, then in line#15 you allocate the address (or at least that was your intention) to your local variable named 'toRet' in M_GreedyBox(...) thus losing the address of the memory you already allocated on the heap, so you now have no way to regain that lost memory. You don't delete any memory allocated (that you still have a pointer to) on the heap.

Getting back to the problem of returning the result from M_GreedyBox(...). You are allocating memory and storing the address's in toRet a local variable to M_GreedyBox(...) but as soon as the toRet variable goes out of scope it is lost so your address retuned points to nothing! To get around this problem pass a pointer through to the M_GreedyBox(...) function in its argument list, see my out variable in the argument list in the code below:

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
#include <iostream>
#include <cstdio>
#define MAX 200
using namespace std;

void M_GreedyBox(int*, const int, char***);

int main()
{  
  int coins[] = {200, 100, 50, 20, 10, 5, 2, 1};
  char** NumToMax = NULL;
  const int SIZE = sizeof coins / sizeof &coins;
  M_GreedyBox(&coins[0], SIZE, &NumToMax);
  
  for (int n = 0; n < SIZE-1; ++n)
  {
    cout << NumToMax[n] << " ";
	 delete[] NumToMax[n];
	 NumToMax[n] = NULL;
  }

  delete[] NumToMax;
  NumToMax = NULL;
  cout << endl;
    
    return 0;
}

void M_GreedyBox(int* in, const int SZ, char*** out)
{
  *out = new char*[SZ];
  int temp;
  int temp2;
  for (int n = 0; n < SZ-1; ++n)
  {
    (*out)[n] = new char[10];
    temp = (in[n] / in[n + 1]);
    temp2 = in[n] - temp*(in[n + 1]);
    
    if (in[n] % in[n + 1] == 0)
      sprintf((*out)[n], "%d R 0", temp);
    else
      sprintf((*out)[n], "%d R %d", temp, temp2);
   }
}
@Smac89 - when you have read the replies please update us to let us know if the advice given helps or not. If not, why not and what further help in which area of your code do you require assistance with etc...
Last edited on
@Darkmaster thanks so much! That is exactly what I needed to hear.

@ajh32, Thanks for your response too. When I get to school, I will look at it more.

Guys it works now! WOOT

Thanks all

$ ./Coin\ Sums
2 R 0
2 R 0
2 R 10
2 R 0
2 R 0
2 R 1
2 R 0
Last edited on
Topic archived. No new replies allowed.