I have to write a program that is going to read n phone call times (in seconds). And then, the program will calculate the total amount collected from all users.
The first 2 minutes is amount 100.
Every additional 10 seconds amount is 5.
I need the formula to find the total amount every additional 10 seconds (more than 2 minutes)..I wrote this code but it's not ok..Can you help me fix it?
 ``12345678910111213141516171819202122232425`` `````` #include using namespace std; int callPrice (int sec) { int res=0; int i=i+10; if (sec<120) res=100; else if (sec>120 && sec%10==0) res=100+(sec-120)*5; return res; } int main () { int n,time,sum; sum=0; cin >> n; for (int cnt=1;cnt<=n;cnt++) { cin >> time; sum=sum+callPrice(time); } cout << sum << endl; return 0; }``````
Every call has at least 100.

Take 2 minutes off. If any time remains, divide the remainder by 10. Now you know what to add to the total, don't you?
 ``12345678910111213141516171819`` ``````int call_price( int secs ) { const int min_cost = 100 ; // for call duration up to 120 secs, cost is min_cost if( secs <= 120 ) return min_cost ; // for call duration > 120 secs, cost is min_cost // plus 5 for every additional 10 seconds or part thereof int extra_secs = secs - 120 ; // first 120 seconds are already accounted for extra_secs += 9 ; // add 9 to round up to next higher multiple of 10 // 30 => 39/10 == 3 // 34 => 43/10 == 4 // 39 => 48/10 == 4 return min_cost + (extra_secs/10) * 5 ; }``````
thank you very much.. :)
Topic archived. No new replies allowed.