Advertisement
Alx09

8.8

Apr 15th, 2022
1,106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdarg.h>
  4. #include <string.h>
  5.  
  6. void swap(int *xp, int *yp)
  7. {
  8.     int temp = *xp;
  9.     *xp = *yp;
  10.     *yp = temp;
  11. }
  12.  
  13. void sort(int n, ...)
  14. {
  15.     va_list argument;           //declaram variabila de tip va_list
  16.     int i, *elmAnt, *elmCurent;
  17.    
  18.     int sortat = 0;
  19.     do{
  20.         sortat = 1;
  21.     va_start(argument, n);  //apelam va_start() si o initializam
  22.     elmAnt = va_arg(argument, int *);
  23.     for (i = 0; i < n - 1; i++) {
  24.        
  25.         elmCurent = va_arg(argument, int *);
  26.         if ((*elmAnt) > (*elmCurent)) {
  27.             swap(elmAnt, elmCurent);
  28.             sortat = 0;
  29.         }
  30.         elmAnt = elmCurent;
  31.     }
  32.     }while (sortat == 0);
  33.    
  34.     va_end(argument);             // terminare parcurgere + eliberare memorie
  35.    
  36. }
  37.  
  38. int main(void){
  39.     int a = 2,  b = 1, c = 0;
  40.     sort(3, &a, &b, &c);
  41.     printf("%d %d %d", a, b, c);
  42.     scanf("%*c");
  43.     return 0;
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement