Hi guys,
I tried debugging my programme with a printf statement. But it gives me the following error:tutorial_9_qn3.c:37: error: parse error at end of input. Can you please advise what's wrong?
Code:
1 #include <stdio.h>
2
3 struct Item { int id; int value; int weight;};
4 struct money {int id; int value; int weight; double valuePerWeight;};
5
6 void greedy (struct Item items[], int numItems, int maxWeight, struct Item taken [], int *numTaken);
7 int totalItems(struct Item items[]);
8 void takemore (struct Item items[], struct money worth[], int numItems);
9 void sort(struct money worth[], int numItems);
10 void printItems ( struct Item items[], int numItems);
11
12 int main ( )
13 {
14 struct Item items[10];
15 //struct money worth [10];
16 struct Item taken[10];
17
18 int numItems=0; int takenitems=0;
19 int W=0; int i=0;
20 int *numTaken=0;
21
22 numTaken=&takenitems;
23 *numTaken=0;
24
25 FILE *inFile;
26
27 inFile = fopen("items.txt","r");
28 if (inFile==NULL)
29 {
30 printf("File open error!\n");
31 return 0;
32 }
33
34 while (fscanf(inFile, "%d", &items[i])==1)
35 i++;
36 int try=items[0].id;
37 printf("%d",try);
38 /* numItems=totalItems(items);
39 printf("Enter maximum weight, W: ");
40 scanf("%d",&W);
41 greedy(items,numItems,W,taken,&takenitems);
42 printItems ( taken, takenitems);
43 // fclose(inFile);
44
45 return 0;
46 }
47
48 void greedy ( struct Item items[], int numItems, int maxWeight, struct Item taken[], int *numTaken)
49 {
50 int totalWeight=0;int i=0;int j=1;
51 struct money worth [10];
52 takemore(items,worth,numItems);
53 while( totalWeight<=maxWeight)
54 for(i=0;i<numItems;i++)
55 for (j=1;j<=numItems;j++)
56 if (((worth[numItems-j]).weight)<=maxWeight-totalWeight)
57 {
58 ((taken[i]).id)=((worth[numItems-j]).id);
59 ((taken[i]).value)=((worth[numItems-j]).value);
60 ((taken[i]).weight)=((worth[numItems-j]).weight);
61 totalWeight=totalWeight+((taken[i]).weight);
62 (*numTaken)++;
63 break;
64 }
65 return;
66 }
67
68 int totalItems(struct Item items[])
69 {
70 int i=0;int count=0;
71 for (i=0;i<10; i++)
72 if (((items[i]).id)!=0)
73 count++;
74
75 return count;
76 }
77
78 void takemore (struct Item items[], struct money worth[], int numItems)
79 {
80 int i=0;
81 for(i=0; i<numItems; i++)
82 {
83 ((worth[i]).id)=((items[i]).id);
84 ((worth[i]).value)=((items[i]).value);
85 ((worth[i]).weight)=((items[i]).weight);
86 ((worth[i]).valuePerWeight)=((items[i]).value)/((items[i]).weight);
87 }
88
89 sort(worth, numItems);
90 return;
91 }
92 void sort(struct money worth[], int numItems)
93 {
94 int i=0; int j=0; int minIndex=0; double temp=0.000;
95 for (i=0; i< numItems-1; i++)
96 {
97 minIndex=i;
98 for (j=i+1; j<numItems; j++)
99 if (((worth[j]).valuePerWeight)<((worth[minIndex]).valuePerWeight))
100 minIndex=j;
101 if (minIndex!=i)
102 {
103 temp=((worth[minIndex]).valuePerWeight);
104 ((worth[minIndex]).valuePerWeight) = ((worth[i]).valuePerWeight);
105 ((worth[i]).valuePerWeight)=temp;
106 }
107 }
108 return;
109 }
110
111 void printItems (struct Item items[], int numItems)
112 {
113 int i=0;int totalvalue=0; int totalweight=0;
114 printf("\n");
115 printf("Items taken: ");
116 for (i=0;i<numItems;i++)
117 printf("<%d, %d, %d> ",((items[i]).id),((items[i]).value),((items[i]). weight));
118 for (i=0; i<numItems;i++)
119 { totalweight=totalweight+((items[i]).weight);
120 totalvalue= totalvalue+((items[i]).value);
121 }
122
123 printf("Total value: %d; total weight: %d",totalvalue,totalweight);
124 }*/
please help....i'll be very grateful.....