Advertisement
green1ant

32-1

Jan 20th, 2019
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int operation(int *arr, int length, int counter){
  5.    if(length == 0){
  6.       if(counter < 3){
  7.          printf("Less than 3");
  8.       }
  9.       return 0;
  10.  
  11.    }
  12.    else{
  13.       /*
  14.       printf("Print for %d:\n", length);
  15.       for (int i = 0; i < length; i++){
  16.          for (int j = 0; j < length; j++){
  17.             printf("%5d ", *(arr + i*length + j));
  18.          }
  19.          printf("\n");
  20.       }
  21.       */
  22.       int sum = 0;
  23.       int *one;
  24.       one = (int*)malloc((length - 1) * sizeof(int));
  25.       for (int i = 0; i < length - 1; i++){
  26.           *(one + i) = *(arr + i + 1);
  27.       }
  28.  
  29.       for (int i = 0; i < length - 1; i++){
  30.          printf("%5d! ", *(one + i));
  31.          if(*(one + i) % 2 != 0){
  32.             sum += *(one + i);
  33.             counter++;
  34.          }
  35.  
  36.          if(counter == 3){
  37.             return sum;
  38.          }
  39.       }
  40.       printf("\n");
  41.  
  42.       int *newarr;
  43.       newarr = (int*)malloc((length - 1) * (length - 1) * sizeof(int));
  44.       for (int i = 0; i < length - 1; i++){
  45.          for (int j = 0; j < length - 1; j++){
  46.              *(newarr + i*(length - 1) + j) = *(arr + (i + 1)*(length) + j + 1);
  47.          }
  48.       }
  49.  
  50.       if(counter < 3){
  51.          return sum + operation(newarr, length - 1, counter);
  52.       }
  53.    }
  54. }
  55.  
  56.  
  57. int main()
  58. {
  59.     printf("Start\n");
  60.  
  61.     int *initial;
  62.     int n;
  63.  
  64.     printf("Enter n: ");
  65.     scanf("%d", &n);
  66.  
  67.     initial = (int*)malloc(n * n * sizeof(int));
  68.  
  69.     for (int i = 0; i < n; i++){
  70.         for (int j = 0; j< n; j++){
  71.             printf("arr[%d][%d] = ", i, j);
  72.             scanf("%d", (initial + i*n + j));
  73.         }
  74.  
  75.     }
  76.  
  77.     int counter = 0;
  78.     int ans = operation(initial, n, counter);
  79.     printf("\n");
  80.     printf("Sum = %d", ans);
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement