Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- int InputN() {
- int n = -1;
- while ((n < 2) | (n > 15)) {
- printf("Введите размер[2..15] матрицы n*n: ");
- scanf("%d", &n);
- if ((n < 2) || (n > 15)) {
- printf("Ошибка!\n");
- }
- }
- return n;
- }
- int CorrectElement(int i, int j) {
- int n = -100;
- while ((n < -99) || (n > 99)) {
- printf("[%d][%d] = ", i, j);
- scanf("%d", &n);
- }
- return n;
- }
- int* InputMatrix(int n) {
- int* arr = NULL;
- arr = (int*)malloc(sizeof(int) * n * n);
- for (int i = 0; i < n; i++) {
- printf("\n");
- for (int j = 0; j < n; j++) {
- arr[i * n + j] = CorrectElement(i, j);
- }
- }
- return arr;
- }
- void PrintMatrix(int* arr, int n) {
- for (int i = 0; i < n; i++) {
- printf("\n");
- for (int j = 0; j < n; j++) {
- printf("%3d", *(arr + i * n + j));
- }
- }
- }
- int* ChangeMatrix(int* arr, int n) {
- int temp;
- for (int i = 0; i < n; i++) {
- temp = *(arr + i * n + 0);
- *(arr + i * n + 0) = *(arr + i * n + i);
- *(arr + i * n + i) = temp;
- }
- return arr;
- }
- int main() {
- int n;
- int* arr;
- n = InputN();
- arr = InputMatrix(n);
- printf("\nИсходная матрица: ");
- PrintMatrix(arr, n);
- arr = ChangeMatrix(arr, n);
- printf("\n");
- printf("\nИзменённая матрица: ");
- PrintMatrix(arr, n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement