Advertisement
Guest User

Untitled

a guest
May 19th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.54 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int dd_test(const int *ptr, int width, int height){
  4.     int diag, rest;
  5.     int i, k = 0;
  6.     int temp = 0;
  7.     int counter = 0;
  8.     int watcher = width - 1;
  9.    
  10.     int size = width * height;
  11.    
  12.     if(width <= 0 || height <= 0 || width != height || ptr == NULL) return -1;
  13.    
  14.        
  15.    
  16.    
  17.     for(i = 0; i < size; i++){
  18.            
  19.             if(i == 0){
  20.                 if(*(ptr+i) < 0)    {temp = *(ptr+i); temp *= (-1); diag += temp; k++;}
  21.                 else                {diag += *(ptr+i); k++;}
  22.             }
  23.             else if(i == (width+1) * k){
  24.                 if(*(ptr+i) < 0)    {temp = *(ptr+i); temp *= (-1); diag += temp; k++;}
  25.                 else                {diag += *(ptr+i); k++;}
  26.             }
  27.             else if(*(ptr+i) < 0)   {temp = *(ptr+i); temp *= (-1); rest += temp;}
  28.             else                    rest += *(ptr+i);
  29.            
  30.  
  31.             if(i == watcher) {
  32.                 if(diag > rest) counter++;
  33.                 else return 0;
  34.                 watcher += width;
  35.                 diag = 0;
  36.                 rest = 0;
  37.    
  38.             }
  39.     }
  40.    
  41.  
  42.     if(counter == height) return 1;
  43.     return 0;
  44. }
  45.  
  46.  
  47. int main(){
  48.    
  49.     int macierz2D[100][100] = {0};
  50.     int height = 5;
  51.     int width = 5;
  52.     int size = height * width;
  53.     int funkcja;
  54.    
  55.    
  56.                 int k;
  57.                 int temp = 0;
  58.                 int check;
  59.  
  60.     int *tablica = 0;          
  61.     tablica = *macierz2D;
  62.    
  63.     printf("Podaj liczby: \n");
  64.    
  65.    
  66.    
  67.         for(k = 0; k < size; k++){
  68.                 check = scanf("%d", &temp);
  69.                 if(check != 1) {  printf("Incorrect input"); return 1;  }
  70.                 else *(tablica+k) = temp;
  71.             }
  72.        
  73.         funkcja = dd_test(tablica, width, height);
  74.         if     (funkcja == 1)   printf("YES");
  75.         else if(funkcja == 0)   printf("NO");
  76.         else if(funkcja == -1)  printf("Incorrect input");
  77.        
  78.         return 0;
  79.        
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement