Re: 0/1 Knapsack problem, what am I doing wrong
- From: Bubba <nickname@xxxxxxxxxxxxxxxxxxx>
- Date: 22 Apr 2009 19:27:38 GMT
Ben Bacarisse's log on stardate 22 tra 2009
A few comments...
Thx, I was secretly hoping for something like that... ;)
Missing } here!
Noted. I removed all unnecessary parenthesizes.
Why is everything double? When w[x] and p[x] are used, they must be
converted to int so the effect is that M only holds integers. Using
only integers would simplify a few things.
Indeed, this actually, when I look at the algorithm and the code makes
sense...
It is usually a good idea to test the scanf returns the correct count
(1 in this case). You could have a function that prompts and then
reads an int. This may see like a lot of work for a few cases, but
programming is cumulative. The program you write tomorrow might
benefit from this function so it is almost always worth writing it
now.
Something like this? I could call a function for this one, but just as a
concept, is ti OK?
int s_err;
....
s_err = scanf ("%d", &c);
if (s_err != 1 || s_err == EOF)
{
fprintf (stderr, "Error inputing capacity, terminating...");
exit(2);
}
printf ("Enter knapsack capacity: ");
scanf ("%d", &c);
double **M = (double **)malloc ((n + 1) * sizeof (double *));
I'd write:
double **M = malloc((n + 1) * sizeof *M);
for (i = 0 ; i < n + 1; i++)
M[i] = (double *)malloc ((c + 1) * sizeof (double));
Same here (and below). This one looks more complex but the pattern
is the same:
M[i] = malloc ((c + 1) * sizeof *M[i]);
I did as you suggested, but can you clarify the reasons for such
intervention?[1]
Note that if you do this and decide to switch to int rather than
double, none of the malloc calls need to change.
I did switch to int and removed the casts.
Small point: you are not being consistent. Some of your one-line
loops have {}s and some don't. I favour not having them, but many
people feel differently. Consistency is probably more important than
which of these options you choose.
Critics noted. I agree and also dislike {} on oneliners and am naturally
sloppy... :)
double *w = (double *)malloc (n * sizeof (double));
double *p = (double *)malloc (n * sizeof (double));
if (M == NULL || p == NULL || w == NULL)
This worries me. It is possible that one of the row allocations in
the loop above could fail, while those for w and p might succeed.
You need to test inside the loop as well. I'd probably write:
for (i = 0 ; i < n + 1; i++)
if ((M[i] = malloc ((c + 1) * sizeof *M[i])) == NULL)
break;
and then test that the loop ran to the end:
if (i != n + 1 || M == NULL || p == NULL || w == NULL)
Thx, yes, I forgot to check the rows allocation...
You might want to look at the C FAQ. 6.16 is all about allocating
multi-dimensional arrays. http://c-faq.com/aryptr/dynmuldimary.html
I did when I wrote the code, that's why I'm perplexed with the
intervention [1]. :)
Thx a lot for your kind comments.
Best regards.
--
Everything will be okay
in the end.
If it's not okay
it's not the end!
.
- Follow-Ups:
- Re: 0/1 Knapsack problem, what am I doing wrong
- From: Ben Bacarisse
- Re: 0/1 Knapsack problem, what am I doing wrong
- From: James Kuyper
- Re: 0/1 Knapsack problem, what am I doing wrong
- References:
- 0/1 Knapsack problem, what am I doing wrong
- From: Bubba
- Re: 0/1 Knapsack problem, what am I doing wrong
- From: Eric Sosman
- Re: 0/1 Knapsack problem, what am I doing wrong
- From: Bubba
- Re: 0/1 Knapsack problem, what am I doing wrong
- From: Chris McDonald
- Re: 0/1 Knapsack problem, what am I doing wrong
- From: Bubba
- Re: 0/1 Knapsack problem, what am I doing wrong
- From: Keith Thompson
- Re: 0/1 Knapsack problem, what am I doing wrong
- From: Bubba
- Re: 0/1 Knapsack problem, what am I doing wrong
- From: Ben Bacarisse
- 0/1 Knapsack problem, what am I doing wrong
- Prev by Date: Re: Float comparison
- Next by Date: Re: Float comparison
- Previous by thread: Re: 0/1 Knapsack problem, what am I doing wrong
- Next by thread: Re: 0/1 Knapsack problem, what am I doing wrong
- Index(es):
Relevant Pages
|