# Program to tell which if any of three numbers is in the middle

I wrote this program, and it does what it asks, but it's so big and ugly. What can I change?

Some examples:

a  b  c --->
5  1  12   a is between b and c
5  12 1    a is between c and b
5  5  2    there is no gap


This was in an example of a test that is supposed to be small, so that's why I'm asking if there is another way.

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main (void){
int a,b,c;

printf("Inserir 3 números: "); //its in portuguese, insert 3 numbers
scanf("%i %i %i",&a,&b,&c);

if(a==b||b==c||a==b){
printf("não há intervalo\n"); //there is no gap
exit(0);
}

int maior, meio, menor,flag,flag2,flag3;

maior=a; //maior means biggest
flag=1;
if(maior<b){
maior=b;
flag=2;
}if(maior<c){
flag=3;
maior=c;
}

if(maior!=a){
meio=a;  //meio means middle
flag2=1;
if(meio<b&&b<maior){
meio=b;
flag2=2;
}if(meio<c&&c<maior){
flag2=3;
}
}else{
meio=b;
flag2=2;
if(meio<c&&c<maior){
flag2=3;
}
}

menor=a; //menor means smallest
flag3=1;
if(menor>b){
menor=b;
flag3=2;
}if(menor>c){
flag3=3;
}

if(flag2==1)
printf("o a entre [");
if(flag2==2)
printf("o b entre [");
if(flag2==3)
printf("o c entre [");

if(flag3==1)
printf("a e ");
if(flag3==2)
printf("b e ");
if(flag3==3)
printf("c e ");

if(flag==1)
printf("a]\n");
if(flag==2)
printf("b]\n");
if(flag==3)
printf("c]\n");

return 0;
}

• Welcome to Code Review! Good job on your first question. Nov 1, 2015 at 23:34

1. Well, first of all, consider adopting one of the myriad common indentation-styles and use it consistently.
Doing so will make your code far easier to read and write.

2. You should test whether you really got three numbers.
Currently, you don't, which leads to UB.

3. If in doubt, sort.

That eliminates an astonishing number of problems.

4. You aren't using anything from <math.h>, so don't include it.

5. exit(0); and return 0; have nearly the same effect when done in main.
The only differences are when main is called recursively, and we won't do such an abomination outside the IOCCC. (Or if we change to a different language, like C++, but who would do that?)

#include <stdlib.h>
#include <stdio.h>

typedef struct namednumber {int num; char name; } namednumber;

static int comp_namednumber(const void* pa, const void* pb) {
int a = ((namednumber*)pa)->num,
b = ((namednumber*)pb)->num;
return (a > b) - (a < b);
}

int main() { // No need for void in the parameter-list, this is a definition
int a, b, c;
if(3 != scanf("%i %i %i", &a, &b, &c)) {
printf("You didn't enter three numbers. Aborting.\n");
return EXIT_FAILURE;
}

if(a == b || b == c || c == a) {
printf("There is no gap.");
return 0;
}

namednumber data[] = {{a,'a'},{b,'b'},{c,'c'}};
qsort(data, 3, sizeof *data, comp_namednumber);
printf("%c is between %c and %c\n", data[1].name, data[0].name, data[2].name);

return 0; // This line is implicit in C99+
}


Actually, getting out qsort is severe overkill, so here with a standard sorting-network:

#include <stdlib.h>
#include <stdio.h>

static void sort2(int* a, char* na, int* b, char* nb) {
if(*a < *b) return;
int t = *a;
*a = *b;
*b = t;
char nt = *na;
*na = *nb;
*nb = nt;
}

int main() {
int a, b, c;
if(3 != scanf("%i %i %i", &a, &b, &c)) {
printf("You didn't enter three numbers. Aborting.\n");
return EXIT_FAILURE;
}

if(a == b || b == c || c == a) {
printf("There is no gap.");
return 0;
}

char na = 'a', nb = 'b', nc = 'c';
sort2(&a, &na, &b, &nb);
sort2(&a, &na, &c, &nc);
sort2(&b, &nb, &c, &nc);
printf("%c is between %c and %c\n", nb, na, nc);
}

• Well, I don't understand about half of what you wrote, but it's surely much more better looking than mine. I'll play around with it. Thank you very much! Nov 2, 2015 at 8:58