E có đoạn code sau dùng để thêm dòng/cột vào ma trận cho trước:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void FillOut(int **, int, int); // File out matrix automatically
void Output(int **, int, int); // Show matrix on screen
void SwapRows(int *, int *, int); // Swap two rows
void ValueSwap(int *, int *); // Swap two values
void AddRows(int ***, int *, int, int *, int); // Insert a row in the matrix
void AddColumns(int ***, int, int *, int *, int); // Insert a column in the matrix
void FillOut(int **arr, int rows, int columns)
{
for (int i = 0; i < rows * columns; ++i) {
*(*(arr + i / columns) + i % columns) = 1 + rand() % 100;
}
}
void Output(int **arr, int rows, int columns)
{
for (int i = 0; i < rows * columns; ++i) {
printf("%5d", *(*(arr + i / columns) + i % columns));
if ((i + 1) % columns == 0)
printf("\n");
}
}
void SwapRows(int *arr1, int *arr2, int size)
{
for (int i = 0; i < size; ++i) {
int Temp = arr1[i];
arr1[i] = arr2[i];
arr2[i] = Temp;
}
}
void ValueSwap(int *a, int *b)
{
int Temp = *a;
*a = *b;
*b = Temp;
}
void AddRows(int ***arr, int *rows, int columns, int *insert_array, int rows_index)
{
*arr = (int **)realloc(*arr, (*rows + 1) * sizeof(int *));
(*arr)[*rows] = (int *)calloc(columns, sizeof(int));
for (int i = 0; i < columns; ++i)
*(*(*arr + *rows) + i) = *(insert_array + i);
for (int i = *rows; i > rows_index; --i) {
SwapRows((*arr)[i], (*arr)[i - 1], columns);
}
++(*rows);
}
void AddColumns(int ***arr, int rows, int *columns, int *insert_array, int columns_index)
{
for (int i = 0; i < rows; ++i) {
(*arr)[i] = (int *)realloc((*arr)[i], (*columns + 1) * sizeof(int));
(*arr)[*columns] = insert_array[i];
}
for (int i = 0; i < rows; ++i) {
for (int j = *columns; j > columns_index; --j) {
ValueSwap(&(*arr)[i][j], &(*arr)[i][j - 1]);
}
}
++(*columns);
}
int main()
{
srand(time(NULL));
int rows, columns;
printf("Type the number of row and column: ");
scanf("%d %d", &rows, &columns);
// allocate 2D array (level 2-pointer)
int **arr = (int **)calloc(rows, sizeof(int *));
for (int i = 0; i < rows; ++i) {
arr[i] = (int *)calloc(columns, sizeof(int));
}
printf("Current matrix:\n");
FillOut(arr, rows, columns);
Output(arr, rows, columns);
int *insert_array = (int *)calloc(columns, sizeof(int)); // This array contains "rows" integer elements to add to the matrix
for (int i = 0; i < columns; ++i)
*(insert_array + i) = 1 + rand() % 100;
int rows_index; // Denoting the position of row to add
// Checking if row index is not valid
do {
printf("Type the number of row's index: ");
scanf("%d", &rows_index);
if (rows_index > rows)
printf("Row's index is invalid. Please check again!\n");
} while (rows_index > rows);
AddRows(&arr, &rows, columns, insert_array, rows_index);
printf("New matrix:\n");
Output(arr, rows, columns);
int *insert_array2 = (int *)calloc(rows, sizeof(int)); // This array contains "columns" integer elements to add to the matrix
for (int i = 0; i < rows; ++i) {
*(insert_array2 + i) = 1 + rand() % 100;
}
int columns_index; // Denoting the position of column to add
// Checking if column index is not valid
do {
printf("Type the number of column's index: ");
scanf("%d", &columns_index);
if (columns_index > columns)
printf("Column'index is invalid. Please check again!\n");
} while (columns_index > columns);
AddColumns(&arr, rows, &columns, insert_array2, columns_index);
printf("New matrix:\n");
Output(arr, rows, columns);
// Free pointer
free(insert_array);
free(insert_array2);
for (int i = 0; i < rows; ++i) {
free(arr[i]);
}
free(arr);
return 0;
}
Khi thêm 1 cột vào thì vẫn được nhưng các giá trị của cột đó đều bằng 0 hết !!!
Ai tìm giúp e cái lỗi với ạ, em cảm ơn nhiều