galib71

ALGO LAB 2(SORT)

Oct 8th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct laptop
  5. {
  6. int l;
  7. int c;
  8. float uc;
  9. };
  10. int main()
  11. {
  12. int i=0,j=0,k=0,m=0;
  13. struct laptop arr[5],temp;
  14. for(; i<5,j<5; i++,j++)
  15. {
  16. cout<<"L : ";
  17. cin>>arr[j].l;
  18. cout<<"C : ";
  19. cin>>arr[i].c;
  20. }
  21. i=0;
  22. j=0;
  23. for(; k<5; k++)
  24. {
  25. arr[k].uc= (float)arr[i].c/arr[j].l;
  26. i++;
  27. j++;
  28. }
  29.  
  30. cout<<endl<<" L"<<" "<<" C"<<" "<<" UC"<<endl;
  31. for(; m<5; m++)
  32. {
  33. cout<<" "<<arr[m].l<<" "<<arr[m].c<<" "<<arr[m].uc<<endl;
  34. }
  35. cout<<endl;
  36. /////////////////////////////////////////////////////////
  37. ////////////////////////////////////////////////////////
  38. ///////////////////////////////////////////////////////
  39. int input=m,i1=0,j1;
  40. for(;i1<input;i1++)
  41. {
  42. for(j1=i1+1;j1<input;j1++)
  43. {
  44. if(arr[i1].uc>arr[j1].uc)
  45. {
  46. temp=arr[i1];
  47. arr[i1]=arr[j1];
  48. arr[j1]=temp;
  49. }
  50. }
  51. }
  52. cout<<endl<<endl<<"After Shorting : ";
  53. cout<<endl<<" L"<<" "<<" C"<<" "<<" UC"<<endl;
  54. int g=0;
  55. for(; g<5; g++)
  56. {
  57. cout<<" "<<arr[g].l<<" "<<arr[g].c<<" "<<arr[g].uc<<endl;
  58. }
  59. cout<<endl;
  60. int capa,u=0,store;
  61. int item=g;
  62. float ans=0;
  63. cout<<"How many laptop you want to buy : ";
  64. cin>>capa;
  65. store=capa;
  66. while (capa>0 && u<item)
  67. {
  68. if(capa>=arr[u].l)
  69. {
  70. ans+=(float)arr[u].c;
  71. capa-=arr[u].l;
  72. }
  73. else
  74. {
  75. ans+=capa*(float)arr[u].uc;
  76. capa=0;
  77. }
  78. u++;
  79. }
  80. if(capa==0)
  81. {
  82. cout<<endl<<"Minimum cost to buy"<<" "<<store<<" laptop : "<<ans<<endl;
  83. }
  84. else
  85. {
  86. cout<<"Insufficient"<<endl;
  87. }
  88.  
  89. return 0;
  90. }
Add Comment
Please, Sign In to add comment