Σημειώσεις - Εισαγωγή στον Προγραμματισμό

Κεφάλαιο 4: Συναρτήσεις, εμβέλεια και αναδρομή

Δομή ενός προγράμματος C – Συναρτήσεις

Συνάρτηση ύψωσης σε δύναμη

/* File: powers.c */
#include <stdio.h>
#define MAXM 4          /* Maximum base of powers to compute */
#define MAXN 6                           /* Maximum exponent */

int power(int, int);                   /* Prototype of power */

int main(void)
{ int m, n, p;
  for (m=2 ; m <= MAXM ; m++)           /* Start from base 2 */
    for (n=2 ; n <= MAXN ; n++) {   /* Start from exponent 2 */
       p = power(m,n);                /* Call power function */
       printf("%d^%d = %d\n", m, n, p);
    }
  return 0;
}

int power(int base, int n)
{ int p;
  for (p=1 ; n > 0 ; n--)                /* base^n equals to */
    p *= base;         /* base * base * ... * base (n times) */
  return p;                                 /* Return result */
}
% gcc -o powers powers.c
% ./powers
2^2 = 4
2^3 = 8
2^4 = 16
2^5 = 32
2^6 = 64
3^2 = 9
3^3 = 27
3^4 = 81
3^5 = 243
3^6 = 729
4^2 = 16
4^3 = 64
4^4 = 256
4^5 = 1024
4^6 = 4096
%

Συνάρτηση υπολογισμού παραγοντικού

/* File: factorial.c */
#include <stdio.h>
#define MAXN 12             /* Compute factorials up to MAXN */

int factorial(int n)                  /* No prototype needed */
{ int f;
  for (f=1 ; n > 0 ; n--)       /* Initialize factorial to 1 */
    f *= n;        /* Multiply into factorial n, n-1, ..., 1 */
  return f;                                 /* Return result */
}

int main(void)
{ int n;                         /* For all n from 1 to MAXN */
  for (n=1 ; n <= MAXN ; n++)           /* Call function and */
    printf("%2d! = %d\n", n, factorial(n));  /* print result */
  return 0;
}
% gcc -o factorial factorial.c
% ./factorial
 1! = 1
 2! = 2
 3! = 6
 4! = 24
 5! = 120
 6! = 720
 7! = 5040
 8! = 40320
 9! = 362880
10! = 3628800
11! = 39916800
12! = 479001600
%

Εμβέλεια και χρόνος ζωής μεταβλητών

Υπολογισμός παραγοντικού με αναδρομή

/* File: recfact.c */
#include <stdio.h>
#define MAXN 12             /* Compute factorials up to MAXN */

int recfact(int);                    /* Prototype of recfact */

int main(void)
{ int n;                         /* For all n from 0 to MAXN */
  for (n=0 ; n <= MAXN ; n++)           /* Call function and */
    printf("%2d! = %d\n", n, recfact(n));    /* print result */
  return 0;
}

int recfact(int n)
{ if (!n)                                       /* If n == 0 */
    return 1;                                      /* 0! = 1 */
  else
    return n*recfact(n-1);                /* n! = n * (n-1)! */
}
% gcc -o recfact recfact.c
% ./recfact
 0! = 1
 1! = 1
 2! = 2
 3! = 6
 4! = 24
 5! = 120
 6! = 720
 7! = 5040
 8! = 40320
 9! = 362880
10! = 3628800
11! = 39916800
12! = 479001600
%

Μετατροπές μεταξύ δεκαδικών και δυαδικών αριθμών

/* File: convdecbin.c */
#include <stdio.h>
#define ERROR -1       /* Return value for illegal character */

int getinteger(int base)
{ char ch; /* No need to declare ch as int - no EOF handling */
  int val = 0;                    /* Initialize return value */
  while ((ch = getchar()) != '\n')    /* Read up to new line */
    if (ch >= '0' && ch <= '0'+base-1)   /* Legal character? */
      val = base*val + (ch-'0');      /* Update return value */
    else
      return ERROR;                /* Illegal character read */
  return val; /* Everything OK - Return value of number read */
}

int main(void)
{ int n, digs[32], ind = 0;
  printf("Please, give a binary number:  ");
  n = getinteger(2);                   /* Read binary number */
  if (n == ERROR) {
    printf("Sorry, illegal character\n");  return 1; }
  else                      /* Print decimal value of number */
    printf("Decimal equivalent is:         %d\n", n);
  printf("Please, give a decimal number: ");
  n = getinteger(10);                 /* Read decimal number */
  if (n == ERROR) {
    printf("Sorry, illegal character\n");  return 1; }
  else {                  /* Convert number to binary digits */
    while (n) {           /* Repeat until number equals to 0 */
      digs[ind++] = n%2;        /* New digit is number mod 2 */
      n = n/2;                     /* New number is number/2 */
    }
    printf("Binary equivalent is:          ");
    while (ind--)           /* Print digits in reverse order */
      printf("%d", digs[ind]);
    printf("\n");
  }
  return 0;
}
% gcc -o convdecbin convdecbin.c
% ./convdecbin
Please, give a binary number:  1000100101111010001001001
Decimal equivalent is:         18019401
Please, give a decimal number: 18019401
Binary equivalent is:          1000100101111010001001001
% ./convdecbin
Please, give a binary number:  10011101
Decimal equivalent is:         157
Please, give a decimal number: 65535
Binary equivalent is:          1111111111111111
%

Κατεβάστε το κεφάλαιο: PDF · Markdown