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];
}tenderVariable;
for (j=0;j<3;j++)
totAmt[j]=0;
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)
totAmt[0]=tenderVariable.tenderAmt[i]+totAmt[0];
if (tenderVariable.tenderRiskLvl[i]==2)
totAmt[1]=tenderVariable.tenderAmt[i]+totAmt[1];
if (tenderVariable.tenderRiskLvl[i]==3)
totAmt[2]=tenderVariable.tenderAmt[i]+totAmt[2];
}
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;
}
}
getch();
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:
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).