-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ8.cpp
More file actions
96 lines (80 loc) · 2.24 KB
/
Q8.cpp
File metadata and controls
96 lines (80 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include<iostream>
#include<math.h>
using namespace std;
struct license{
//structure to store license no. along with rate of growth of its price
int license_no;
float rate_of_growth;
};
int find_max(license l_arr[], int n){
int max = 0;
//tranversing entire array to find most expensive license
for(int i=1;i<n;i++){
if(l_arr[i].rate_of_growth > l_arr[max].rate_of_growth)
max = i;
}
swap(l_arr[0].rate_of_growth, l_arr[max].rate_of_growth);
swap(l_arr[0].license_no, l_arr[max].license_no);
//return the license of first license object in array l_arr
return l_arr[0].license_no;
}
void print_order(license l_arr[], int n){
//This function finds and prints the order of buying license
for(int i=0; i<n; i++){
//call to find maximum from the remaining array l[i..n-1]
cout<<"License "<<find_max((l_arr+i), n-i);
cout<<" : Rate "<<l_arr[i].rate_of_growth<<endl;
}
}
float amount(license l_arr[], int n){
/*
Description
-----------
This function returns the total minimized amount to be spend to buy license
Input variables
---------------
- l_arr : array of objects of license type
- n : number of objects in array l_arr
Return value
------------
None
Side effects
------------
Sorts l_arr in decreasing order of rate_of_growth and prints this sorted array.
*/
float amount = 0;
for(int i=0; i<n; i++){
amount += pow(l_arr[i].rate_of_growth, i);
}
return amount*100;
}
int main(){
int n;
cout<<"\t\t\tSOFTWARE LICENSE PROBLEM\n";
while(true){
cout<<"\n\nEnter number of cryptographic softwares : ";
cin>>n;
if(n>=1) //number of software must be atleast 1
break;
else
cout<<"\nNo. of softwares should be atleast 1!";
}
license *l = new license[n];
//input rate
for(int i=0; i<n; i++){
float temp;
cout<<"\n\nEnter rate of growth for license "<<i+1<<" : ";
cin>>temp;
//test whether rate entered is greater than 1 or not
if(temp <= 1){
cout<<"\nrate must be greater than 1!";
i--;
continue;
}
l[i].license_no = i+1;
l[i].rate_of_growth = temp;
}
cout<<"\n\nOrder of buying license is \n\n";
print_order(l, n);
cout<<"\n\nMinimized amount is : $"<<amount(l, n);
}