DavidNorgren

Untitled

May 27th, 2015
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.30 KB | None | 0 0
  1. /*
  2.  
  3.     MDH DVA117 Programmering
  4.  
  5.     Laboration 4, Uppgift 1, "Enkel sortering"
  6.         Två funktioner för att sortera eller byta plats
  7.         på flera variablers plats i minnet.
  8.  
  9.     Av David Norgren, dnn13002, 941018-6077
  10.         2013.09.03
  11.  
  12. */
  13.  
  14. #include <stdio.h>
  15.  
  16. int byt(int *tal1, int *tal2)
  17. {
  18.     //Byter plats på de två talen.
  19.     int tmp;
  20.     tmp = *tal1;
  21.     *tal1 = *tal2;
  22.     *tal2 = tmp;
  23.     return 0;
  24. }
  25.  
  26. int sortera(int *tal1, int *tal2, int *tal3)
  27. {
  28.     //Sorterar de tre givna talen från störst->minst.
  29.     while (1)
  30.     {
  31.         if (*tal1 < *tal2)
  32.         {
  33.             byt(tal1, tal2);
  34.             continue;
  35.         }
  36.  
  37.         if (*tal2 < *tal3)
  38.         {
  39.             byt(tal2, tal3);
  40.             continue;
  41.         }
  42.         break;
  43.     }
  44.     return 0;
  45. }
  46.  
  47. int main(void)
  48. {
  49.     int tal1, tal2, tal3;
  50.  
  51.     //Byt två tal
  52.     printf("Enter the two numbers to switch:\n");
  53.     scanf("%d", &tal1);
  54.     scanf("%d", &tal2);
  55.     byt(&tal1, &tal2);
  56.     printf("\nSwitched!\n%d\n%d\n\n", tal1, tal2);
  57.  
  58.     //Sortera tre tal
  59.     printf("Enter three numbers to sort:\n");
  60.     scanf("%d", &tal1);
  61.     scanf("%d", &tal2);
  62.     scanf("%d", &tal3);
  63.     sortera(&tal1, &tal2, &tal3);
  64.     printf("\nSorted!\n%d\n%d\n%d\n\n", tal1, tal2, tal3);
  65.  
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment