Here is a complete and working code that
1. Adds the numbers(input) to get a sum(output)
2. Sorts the numbers in a descending order
Can you post any suggestions on how I can improve my code :)
main.c
#include <stdio.h>
#include "header.h"
main (){
int sum, x, y, z,choices;
input(&x, &y, &z);
printf ("\nChoose function\n");
do{
printf("\n1 -Input-Output (Add) function \n");
printf("2 - Swap and Sort function \n");
printf("3 - Quit\n");
scanf ("%d", &choices);
switch (choices) {
case 1: addfunction(&x, &y, &z, &sum);
printf ("%d+%d+%d=%d.\n",x,y,z,sum);
break;
case 2: sortnswap(&x, &y, &z);
printf ("%d %d %d \n", x, y, z);
break;
case 3: printf ("\nprogram terminated!!!\n");
break;
}
}
while(choices!=3);
}
header.h
void input (int *a, int *b, int *c) { //Input Function
printf ("\nEnter x: ");
scanf ("%d", a);
printf ("Enter y: ");
scanf ("%d", b);
printf ("Enter z: ");
scanf ("%d", c);
}
void addfunction(int *a, int *b, int *c, int *sum) { //Output function
*sum=*a+*b+*c;
}
void sortnswap(int *a, int *b, int *c) { //Sort and Swap Function
int hi,lo,mid;
printf ("\nThe numbers sorted from descending order\n");
if (*a>=*b && *a>=*c && *b>=*c) {
hi = *c;
mid = *b;
lo = *a;
}
else if (*a>=*b && *a>=*c && *c>=*b) {
hi = *b;
mid = *c;
lo = *a;
}
else if (*b>=*a && *b>=*c && *c>=*a) {
hi = *a;
mid = *c;
lo = *b;
}
else if (*b>=*a && *b>=*c && *a>=*c) {
hi = *c;
mid = *a;
lo = *b;
}
else if (*c>=*a && *c>=*b && *b>=*a) {
hi = *a;
mid = *b;
lo = *c;
}
else if (*c>=*a && *c>=*b && *a>=*b) {
hi = *b;
mid = *a;
lo = *c;
}
*a = lo;
*b = mid;
*c = hi;
}