Thread: error with simple printf statement

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    39

    error with simple printf statement

    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.....

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    39
    i've no idea what's wrong with my code...cannot be executed...gives "floating point exception(core dumped)" when executed...can u pls help....pls pls....i know its long....but please....

    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 
     37     numItems=totalItems(items);
     38     printf("Enter maximum weight, W: ");
     39     scanf("%d",&W);
     40     greedy(items,numItems,W,taken,&takenitems);
     41     printItems ( taken, takenitems);
     42     // fclose(inFile);
     43 
     44     return 0;
     45 }
     46 
     47 void greedy ( struct Item items[], int numItems, int maxWeight, struct Item     taken[], int *numTaken)
     48 {
     49     int totalWeight=0;int i=0;int j=1;
     50     struct money worth [10];
     51     takemore(items,worth,numItems);
     52     while( totalWeight<=maxWeight)
     53         for(i=0;i<numItems;i++)
     54             for (j=1;j<=numItems;j++)
     55                 if (((worth[numItems-j]).weight)<=maxWeight-totalWeight)
     56                 {
     57                     ((taken[i]).id)=((worth[numItems-j]).id);
     58                     ((taken[i]).value)=((worth[numItems-j]).value);
     59                     ((taken[i]).weight)=((worth[numItems-j]).weight);
     60                     totalWeight=totalWeight+((taken[i]).weight);
     61                     (*numTaken)++;
     62                     break;
     63                 }
     64     return;
     65 }
     66 
     67 int totalItems(struct Item items[])
     68 {
     69     int i=0;int count=0;
     70     for (i=0;i<10; i++)
     71         if (((items[i]).id)!=0)
     72             count++;
     73 
     74     return count;
     75 }
     76 
     77 void takemore (struct Item items[], struct money worth[], int numItems)
     78 {
     79     int i=0;
     80     for(i=0; i<numItems; i++)
     81     {
     82         ((worth[i]).id)=((items[i]).id);
     83         ((worth[i]).value)=((items[i]).value);
     84         ((worth[i]).weight)=((items[i]).weight);
     85         ((worth[i]).valuePerWeight)=((items[i]).value)/((items[i]).weight);
     86     }
     87 
     88     sort(worth, numItems);
     89     return;
     90 }
     91 void sort(struct money worth[], int numItems)
     92 {
     93     int i=0; int j=0; int minIndex=0; double temp=0.000;
     94     for (i=0; i< numItems-1; i++)
     95     {
     96         minIndex=i;
     97         for (j=i+1; j<numItems; j++)
     98             if (((worth[j]).valuePerWeight)<((worth[minIndex]).                 valuePerWeight))
     99                 minIndex=j;
    100         if (minIndex!=i)
    101         {
    102             temp=((worth[minIndex]).valuePerWeight);
    103             ((worth[minIndex]).valuePerWeight) = ((worth[i]).                   valuePerWeight);
    104             ((worth[i]).valuePerWeight)=temp;
    105         }
    106     }
    107     return;
    108 }
    109 
    110 void printItems (struct Item items[], int numItems)
    111 {
    112     int i=0;int totalvalue=0; int totalweight=0;
    113     printf("\n");
    114     printf("Items taken: ");
    115     for (i=0;i<numItems;i++)
    116         printf("<%d, %d, %d> ",((items[i]).id),((items[i]).value),              ((items[i]).weight));
    117     for (i=0; i<numItems;i++)
    118        { totalweight=totalweight+((items[i]).weight);
    119          totalvalue= totalvalue+((items[i]).value);
    120        }
    121 
    122     printf("Total value:  %d; total weight:  %d",totalvalue,totalweight);
    123 }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with a printf statement
    By Dr.Xperience in forum C Programming
    Replies: 5
    Last Post: 02-23-2011, 01:26 AM
  2. printf statement executed twice
    By ananddr in forum C Programming
    Replies: 4
    Last Post: 05-07-2009, 12:17 AM
  3. ASCII Character in printf Statement
    By JacquesLeJock in forum C Programming
    Replies: 1
    Last Post: 03-28-2008, 10:21 PM
  4. Printing %d and newline in printf statement
    By lookforraja07 in forum C Programming
    Replies: 2
    Last Post: 12-19-2007, 10:04 AM
  5. storing printf statement in to string
    By ameet in forum C Programming
    Replies: 1
    Last Post: 09-20-2004, 10:33 PM