-1

I am creating a program that returns the rest with the minimum number of coins. In input I have a set of coins cuts and their amount. I did a java clone that works discreetly. Now I would need to turn it into C, a language I'm less prepared for. Can anyone help me? My main problem is how to return the results to C. In java are returned as a list of whole arrays. Like these: [1, 0, 0, 1, 0]

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Change {
int[] values = {10,20,50,100,200};

public static void main(String[] args) {
    int[] values = {7, 2, 3, 6, 0};
    int[] ammounts = {1, 1, 1, 1, 0};
    List<Integer[]> results = solutions(values, ammounts, new int[5], 13, 0);
    for (Integer[] result : results){
        System.out.println(Arrays.toString(result));
    }


}

public static List<Integer[]> solutions(int[] values, int[] ammounts, int[] variation, int price, int position){
    List<Integer[]> list = new ArrayList<>();
    int value = compute(values, variation);
    if (value < price){
        for (int i = position; i < values.length; i++) {
            if (ammounts[i] > variation[i]){
                int[] newvariation = variation.clone();
                newvariation[i]++;
                List<Integer[]> newList = solutions(values, ammounts, newvariation, price, i);
                if (newList != null){
                    list.addAll(newList);
                }
            }
        }
    } else if (value == price) {
        list.add(myCopy(variation));
    }
    return list;
}

public static int compute(int[] values, int[] variation){
    int ret = 0;
    for (int i = 0; i < variation.length; i++) {
        ret += values[i] * variation[i];
    }
    return ret;
}

public static Integer[] myCopy(int[] ar){
    Integer[] ret = new Integer[ar.length];
    for (int i = 0; i < ar.length; i++) {
        ret[i] = ar[i];
    }
    return ret;
}

}

This is my code for the moment:

struct final_list *solution_change(struct rest * r, struct variation * v, struct final_list *lfinale, int price, int position) {
int value = compute(r, v);
if (value < price) {
    for (int i = position; i < r->max_lenght; i++) {
        if (r->coin[i].number_coin > v->variation[i]) {
            struct variation *newVar = v;
            newVar->variation[i]++;
            // recall the function (is recursive) i also need help here
            if (newVar != NULL) {
                if (lfinale->lenght == 0) {
                    lfinale->lenght++;
                    lfinale->variation = (struct variation *)calloc(lfinale->lenght, sizeof(struct variation));
                }
                else {
                    lfinale->lenght++;
                    lfinale->variation = (struct variation *)realloc(lfinale->variation, lfinale->lenght * sizeof(struct variation));
                }
                lfinale->variation[lfinale->lenght] = *newVar;
            }
        }
    }
}
else if (value == price) {
    //add myCopy
    lfinale->variation[lfinale->lenght] = *v;
}

MY STRUCT:

struct coin {
  unsigned int value_coin; // value money
  unsigned int number_coin; // amount money
};

struct rest {
  unsigned int max_lenght;
  struct coin *coin;
};

struct variation {
  int lenght;
  int *variation;
};

struct final_list {
int lenght;
variation *variation;
};

1 Answers1

0

Just return lfinale.

You don't actually need to return it since you already have it in the calling function.

In the calling function:

struct final_list results;
results.lenght = 0;
results.variaton = NULL;

solution_change(&lrest, &lvariation, &results, price, position);

// results has now been populated.

This is the equivalent of creating the List before the call and then sending it as a parameter.

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82