简体   繁体   中英

Translate from C++ to C

I try to translate a code from c++ to c but the program didnt work properly.

This is the c++ code

#include <iostream>
#include <limits.h>
using namespace std;

int CoinChangeDynamic(int jumlah, int d[], int size, int C[], int s[])
{
   C[0] = 0;
   for(int j = 1; j <= jumlah; j++) {
     C[j] = INT_MAX;
     for(int i = 0; i < size; i++) {
       if(j >= d[i] && 1 + C[j-d[i]] < C[j] ) {
         C[j] = 1 + C[j-d[i]];
         // i-th denomination used for the amount of j
         s[j] = i;
       }
     }
   }
 return C[jumlah];
}

int main()
{
   int d[] = {1, 5, 10, 25, 50, 100,500,1000};
   int jumlah ;//= 67;
   cout <<"Masukan Jumlah Nilai Koin = ";cin >>jumlah;
   int size = sizeof(d)/sizeof(d[0]);
   int *C = new int[jumlah+1];
   int *s = new int[jumlah+1];
   int ans = CoinChangeDynamic(jumlah, d, size, C, s);
   cout << "Minimal Koin = " << ans << endl;
   cout << "Menggunakan Koin: " ;
   int k = jumlah;
   while(k) {
      cout << d[s[k]] << " ";
      k = k - d[s[k]];
   }
   delete[] C;
   delete[] s;
   return 0;
}

And this is my translation

#include <stdio.h>
#include <limits.h>

int CoinChangeDynamic(int jumlah, int d[], int size, int C[], int s[])
{
    //variabel
    int j, i;

    //program
    C[0] = 0 ;
    for(j = 1; j <= jumlah; j++) {
    C[j] = INT_MAX;  
    for(i = 0; i < size; i++) {
       if(j >= d[i] && 1 + C[j-d[i]] < C[j] ) {
            C[j] = 1 + C[j-d[i]];
            // i-th denomination used for the amount of j
            s[j] = i;
       }
    }
 }
return C[jumlah];
}

int main()
{
    //variabel
    int d[] = {1, 5, 10, 25, 50, 100,500,1000};
    int jumlah;
    printf ("Masukan Jumlah Nilai Koin = "); scanf ("%i", &jumlah);
    int size = sizeof(d)/sizeof(d[0]);
    int *C = (int *) malloc(sizeof(jumlah+1));
    int *s = (int *) malloc(sizeof(jumlah+1));

    //program

    int ans = CoinChangeDynamic(jumlah, d, size, C, s);
    printf ("Minimal Koin = %i \n", ans);
    printf ("Menggunakan Koin: ") ;
    int k = jumlah;  
    while(k) 
    {
       printf (" %i ", d[s[k]]);
       k = k - d[s[k]];
    }
    free (C);
    free (s);
    return 0;
}

But the program didn't work properly like the C++ code. is there someone who can help me

This will help:

int *C = (int *) malloc((jumlah+1)*sizeof(int));
int *s = (int *) malloc((jumlah+1)*sizeof(int));

You've mistranslated the calls to new .

NB: Purists don't like the cast of malloc to (int *) . However for clarity of what is causing the error I've made the minimal changes to correct it.

Also, remember that both C++ iostreams and C FILE s are buffered and you probably need to flush them.

BTW, you C++ code is not genuine C++, you should use containers in it.

To flush a C++ iostream, use std::flush . Notice that std::endl is also flushing (after emitting the newline).

To flush C FILE s, use fflush . Notice that stdout is often (but not always) line buffered, so ending printf control strings with a newline \\n is preferable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM