Advertisement
PaweU

kolos poprawiony

Jan 15th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int f(int n,int k)
  5. {
  6.    if(n==k || n==0)
  7.       return 2;
  8.     if(k==0)
  9.         return f(n-1,k);
  10.    return f(n-1,k-1)*f(n-1,k);
  11. }
  12.  
  13. int main(void) {
  14.  
  15.     int n, i, j;
  16.     int x, y;
  17.     int liczba;
  18.    
  19.     printf("Podaj liczbe (n): ");
  20.     scanf("%d", &n);
  21.    
  22.     for (j=0; j<n; j++) {
  23.         for (i=0; i<=j; i++) {
  24.             liczba = f(j, i);
  25.             printf ("%3d ", liczba);
  26.         }
  27.         printf ("\n");
  28.     }
  29.    
  30.  
  31.  
  32.    
  33.     // druga czesc zadania
  34.  
  35.     int *tab[n];
  36.  
  37.     for (i=1; i<=n; i++) {
  38.         tab[i-1] = malloc( i * sizeof (int));
  39.     }
  40.  
  41.     for (j=0; j<n; j++) {
  42.         for (i=0; i<=j; i++) {
  43.             tab[j][i] = 0;
  44.             if (i == 0) {
  45.                 tab[j][i] = 2;
  46.             }
  47.             else if (i == j) {
  48.                 tab [j][i] = tab[j-1][i-1];
  49.             }
  50.             else {
  51.             tab[j][i] = tab[j-1][i] * tab[j-1][i-1];
  52.             }
  53.             printf ("%3d ", tab[j][i]);
  54.         }
  55.         printf ("\n");
  56.     }
  57.    
  58.     do {
  59.         printf ("Podaj numer wiersza: ");
  60.         scanf ("%d", &x);
  61.         if (x>n || x<=0) printf ("Niepoprawny wpis.\n");
  62.     } while (x>n || x<=0);
  63.  
  64.     do {
  65.         printf ("Podaj numer elementu w tym wierszu: ");
  66.         scanf ("%d", &y);
  67.         if (y>x || y<=0) printf ("Niepoprawny wpis.\n");
  68.     } while (y>x || y<=0);
  69.  
  70.     printf ("\nTen element to: %d.\n", tab[x-1][y-1]);
  71.  
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement