15,179,246 members
See more:
//c++
```#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
int t;
cin >> t;
while (t--)
{
int n, a, b;
cin >> n >> a >> b;

int x = min(a, b);
int y = max(a, b);
int ans = n;

for (int i = x; i < y; i++)
{
int temp = max((i - 1) + min(x - 1, i - x), (n - i - 1) + min(n - y, y - i - 1));
ans = min(ans, temp);
}

cout << ans <<endl;
}
}```

What I have tried:

//c lang

```#include <stdio.h>
#define min(x,y) (x <= y ? x : y)
#define max(x,y) (x <= y ? x : y)

int main()
{
int t;
scanf("%d",&t);
while (t--)
{
int n, a, b;
scanf("%d%d%d",&n,&a,&b);

int x = min(a, b);
int y = max(a, b);
int ans = n;

for (int i = x; i < y; i++)
{
int temp = max((i - 1) + min(x - 1, i - x), (n - i - 1) + min(n - y, y - i - 1));
ans = min(ans, temp);
}

printf(ans,"\n");
}
}```

error
timeout: the monitored command dumped core
Posted
Updated 26-May-21 12:35pm
v2
Dave Kreskowiak 25-May-21 12:16pm

That's not "your code". That's code you got off the web and modified so it wouldn't be so obvious you're using someone's code.

Now you want it converted to C so you can turn in someone's work as your own.

## Solution 1

See __min | Microsoft Docs[^].

Maybe you are using gcc in which case you will need to create your own macros or inline functions for min and max. something like:
C++
`#define min(x,y) (x <= y ? x : y)`

[/edit]
v2
CPallini 25-May-21 7:04am

5.
Richard MacCutchan 25-May-21 7:48am

Thanks.
NISHANK BABU 26-May-21 3:26am

#include <stdio.h>
#define min(x,y) (x <= y ? x : y)
#define max(x,y) (x <= y ? x : y)

int main()
{
int t;
scanf("%d",&t);
while (t--)
{
int n, a, b;
scanf("%d%d%d",&n,&a,&b);

int x = min(a, b);
int y = max(a, b);
int ans = n;

for (int i = x; i < y; i++)
{
int temp = max((i - 1) + min(x - 1, i - x), (n - i - 1) + min(n - y, y - i - 1));
ans = min(ans, temp);
}

printf(ans,"\n");
}
}
error
timeout: the monitored command dumped core
Richard MacCutchan 26-May-21 3:38am

You have defined max the same as min. The max definition needs to test x as greater than y.
NISHANK BABU 26-May-21 3:40am

i didn't get u..
Richard MacCutchan 26-May-21 3:41am

Look at your definition of min and max, they are both returning the minimum value.
Rick York 26-May-21 4:14am

Also - you have <= for min. That is not correct. It should be just < and min should have just > as its comparison
operator.
Richard MacCutchan 26-May-21 4:20am

I suggested `<=` although not strictly the rule. I am never sure why you should take the second value if they are equal.

## Solution 2

This is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be "good code" in the target language – they are based on very different frameworks, and what makes something work in one language does not always "translate" directly into another.
So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it’ll be a nightmare to debug if it doesn’t work "straight out of the box".
Instead, use the source code as a specification for a new app written in and for the target language / framework and write it from scratch using the original as a "template". You will get a much, much better result that will save you a lot of time in the long run.

And to be honest, if you wrote that code in C++, you should be able to rewrite it for C in minutes, if not seconds - C is a subset of C++.

## Solution 3

I suggest activating the compiler flags for warnings. With gcc this would be -Wall and -Wconversion for example.

An error message like the following should definitely be displayed:
C
`error C2664: "int printf(const char *const ,...)" `

Thats because
C
`printf(ans,"\n");`

Is definitely not functional.

As already written, the macro for max () is wrong.

The loop works, but it probably doesn't make much sense:
C
```for (int i = x; i < y; i++) {
int temp = max((i - 1) + min(x - 1, i - x), (n - i - 1) + min(n - y, y - i - 1));
ans = min(ans, temp);
}```