kaenan

Fib matrice

Nov 12th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. /* fib pe parelelele la diagonala secundara. */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #define SUCCESS (0)
  5. #define INVALID_INPUT (1)
  6. #define NEWLINE printf("\n")
  7.  
  8. long sum(long a, long b);
  9. int numberlen(long n);
  10. int strlen_(char *s);
  11. long strtol_(char *number);
  12. void validate(long n, char *number);
  13. void scanf_number(long *n);
  14.  
  15. void *ec_realloc(void *ptr, unsigned int size)
  16. {
  17.     void *new_ptr;
  18.  
  19.     new_ptr = realloc(ptr, size);
  20.  
  21.     if (!new_ptr) {
  22.         printf("[Error] ec_realloc(): realloc() returned NULL.");
  23.         exit(-1);
  24.     }
  25.  
  26.     return new_ptr;
  27. }
  28.  
  29. void *ec_malloc(unsigned int size)
  30. {
  31.     return ec_realloc(NULL, size);
  32. }
  33.  
  34.  
  35. int main()
  36. {
  37.     int n;
  38.     scanf_number((long *)&n);
  39.  
  40.     long **mat;
  41.  
  42.     mat = (long **) ec_malloc(n * sizeof(long *));
  43.  
  44.     for (int i = 0; i < n; i++) {
  45.         mat[i] = (long *)ec_malloc(n * sizeof(long));
  46.     }
  47.  
  48.     long fib, pre, antepre;
  49.     fib = 0;
  50.     antepre = 0;
  51.     pre = 1;
  52.  
  53.     for (
  54.         int col = 0;
  55.         col < n - 1;
  56.         col++
  57.         )
  58.     {
  59.         for (
  60.             int ln = 0;
  61.             ln < col+1;
  62.             ln++
  63.             )
  64.         {
  65.             mat[ln][col - ln] = fib;
  66.  
  67.             fib     = antepre + pre;
  68.             antepre = pre;
  69.             pre     = fib;
  70.         }
  71.     }
  72.     for (
  73.         int ln = 1;
  74.         ln < n;
  75.         ln++
  76.         )
  77.     {
  78.         for (
  79.             int col = n - 1;
  80.             col > ln-1;
  81.             col--
  82.             )
  83.         {
  84.             mat[ln+(n-1)-col][col] = fib;
  85.             fib     = antepre + pre;
  86.             antepre = pre;
  87.             pre     = fib;
  88.         }
  89.     }
  90.     NEWLINE;
  91.     for (int i = 0; i < n; i++) {
  92.         for (int j = 0; j < n; j++) {
  93.             printf("%10ld", mat[i][j]);
  94.         }
  95.         NEWLINE;
  96.     }
  97.     return 0;
  98. }
  99.  
  100. /* strtol wrapper
  101.  * Returns the number string converted to n.
  102.  * If conversion "failed", it returns 1.
  103.  */
  104. long strtol_(char *number)
  105. {
  106.     char *err = "";
  107.     /* convert */
  108.     long n = strtol(number, &err, 10);
  109.     /* check */
  110.     if (*err) {
  111.         return INVALID_INPUT;
  112.     }
  113.     return n;
  114. }
  115. int strlen_(char *s)
  116. {
  117.     int nr = 0;
  118.     for (; *s; s++, nr++) { ; }
  119.     return nr;
  120. }
  121. /* Validate conversions with strtol_.
  122.  * If conversion is invalid terminate execution. !!
  123.  * */
  124. void validate(long n, char *number)
  125. {
  126.     if (
  127.         1 == n &&
  128.         numberlen(n) != strlen_(number)
  129.         )
  130.     {
  131.         printf("Please enter a valid integer.\n");
  132.         exit(INVALID_INPUT);
  133.     }
  134. }
  135. /* Validated read of one long number. */
  136. void scanf_number(long *n)
  137. {
  138.     char number[1000];
  139.     scanf("%s", number);
  140.     *n = strtol_(number);
  141.     validate(*n, number);
  142. }
  143.  
  144. int numberlen(long n)
  145. {
  146.     int nr = 0;
  147.     for (; n; n /= 10, nr++) { ; }
  148.     return nr;
  149. }
Add Comment
Please, Sign In to add comment