Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <malloc.h>
  3.  
  4.  
  5.  int *getMax(int **mass, int N){
  6.     int* maxArr = (int*)malloc(N*sizeof(int));
  7.     for (int j = 0; j < N ; j++ ){
  8.         int Max = mass[0][j];
  9.         for (int i = 1; i < N; i++ ){
  10.             if (mass[i][j] > Max){
  11.                 Max = mass[i][j];
  12.             }
  13.         }
  14.         maxArr[j] = Max;
  15.     }
  16.     return maxArr;
  17. }
  18.  
  19.  int *getMin( int **mass, int N){
  20.         int* minArr = (int*)malloc(N*sizeof(int));
  21.         for (int i = 0; i < N; i++ ){
  22.             int Min = mass[i][0];
  23.             for (int j = 1; j < N; j++ ){
  24.                 if (mass[i][j] < Min){
  25.                     Min = mass[i][ j];
  26.                 }
  27.             }
  28.             minArr[i] = Min;
  29.         }
  30.         return minArr;
  31. }
  32.  
  33.  void getPoint(int **mass, int *minArr, int *maxArr, int N){
  34.     FILE* out = fopen("D:\\Labs\\labs 2-3\\result C\\result.txt", "w+");
  35.     int res = 0;
  36.     for(int i = 0; i < N; i++ ){
  37.         for(int j = 0; j < N; j++ ){
  38.             if ((mass[i][j] == minArr[i]) && (mass[i][j] == maxArr[j])){
  39.                 fprintf(out,"The saddle point of this matrix is the number %d in %d-th line %d-th column.\n", (mass[i][j]), (i + 1), (j + 1));
  40.                 res += 1;
  41.             }
  42.         }
  43.     }
  44.     if (res == 0){
  45.         fprintf(out,"There are no saddle points in this matrix.");
  46.     }
  47.     fclose(out);
  48. }
  49.  
  50.  int** getMatrix(){
  51.     int arrLength;
  52.     FILE* in = fopen("D:\\Labs\\labs 2-3\\text\\task.txt", "r+");
  53.     fscanf(in,"%d", &arrLength);
  54.     int** mass = (int**)malloc(arrLength*sizeof(int*));
  55.     for (int i = 0; i < arrLength; i++){
  56.         mass[i] = (int*)malloc(arrLength* sizeof(int));
  57.         for (int j = 0; j < arrLength; j++) {
  58.             fscanf(in, "%d", &mass[i][j]);
  59.         }
  60.     }
  61.     fclose(in);
  62.     return mass;
  63. }
  64.  
  65.  int arrLength(){
  66.     int arrLength;
  67.     FILE* in = fopen("D:\\Labs\\labs 2-3\\text\\task.txt", "rt");
  68.     fscanf(in,"%d", &arrLength);
  69.     fclose(in);
  70.     int Length = arrLength;
  71.     return Length;
  72. }
  73.  
  74.  void Main() {
  75.      int Num = arrLength();
  76.      int **Mass = getMatrix();
  77.      getPoint(Mass, getMin(Mass, Num), getMax(Mass, Num), Num);
  78. }
  79.  
  80.  
  81. int main() {
  82.     Main();
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement