patterncMinor
Program to tell which if any of three numbers is in the middle
Viewed 0 times
threetheanynumbersprogramtellwhichmiddle
Problem
I wrote this program, and it does what it asks, but it's so big and ugly. What can I change?
Some examples:
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.
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 gapThis 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
#include
#include
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(maiorb){
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;
}Solution
-
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.
-
You should test whether you really got three numbers.
Currently, you don't, which leads to UB.
-
If in doubt, sort.
That eliminates an astonishing number of problems.
-
You aren't using anything from `
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.
-
You should test whether you really got three numbers.
Currently, you don't, which leads to UB.
-
If in doubt, sort.
That eliminates an astonishing number of problems.
-
You aren't using anything from `
, so don't include it.
-
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
#include
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;
printf("Please enter three numbers:\n");
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
#include
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;
printf("Please enter three numbers:\n");
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);
}Code Snippets
#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;
printf("Please enter three numbers:\n");
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+
}#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;
printf("Please enter three numbers:\n");
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);
}Context
StackExchange Code Review Q#109487, answer score: 6
Revisions (0)
No revisions yet.