0

Link to the problem : https://www.codechef.com/problems/HS08TEST

Description

Pooja would like to withdraw X $US from an ATM. The cash machine will only accept the transaction if X is a multiple of 5, and Pooja's account balance has enough cash to perform the withdrawal transaction (including bank charges). For each successful withdrawal the bank charges 0.50 $US. Calculate Pooja's account balance after an attempted transaction.

Input

Positive integer 0 < X <= 2000 - the amount of cash which Pooja wishes to withdraw.

Nonnegative number 0<= Y <= 2000 with two digits of precision - Pooja's initial account balance.

Output

Output the account balance after the attempted transaction, given as a number with two digits of precision. If there is not enough money in the account to complete the transaction, output the current bank balance.

Example - Successful Transaction

Input: 30 120.00

Output: 89.50

Example - Incorrect Withdrawal Amount (not multiple of 5)

Input: 42 120.00

Output: 120.00

Example - Insufficient Funds

Input: 300 120.00

Output: 120.00

I unable to find any incorrect output when I change the parameters in transaction function but still I am unable to submit it successfully due to some error which I am not able to find. This is my first try with codechef or in fact with any competitive programming so any help will be appreciated.Here's my code:

#define count 0.5

float transaction(int , float);

int main(void) 
{
    float transaction(int x, float acc_bal)
    {
        float z=acc_bal-(x+count);
        if(x<acc_bal)
        {
            if(x%5==0)
            {
                printf("%.2f",z);
            }   
        }

        if(x%5!=0)
        {
            printf("%0.2f",acc_bal);
        }
    }   

    transaction(42,120.00); 
}
klutt
  • 30,332
  • 17
  • 55
  • 95
Epsilon zero
  • 113
  • 5
  • The link you have added is not public access. Perhaps you should summarise the question from the site on here so we can understand what you are trying to do? – Martin Oct 28 '19 at 13:54
  • @Martin that was the submissions page. – Weather Vane Oct 28 '19 at 13:56
  • @molbdnilo: Oddly, it does. https://ideone.com/hV0g4Z – Fred Larson Oct 28 '19 at 13:56
  • 1
    @molbdnilo Yes it does IF the compiler supports functions in functions. Some do, but not all. – klutt Oct 28 '19 at 13:57
  • @WeatherVane Not sure why it's in the question then as it's inaccessible. – Martin Oct 28 '19 at 13:58
  • 3
    Your first step should be to write in standard C and not use funky compiler-specific extensions. – molbdnilo Oct 28 '19 at 13:58
  • Here is a test you can try: `x=100` and `acc_bal = 100.45` Happy debugging... – Support Ukraine Oct 28 '19 at 14:00
  • @Martin it was there because OP wanted to link to the problem, and I have corrected it. – Weather Vane Oct 28 '19 at 14:01
  • 1
    I suggest you look out for inexact values. Floating point values cannot exactly store some decimal values such as `0.1` and `0.01` and for this reason some recommend to use integers for [currency problems](https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency). – Weather Vane Oct 28 '19 at 14:05

3 Answers3

2

Functions inside functions is not standard C. Some compilers supports it, but then you rely on compiler extensions. Do like this:

#include <stdio.h>
#define count 0.5

float transaction(int x, float acc_bal)
{
    float z=acc_bal-(x+count);
    if(x<acc_bal)
    {
        if(x%5==0)
        {
            printf("%.2f",z);
        }   
    }

    if(x%5!=0)
    {
        printf("%0.2f",acc_bal);
    }
}   

int main(void) 
{
    transaction(42,120.00); 
}

But your code is unnecessarily messy and is missing the case where x is greater than the balance. Also, it does not need to be declared as a float. I would write like this instead:

void transaction(int x, float acc_bal)
{
    const float count = 0.5;
    float after=acc_bal-(x+count);

    if(x%5 == 0 && after>=0.0) {
        printf("%.2f\n",after);
    } else {
        printf("%0.2f\n",acc_bal);
    }
}   
klutt
  • 30,332
  • 17
  • 55
  • 95
1

Besides the function-in-function stuff and a missing return value (which isn't used btw), the problem is that you never check if the account goes negative.

Check what happens if you call your code with transaction(100,100.45);

Instead try like:

#define count 0.5

void transaction(int x, float acc_bal)
{
    float z = acc_bal - count - x;  // z will be new balance if the transaction is made
    if (z >= 0 && x % 5 == 0)       // check if the transaction can be made
    {
        acc_bal = z;                // it could... so update balance
    }
    printf("%0.2f\n",acc_bal);      // print current balance
}   

int main(void) 
{
    transaction(100,100.45);  // not ok - can't pay bank charges
    transaction(99, 100.45);  // not ok - x is not a multiple of 5
    transaction(95, 100.55);  // ok
}

Output:

100.45
100.45
5.05
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
0

The code in the question exhibits several deviations and potential deviations from the problem specification:

  • The program does not correctly test whether a transaction can be completed, because it tests whether the requested amount is less than the account balance instead of whether the requested amount plus the withdrawal fee is less than or equal to the balance.
  • It is not known that float has sufficient precision to represent the bank balance.
  • The program fails to end output lines with a newline character. (Although the problem statement does not state this explicitly, it may be expected implicitly.)
  • The program fails to include <stdio.h>.
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312