Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int getUserInput(int *input, int INPUT_MIN, int INPUT_MAX) {
- int result = scanf_s("%d", input);
- if (result == 1 && *input >= INPUT_MIN && *input <= INPUT_MAX) {
- return 0;
- }
- return -1;
- }
- int main(int argc, char *argv[])
- {
- // Constants
- const int MIN_VALUE = 1;
- const int MAX_VALUE = 15;
- int n;
- int m;
- printf("Podaj n: ");
- if (getUserInput(&n, MIN_VALUE, MAX_VALUE) == -1) {
- printf("Podano zle dane - uruchom ponownie");
- return -1;
- }
- printf("Podaj m: ");
- if (getUserInput(&n, MIN_VALUE, MAX_VALUE) == -1) {
- printf("Podano zle dane - uruchom ponownie");
- return -1;
- }
- // Allocate memory and make each A[i, j]
- int **A;
- A = (int**)malloc(n * sizeof *A);
- for(int row = 0; row < n; ++row) {
- A[row] = (int*)malloc(m * sizeof *A[row]);
- for(int col = 0; col < m; ++col) {
- printf_s("A[%d][%d] = ", row, col);
- // Provided bad input
- if (scanf_s("%d", &A[row][col]) != 1) {
- printf("Podano - zle dane - uruchom ponownie");
- // Free memory (error occured on row-th itteration)
- for (int f = 0; f < row; ++f) {
- free(A[f]);
- }
- free(A);
- // Quit main with ERROR_CODE -1
- return -1;
- }
- }
- }
- // Allocate memory and make each B[i, j]
- int** B;
- B = (int**)malloc(n * sizeof * B);
- for (int row = 0; row < n; ++row) {
- B[row] = (int*)malloc(m * sizeof * B[row]);
- for (int col = 0; col < m; ++col) {
- printf_s("B[%d][%d] = ", row, col);
- // Provided bad input
- if (scanf_s("%d", &A[row][col]) != 1) {
- printf("Podano - zle dane - uruchom ponownie");
- // Free memory (error occured on row-th itteration)
- for (int f = 0; f < row; ++f) {
- free(B[f]);
- }
- free(B);
- // Quit main with ERROR_CODE -1
- return -1;
- }
- }
- }
- // Free memory
- for(int i = 0; i < n; ++i) {
- free(A[i]);
- free(B[i]);
- }
- free(A);
- free(B);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement