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:

#include "stdafx.h"
#include "iostream"
#include <conio.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
	int i,j;
	int totAmt[3];
	bool b[]={false, false ,false};
	struct Tender {
	int tenderType[5];
	int tenderAmt[5];
	int tenderRiskLvl[5];

	for (j=0;j<3;j++)

	for (i=0; i<5; i++)
		cout << "Enter Tender Type: " << i+1 << endl;
		cin >> tenderVariable.tenderType[i];
		cout << "Enter Tender Amount: " << i+1 << endl;
		cin >> tenderVariable.tenderAmt[i];
		cout << "Enter Tender Risk Level: " << i+1 << endl;
		cin >> tenderVariable.tenderRiskLvl[i];

	for (i=0;i<5;i++)
        if (tenderVariable.tenderRiskLvl[i]==1)
		if (tenderVariable.tenderRiskLvl[i]==2)
		if (tenderVariable.tenderRiskLvl[i]==3)
cout << endl << "The result is " << endl << endl;
cout << "Type \t" << "Amt \t" << "Risk\t" << endl;
for (i=0;i<5;i++)
	if (tenderVariable.tenderRiskLvl[i]==1 && b[0]==false)
	cout << tenderVariable.tenderType[i] << "\t" << totAmt[0] << "\t" << tenderVariable.tenderRiskLvl[i] << endl;
	b[0] = true;
	else if (tenderVariable.tenderRiskLvl[i]==2 && b[1] == false)
	cout << tenderVariable.tenderType[i] << "\t" << totAmt[1] << "\t" << tenderVariable.tenderRiskLvl[i] << endl;
	b[1] = true;
	else if (tenderVariable.tenderRiskLvl[i]==3 && b[2] == false)
	cout << tenderVariable.tenderType[i] << "\t" << totAmt[2] << "\t" << tenderVariable.tenderRiskLvl[i] << endl;
	b[2] = true;

return 0;

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:


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

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:

struct Tender {
int tenderType;
int tenderAmt;
int tenderRiskLvl;

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.

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

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.