Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* fib pe parelelele la diagonala secundara. */
- #include <stdio.h>
- #include <stdlib.h>
- #define SUCCESS (0)
- #define INVALID_INPUT (1)
- #define NEWLINE printf("\n")
- long sum(long a, long b);
- int numberlen(long n);
- int strlen_(char *s);
- long strtol_(char *number);
- void validate(long n, char *number);
- void scanf_number(long *n);
- void *ec_realloc(void *ptr, unsigned int size)
- {
- void *new_ptr;
- new_ptr = realloc(ptr, size);
- if (!new_ptr) {
- printf("[Error] ec_realloc(): realloc() returned NULL.");
- exit(-1);
- }
- return new_ptr;
- }
- void *ec_malloc(unsigned int size)
- {
- return ec_realloc(NULL, size);
- }
- int main()
- {
- int n;
- scanf_number((long *)&n);
- long **mat;
- mat = (long **) ec_malloc(n * sizeof(long *));
- for (int i = 0; i < n; i++) {
- mat[i] = (long *)ec_malloc(n * sizeof(long));
- }
- long fib, pre, antepre;
- fib = 0;
- antepre = 0;
- pre = 1;
- for (
- int col = 0;
- col < n - 1;
- col++
- )
- {
- for (
- int ln = 0;
- ln < col+1;
- ln++
- )
- {
- mat[ln][col - ln] = fib;
- fib = antepre + pre;
- antepre = pre;
- pre = fib;
- }
- }
- for (
- int ln = 1;
- ln < n;
- ln++
- )
- {
- for (
- int col = n - 1;
- col > ln-1;
- col--
- )
- {
- mat[ln+(n-1)-col][col] = fib;
- fib = antepre + pre;
- antepre = pre;
- pre = fib;
- }
- }
- NEWLINE;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- printf("%10ld", mat[i][j]);
- }
- NEWLINE;
- }
- return 0;
- }
- /* strtol wrapper
- * Returns the number string converted to n.
- * If conversion "failed", it returns 1.
- */
- long strtol_(char *number)
- {
- char *err = "";
- /* convert */
- long n = strtol(number, &err, 10);
- /* check */
- if (*err) {
- return INVALID_INPUT;
- }
- return n;
- }
- int strlen_(char *s)
- {
- int nr = 0;
- for (; *s; s++, nr++) { ; }
- return nr;
- }
- /* Validate conversions with strtol_.
- * If conversion is invalid terminate execution. !!
- * */
- void validate(long n, char *number)
- {
- if (
- 1 == n &&
- numberlen(n) != strlen_(number)
- )
- {
- printf("Please enter a valid integer.\n");
- exit(INVALID_INPUT);
- }
- }
- /* Validated read of one long number. */
- void scanf_number(long *n)
- {
- char number[1000];
- scanf("%s", number);
- *n = strtol_(number);
- validate(*n, number);
- }
- int numberlen(long n)
- {
- int nr = 0;
- for (; n; n /= 10, nr++) { ; }
- return nr;
- }
Add Comment
Please, Sign In to add comment