Advertisement
AdrianMadajewski

Untitled

Jun 1st, 2020
1,271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int getUserInput(int *input, int INPUT_MIN, int INPUT_MAX) {
  5.     int result = scanf_s("%d", input);
  6.     if (result == 1 && *input >= INPUT_MIN && *input <= INPUT_MAX) {
  7.         return 0;
  8.     }
  9.  
  10.     return -1;
  11. }
  12.  
  13. int main(int argc, char *argv[])
  14. {
  15.     // Constants
  16.     const int MIN_VALUE = 1;
  17.     const int MAX_VALUE = 15;
  18.  
  19.     int n;
  20.     int m;
  21.  
  22.     printf("Podaj n: ");
  23.     if (getUserInput(&n, MIN_VALUE, MAX_VALUE) == -1) {
  24.         printf("Podano zle dane - uruchom ponownie");
  25.         return -1;
  26.     }
  27.  
  28.     printf("Podaj m: ");
  29.     if (getUserInput(&n, MIN_VALUE, MAX_VALUE) == -1) {
  30.         printf("Podano zle dane - uruchom ponownie");
  31.         return -1;
  32.     }
  33.  
  34.     // Allocate memory and make each A[i, j]
  35.     int **A;
  36.     A = (int**)malloc(n * sizeof *A);
  37.     for(int row = 0; row < n; ++row) {
  38.         A[row] = (int*)malloc(m * sizeof *A[row]);
  39.         for(int col = 0; col < m; ++col) {
  40.  
  41.             printf_s("A[%d][%d] = ", row, col);
  42.  
  43.             // Provided bad input
  44.             if (scanf_s("%d", &A[row][col]) != 1) {
  45.                 printf("Podano - zle dane - uruchom ponownie");
  46.  
  47.                 // Free memory (error occured on row-th itteration)
  48.                 for (int f = 0; f < row; ++f) {
  49.                     free(A[f]);
  50.                 }
  51.                 free(A);
  52.                
  53.                 // Quit main with ERROR_CODE -1
  54.                 return -1;
  55.             }
  56.         }
  57.     }
  58.  
  59.     // Allocate memory and make each B[i, j]
  60.     int** B;
  61.     B = (int**)malloc(n * sizeof * B);
  62.     for (int row = 0; row < n; ++row) {
  63.         B[row] = (int*)malloc(m * sizeof * B[row]);
  64.         for (int col = 0; col < m; ++col) {
  65.  
  66.             printf_s("B[%d][%d] = ", row, col);
  67.  
  68.             // Provided bad input
  69.             if (scanf_s("%d", &A[row][col]) != 1) {
  70.                 printf("Podano - zle dane - uruchom ponownie");
  71.  
  72.                 // Free memory (error occured on row-th itteration)
  73.                 for (int f = 0; f < row; ++f) {
  74.                     free(B[f]);
  75.                 }
  76.                 free(B);
  77.  
  78.                 // Quit main with ERROR_CODE -1
  79.                 return -1;
  80.             }
  81.         }
  82.     }
  83.    
  84.    
  85.     // Free memory
  86.     for(int i = 0; i < n; ++i) {
  87.         free(A[i]);
  88.         free(B[i]);
  89.     }
  90.     free(A);
  91.     free(B);
  92.  
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement