Question: #include #include #include #include #define MAX _ LINE _ LENGTH 1 0 2 4 #define MAX _ LABEL _ LENGTH 1 0 0 int max

#include
#include
#include
#include
#define MAX_LINE_LENGTH 1024
#define MAX_LABEL_LENGTH 100
int max(int a, int b);
int countLines(FILE *file);
void parseItems(FILE *file, char **labels, int *val, int *wt);
int knapsack(int W, int* wt, int* val, int n)
{
int withoutItem, withItem;
if (n ==0|| W ==0)
{
return 0;
}
--n;
if (wt[n]> W)
{
return knapsack(W, wt, val, n);
}
else
{
withoutItem = knapsack(W, wt, val, n);
withItem = val[n]+ knapsack(W - wt[n], wt, val, n);
return max(withItem, withoutItem);
}
}
int main(int argc, char *argv[])
{
FILE *file;
int count, i, W =50, maximumValue;
int *val,*wt;
char **labels;
if (argc !=2)
{
printf("Usage: %s #include
#include
#include
#include #include
#include
#include
#include
#define MAX_LINE_LENGTH 1024
#define MAX_LABEL_LENGTH 100
int max(int a, int b);
int countLines(FILE *file);
void parseItems(FILE *file, char **labels, int *val, int *wt);
int knapsack(int W, int* wt, int* val, int n)
{
int withoutItem, withItem;
if (n ==0|| W ==0)
{
return 0;
}
--n;
if (wt[n]> W)
{
return knapsack(W, wt, val, n);
}
else
{
withoutItem = knapsack(W, wt, val, n);
withItem = val[n]+ knapsack(W - wt[n], wt, val, n);
return max(withItem, withoutItem);
}
}
int main(int argc, char *argv[])
{
FILE *file;
int count, i, W =50, maximumValue;
int *val,*wt;
char **labels;
if (argc !=2)
{
printf("Usage: %s
", argv[0]);
return 1;
}
file = fopen(argv[1],"r");
if (!file)
{
perror("Error opening file");
return 1;
}
count = countLines(file);
val = malloc(count * sizeof(int));
wt = malloc(count * sizeof(int));
labels = malloc(count * sizeof(char*));
for (i =0; i < count; i++)
labels[i]= malloc(MAX_LABEL_LENGTH * sizeof(char));
parseItems(file, labels, val, wt);
fclose(file);
printf("Items available:
");
for (i =0; i < count; i++)
{
// printf("Item %d: %s (Value: %d, Weight: %d)
", i +1, labels[i], val[i], wt[i]);
}
maximumValue = knapsack(W, wt, val, count);
printf("
Maximum weight capacity of the knapsack: %d
", W);
printf("Maximum value that can be accommodated: %d
", maximumValue);
for (i =0; i < count; i++)
free(labels[i]);
free(labels);
free(val);
free(wt);
return 0;
}
int max(int a, int b)
{
return (a > b)? a : b;
}
int countLines(FILE *file)
{
int lines =0;
char line[MAX_LINE_LENGTH];
while (fgets(line, sizeof(line), file))
lines++;
rewind(file);
return lines;
}
void parseItems(FILE *file, char **labels, int *val, int *wt)
{
char line[MAX_LINE_LENGTH];
char *token;
int index =0;
while (fgets(line, sizeof(line), file))
{
token = strtok(line,",");
strcpy(labels[index], token);
token = strtok(NULL,",");
val[index]= atoi(token);
token = strtok(NULL,",");
wt[index]= atoi(token);
index++;
}
}
How to parallelize this code using OpenMP?", argv[0]);
return 1;
}
file = fopen(argv[1],"r");
if (!file)
{
perror("Error opening file");
return 1;
}
count = countLines(file);
val = malloc(count * sizeof(int));
wt = malloc(count * sizeof(int));
labels = malloc(count * sizeof(char*));
for (i =0; i < count; i++)
labels[i]= malloc(MAX_LABEL_LENGTH * sizeof(char));
parseItems(file, labels, val, wt);
fclose(file);
printf("Items available:
");
for (i =0; i < count; i++)
{
// printf("Item %d: %s (Value: %d, Weight: %d)
", i +1, labels[i], val[i], wt[i]);
}
maximumValue = knapsack(W, wt, val, count);
printf("
Maximum weight capacity of the knapsack: %d
", W);
printf("Maximum value that can be accommodated: %d
", maximumValue);
for (i =0; i < count; i++)
free(labels[i]);
free(labels);
free(val);
free(wt);
return 0;
}
int max(int a, int b)
{
return (a > b)? a : b;
}
int countLines(FILE *file)
{
int lines =0;
char line[MAX_LINE_LENGTH];
while (fgets(line, sizeof(line), file))
lines++;
rewind(file);
return lines;
}
void parseItems(FILE *file, char **labels, int *val, int *wt)
{
char line[MAX_LINE_LENGTH];
char *token;
int index =0;
while (fgets(line, sizeof(line), file))
{
token = strtok(line,",");
strcpy(labels[index], token);
token = strtok(NULL,",");
val[index]= atoi(token);
token = strtok

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!