passing structuce to function as an argument

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2009
Posts: 28
Reputation: thebluestar is an unknown quantity at this point 
Solved Threads: 0
thebluestar thebluestar is offline Offline
Light Poster

passing structuce to function as an argument

 
0
  #1
32 Days Ago
This is the problem:

In this exercise you have to write a program to manage items in a super market.
Suppose that the data about each item includes:
• Item’s ID: ID of the item, it is an integer which ranges from 1 to 9999
• Item’s type: Its value is:
• 1 if the item is food
• 2 if the item is electrical good
• 3 if the item is household good
• Item’s price

1. Create a structure template that contains data of one item. Then use this template to declare an array
of structures that stores data of maximum 50 items.
2. Ask user to input the list of N items from the keyboard. Then print out the list of items on the screen.
3. Print out the title of the item which has maximum price.
Here is my source code
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #define MAX 50
  4.  
  5. struct good
  6. {
  7. int id;
  8. int type;
  9. float price;
  10. };
  11. struct good item[MAX];
  12. int n;
  13. int pos;
  14.  
  15. void input()
  16. { char temp[40];
  17. int i;
  18. printf("how many items u want to store: "); scanf("%d",&n);
  19. for(i=0; i<n; i++)
  20. {
  21. printf("\nItem %d: ",i+1);
  22. do
  23. {
  24. printf("\n id:");
  25. scanf("%d",&item[i].id);
  26. } while( (item[i].id<0) || (item[i].id>9999) );
  27. printf("\n Type: ");
  28. fflush(stdin);
  29. gets(temp);
  30. if(strcmp(temp,"food")==0)
  31. item[i].type=1;
  32. else if(strcmp(temp,"electrical")==0)
  33. item[i].type=2;
  34. else if(strcmp(temp,"household")==0)
  35. item[i].type=3;
  36. printf("\n Price: ");
  37. scanf("%f",&item[i].price);
  38. }
  39. }
  40.  
  41. void display()
  42. {
  43. int i;
  44. for(i=0; i<n; i++)
  45. {
  46. printf("Item %d: ",i+1);
  47. printf("\n id %d, type %d, price %f\n",item[i].id,item[i].type, item[i].price);
  48. }
  49. }
  50.  
  51.  
  52.  
  53. struct good price(struct good arr[])
  54. {
  55. int i,pos;
  56. struct good temp;
  57.  
  58. temp = item[0];
  59. for(i=0; i<n; i++)
  60. {
  61. if(temp.price < item[i].price)
  62. temp = item[i];
  63. }
  64. return temp ;
  65. }
  66.  
  67. void display_item(struct good *p)
  68. {
  69. printf("\n id %d, type %d, price %f\n",p -> id, p -> type, p -> price);
  70. }
  71.  
  72. int main()
  73. {
  74. input();
  75. display();
  76. struct good item_max;
  77. item_max =struct good price(item[MAX]);
  78. display_item(&item_max);
  79. getch();
  80. return 0;
  81. }
to solve problem number 3, I use a function to find the item having max price , then print its details ,but I cannot passing the structure "item" found in function struct good price(struct good arr[]) to display_item function.
Please help me find the mistake!
Thanks a lot!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,740
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 739
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess
 
3
  #2
32 Days Ago
>Please help me find the mistake!
I suspect the mistake is a complete failure to study whatever book on C you have. Instead of trying to finish this exercise in one swell foop, perhaps you should be writing a simple skeleton that passes structures around. At least that way you'll fail on fundamental concepts much sooner and will have an easier time sorting things out.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC