943,938 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1120
  • C++ RSS
Mar 28th, 2007
0

Structure in Maps

Expand Post »
Hi friends,

I am back. I was doing some hands-on with the C++ and I think I am a bit confident now I came across an Exercise question in a C++ book which is as follows:

There is a structure with the variables payment_amount, method_of_payment and risk or priority. The output will give the risk wise cumulative frequency of the Amount. Oh, i dont know how to go about on explainin the question further lol I will give the code which I wrote using struct and basic for loops in VC++. Here it is:

c++ Syntax (Toggle Plain Text)
  1.  
  2. #include "stdafx.h"
  3. #include "iostream"
  4. #include <conio.h>
  5. using namespace std;
  6.  
  7.  
  8. int _tmain(int argc, _TCHAR* argv[])
  9. {
  10. int i,j;
  11. int totAmt[3];
  12. bool b[]={false, false ,false};
  13. struct Tender {
  14. int tenderType[5];
  15. int tenderAmt[5];
  16. int tenderRiskLvl[5];
  17. }tenderVariable;
  18.  
  19. for (j=0;j<3;j++)
  20. totAmt[j]=0;
  21.  
  22. for (i=0; i<5; i++)
  23. {
  24. cout << "Enter Tender Type: " << i+1 << endl;
  25. cin >> tenderVariable.tenderType[i];
  26. cout << "Enter Tender Amount: " << i+1 << endl;
  27. cin >> tenderVariable.tenderAmt[i];
  28. cout << "Enter Tender Risk Level: " << i+1 << endl;
  29. cin >> tenderVariable.tenderRiskLvl[i];
  30. }
  31.  
  32. for (i=0;i<5;i++)
  33. {
  34. if (tenderVariable.tenderRiskLvl[i]==1)
  35. totAmt[0]=tenderVariable.tenderAmt[i]+totAmt[0];
  36. if (tenderVariable.tenderRiskLvl[i]==2)
  37. totAmt[1]=tenderVariable.tenderAmt[i]+totAmt[1];
  38. if (tenderVariable.tenderRiskLvl[i]==3)
  39. totAmt[2]=tenderVariable.tenderAmt[i]+totAmt[2];
  40. }
  41. cout << endl << "The result is " << endl << endl;
  42. cout << "Type \t" << "Amt \t" << "Risk\t" << endl;
  43. for (i=0;i<5;i++)
  44. {
  45. if (tenderVariable.tenderRiskLvl[i]==1 && b[0]==false)
  46. {
  47. cout << tenderVariable.tenderType[i] << "\t" << totAmt[0] << "\t" << tenderVariable.tenderRiskLvl[i] << endl;
  48. b[0] = true;
  49. }
  50. else if (tenderVariable.tenderRiskLvl[i]==2 && b[1] == false)
  51. {
  52. cout << tenderVariable.tenderType[i] << "\t" << totAmt[1] << "\t" << tenderVariable.tenderRiskLvl[i] << endl;
  53. b[1] = true;
  54. }
  55. else if (tenderVariable.tenderRiskLvl[i]==3 && b[2] == false)
  56. {
  57. cout << tenderVariable.tenderType[i] << "\t" << totAmt[2] << "\t" << tenderVariable.tenderRiskLvl[i] << endl;
  58. b[2] = true;
  59. }
  60. }
  61.  
  62. getch();
  63. return 0;
  64. }

So, say if I give the inputs as
Type--Amount --RiskLevel
20 -- 40 --1
21 --50 --1
22 --20 --2
23 --43 --3
24 --100 --1


The Output for my above code will be:

20--190--1
22--20--2
23--43--3



Now I want to implement the same using MAP in STL. I have no idea how I can go on with this. Can any one guide me through please? :rolleyes:


Oh yes! I forgot to say that I do have a logic in my mind for this but not sure how to implement though. I am planning to pass the risk level and struct as parameters for my map. Then i will check if the risk level already exists. If yes, i will add up the amount; otherwise, I will add a new row for the risk level (if the risk level is not already exists, i mean).
Last edited by dahlia_06; Mar 28th, 2007 at 8:00 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dahlia_06 is offline Offline
7 posts
since Mar 2007
Mar 28th, 2007
0

Re: Structure in Maps

There is a small problem in teh way you've used structure and arrays concepts. Better way of doing this would be to have a struct with 3 members and then use an array of struct. (instead of having arrays as members of the struct)
Like this:
  1. struct Tender {
  2. int tenderType;
  3. int tenderAmt;
  4. int tenderRiskLvl;
  5. }tenderVariableArray[5];

A better usecase for using std::map is when you have to associate a value with a key (e.g. "name of student" is the key and "his marks in all exams" is the value).

For your problem in general I would recomment you try with vector instead of map.

In general for STL Map Intro you can see this and this. You should find things on vector as well in same links.

If you want to try out map check this may be there is a problem more suitable for usage of map.
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Mar 28th, 2007
0

Re: Structure in Maps

Okay mate. I shall look at the STL stuff where you linked me to. Meanwhile, could you please tellme what is the significance of using array of members and array of struct. I was in an assumption its actually two ways of implementing it. Has it got something to do with the way how the memory been allocated? Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dahlia_06 is offline Offline
7 posts
since Mar 2007
Mar 28th, 2007
0

Re: Structure in Maps

Click to Expand / Collapse  Quote originally posted by dahlia_06 ...
I was in an assumption its actually two ways of implementing it. Has it got something to do with the way how the memory been allocated? Thanks
Indeed what you've done and what I suggested are 2 diff ways of doing the same thing.
No it has nothing to do with memory allocation. Both use same amount of memory. The memory layout will be different though.

Click to Expand / Collapse  Quote originally posted by dahlia_06 ...
Meanwhile, could you please tell me what is the significance of using array of members and array of struct.
Like I said these are 2 ways of solving same problem. But as they teach in classroom abt OOAD, first thing you do is read the problem statement like a story and underline the words you think are objects. These become your classes (or structures). If you go this way Tender is one such word so you'll make a class or a struct out of it.
Now when you have a struct to represent a Tender, you can do it in 2 ways. But in your way (read this literally not technically) "ONE object of Tender does NOT represent ONE Tender". In my way it does.
So my way makes things easier to work with, understandable...
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help with calculating avrg from input file.
Next Thread in C++ Forum Timeline: who will convert in to machine language





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC