I have a matrix dynamically allocated and I want to create another one which is the first matrix but with another copy beside. for example,I have the matrix:
11
22
My new matrix will be:
1 1 1 1
2 2 2 2
How can I concatenate them? This is my code in C:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int **create_matrix(int row, int col)
{
int **matrix = malloc(sizeof (int*)*row);
int i;
for (i = 0; i < row; i++)
{
matrix[i] = malloc(sizeof (int)*col);
}
return matrix;
}
void matrix_input(int **matrix, int row, int col)
{
int i, j;
printf("enter the elements of the matrix:\n");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
scanf("%d", &matrix[i][j]);
}
}
}
int **extend_matrix(int **matrix, int row, int col)
{
int k, j;
int i;
int **extend_matrix = malloc(sizeof (int*)*row);
for (k = 0; k < row + row; k++)
{
extend_matrix[k] = malloc(sizeof (int)*col);
}
extend_matrix = matrix;
extend_matrix = (int**) realloc(extend_matrix, (row + row) * sizeof (int*));
extend_matrix[j] = matrix[j];
for (i = 0; i < row; i++)
{
extend_matrix[k] = matrix[i];
}
}
void print_matrix(int **matrix, int row, int col)
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf(" %d ", matrix[i][j]);
}
printf("\n");
}
}
void print_extend_matrix(int **extend_matrix, int row, int col)
{
int k, j;
for (k = 0; k < row + row; k++)
{
for (j = 0; j < col; j++)
{
printf("%d", extend_matrix[k][j]);
}
printf("\n");
}
}
int main(void)
{
int **matrix;
int **extend_matrix;
int row, col;
printf("enter the number of rows of cols:");
scanf("%i%i", &row, &col);
matrix = create_matrix(row, col);
matrix_input(matrix, row, col);
print_matrix(matrix, row, col);
print_extend_matrix(extend_matrix, row, col);
getch();
return 0;
}
Although #NPE suggested to you a better way. If you want to allocate memory in extend_matrix()
error in your code (read comments)
int **extend_matrix = malloc(sizeof (int*)*row);
^ on row
for (k = 0; k < row + row; k++)
^ where as loop is for row + row
{
extend_matrix[k] = malloc(sizeof (int)*col); // So this cause an error,
// segment-fault
}
second, your concept is wrong to copy memory:
extend_matrix = matrix;
at this line you are assigning matrix to extend_matrix its wrong. you need loop here to copy each elements from matrix[][] to extend_matrix[][]. (but rectify your memory allocation code first)
I think extend_matrix() should just call create_matrix() to create a new matrix of double the width, and then use two simple nested loops to populate it.
If this is what you are looking for :
int concat(void * oldM, int row, int col,void& *newM) {
newM = malloc(sizeof(int)*row*col*2);
for(int i = 0;i<2;++i)
for(int j=0;j<2;++j)
newM[i][j+col] = newM[i][j] = oldM[i][j];
for(int i = 0;i<2;++i) {
for(int j=0;j<4;++j) {
cout<<"\t"<<newM[i][j];
}
cout<<"\n";
}
}
Related
I'm trying to write a code in C that sum two 4x4 matrix.
But I want my function to have a pointer as my arguments. The only error I'm getting is the time I'm trying to sum up in the function. Could someone help me?
#include <stdio.h>
#include <locale.h>
int i = 0, j = 0;
void calc_soma(int* mat_A, int* mat_B, int* mat_C)
{
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
mat_C[i][j] = mat_A[i][j] + mat_B[i][j];
printf("%d", mat_C[i][j]);
}
}
}
int main()
{
setlocale(LC_ALL, "Portuguese");
int i=0, j=0;
int mA[4][4], mB[4][4], mC[4][4];
int *mat_A, *mat_B, *mat_C;
for(i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("Type in the value for Matrix A [%d][%d]: ", i, j);
scanf_s("%d", &mA[i][j]);
}
}
i, j = 0;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("Type in the value for Matrix B [%d][%d]: ", i, j);
scanf_s("%d", &mB[i][j]);
}
}
*mat_A = &mA;
*mat_B = &mB;
return 0;
}
The types of pointers for the arguments are wrong. You want to pass (the pointer to the first elements of) arrays like int mA[4][4];, so they should be pointers to int[4].
void calc_soma(int (*mat_A)[4], int (*mat_B)[4], int (*mat_C)[4])
{
/* same as original */
}
They can simply be written like this:
void calc_soma(int mat_A[][4], int mat_B[][4], int mat_C[][4])
{
/* same as original */
}
Then the function can be called like:
calc_soma(mA, mB, mC);
The purpose of mat_A and mat_B are unclear, but if you want to get pointers to the matrice like &mA, it should be int(*)[4][4]. Note that dereferencing (like *mat_A) uninitialized pointers will invoke undefined behavior.
int main()
{
setlocale(LC_ALL, "Portuguese");
int i=0, j=0;
int mA[4][4], mB[4][4], mC[4][4];
int (*mat_A)[4][4], (*mat_B)[4][4], (*mat_C)[4][4];
/* omit */
mat_A = &mA;
mat_B = &mB;
return 0;
}
To use functions like
void calc_soma(int* mat_A, int* mat_B, int* mat_C)
you should express the matrice by 1D array to match with the format. It will be like this:
#include <stdio.h>
#include <locale.h>
#define ROWS 4
#define COLS 4
int i = 0, j = 0;
void calc_soma(int* mat_A, int* mat_B, int* mat_C)
{
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
mat_C[i * COLS + j] = mat_A[i * COLS + j] + mat_B[i * COLS + j];
printf("%d", mat_C[i * COLS + j]);
}
}
}
int main()
{
setlocale(LC_ALL, "Portuguese");
int i=0, j=0;
int mA[ROWS * COLS], mB[ROWS * COLS], mC[ROWS * COLS];
for(i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
printf("Type in the value for Matrix A [%d][%d]: ", i, j);
scanf_s("%d", &mA[i * COLS + j]);
}
}
i, j = 0;
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
printf("Type in the value for Matrix B [%d][%d]: ", i, j);
scanf_s("%d", &mB[i * COLS + j]);
}
}
calc_soma(mA, mB, mC);
return 0;
}
In the matrix. c I have
struct matrix{
int row;
int col;
int *a;
};
Matrix* allocateMemory(int m, int n) {
Matrix* mat = (Matrix*) malloc(sizeof(Matrix));
if (mat == NULL) {
return NULL;
}
mat->row = m;
mat->col = n;
mat->a = (int*)calloc(m*n, sizeof(int));
return m;
}
While in the matrix.h I have
#ifndef MATRIX_H_INCLUDED
#define MATRIX_H_INCLUDED
typedef struct matrix Matrix;
Matrix* allocateMemory(int m, int n);
//...
int printMatrix(Matrix* mat);
int transpose(Matrix* mat);
#endif // MATRIX_H_INCLUDED
I am working in an ADT for matrices but I stucked in the print function. I would like to create a function
int printMatrix(Matrix* mat)
So in the main.c I would have something like
int i, j;
for (i = 0; i < row; i++){
for(j = 0; j < col; j++){
printf("%d ", printMatrix(mat));
}
printf("\n");
}
It means I want the printf in the main, not in the function, but I just could do this
void printMatrix(Matrix* mat){
int i, j, k;
for (i = 0; i < mat->row; i++){
for(j = 0; j < mat->col; j++){
k = i*mat->col + j;
printf("%d ", mat->a[k]);
}
printf("\n");
}
}
It does print the matrix, but it doesn't seem to be right. It is the same for the transpose matrix function, it does print the transpose matrix correctly, but I would like an
int transpose(Matrix* mat)
So I would use the function printMatrix in the main to print the transpose, but I just could do
void transpose(Matrix* mat){
int i, j, k;
for (i = 0; i < mat->col; i++){
for (j = 0; j < mat->row; j++){
k = j*mat->row + i;
printf("%f ", mat->a[k]);
}
printf("\n");
}
}
How can I create the int function to print the matrix?
I am still studying ADT, but what would be my lack of understanding so I couldn't do the function?
This is prefaced by my top comments.
A few more style tips ...
Do not cast the return of malloc [et. al.]
A bit more idiomatic is (e.g.):
Matrix *mat = malloc(sizeof(*mat));
I realize that in school, they teach the use of (e.g.) i, j, k, but try to use more descriptive names (e.g) row, col, off.
And, make the arguments descriptive as well:
Matrix *allocateMemory(int row,int col)
Here's a refactored version [with some style cleanups]:
#include <stdio.h>
#include <stdlib.h>
typedef struct matrix {
int row;
int col;
int *a;
} Matrix;
Matrix *
allocateMemory(int row, int col)
{
Matrix *mat = malloc(sizeof(*mat));
if (mat == NULL) {
perror("malloc");
exit(1);
}
mat->row = row;
mat->col = col;
mat->a = calloc(row * col, sizeof(*mat->a));
if (mat->a == NULL) {
perror("calloc");
exit(1);
}
return mat;
}
void
printMatrix(Matrix *mat)
{
int row, col, off;
for (row = 0; row < mat->row; row++) {
for (col = 0; col < mat->col; col++) {
off = (row * mat->col) + col;
printf("%d ", mat->a[off]);
}
printf("\n");
}
}
void
matrix_fill(Matrix * mat)
{
int row, col, off;
int val = 1;
for (row = 0; row < mat->row; row++) {
for (col = 0; col < mat->col; col++) {
off = (row * mat->col) + col;
mat->a[off] = val++;
}
}
}
void
transpose_copy(Matrix *matout,Matrix *matin)
{
int row, col;
int inoff, outoff;
for (row = 0; row < matin->row; row++) {
for (col = 0; col < matin->col; col++) {
inoff = (row * matin->col) + col;
outoff = (col * matout->col) + row;
matout->a[outoff] = matin->a[inoff];
}
printf("\n");
}
}
Matrix *
transpose_new(Matrix *matin)
{
Matrix *matout = allocateMemory(matin->col,matin->row);
transpose_copy(matout,matin);
return matout;
}
int
main(void)
{
Matrix *matin = allocateMemory(2,3);
matrix_fill(matin);
printf("Original:\n");
printMatrix(matin);
Matrix *matout = transpose_new(matin);
printf("Transposed:\n");
printMatrix(matout);
return 0;
}
Here's the program output:
Original:
1 2 3
4 5 6
Transposed:
1 4
2 5
3 6
the Question is: to get from the user sodoku board and if there is a solution to print it, if not to print no solution!
the solution of the soduko: two identical numbers mmust not appear on the same line;
two identical numbers must not appear in the same colum.
I worte a program that works perfectly when I put the soduko board and the size (global parametes-as shown un my code) but when I tried to receive from the user it took so much time to run the solution and sometimes it didn't retun anything. I would like to understand why?!
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
int matrix[5][5] = {
{4,2,0,0,5},
{2,0,0,1,3},
{5,0,1,2,0},
{0,0,3,0,2},
{0,0,0,0,0},
};
void print_sudoku()
{
int i,j;
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
{
printf("%d ",matrix[i][j]);
}
printf("\n");
}
}
int number_unassigned(int *row, int *col)
{
int num_unassign = 0;
int i,j;
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
{
if(matrix[i][j] == 0)
{
*row = i;
*col = j;
num_unassign = 1;
return num_unassign;
}
}
}
return num_unassign;
}
int is_safe(int n, int r, int c)
{
int i;
for(i=0;i<SIZE;i++)
{
if(matrix[r][i] == n)
return 0;
}
for(i=0;i<SIZE;i++)
{
if(matrix[i][c] == n)
return 0;
}
return 1;
}
int solve_sudoku()
{
int row;
int col;
if(number_unassigned(&row, &col) == 0)
return 1;
int i;
for(i=1;i<=SIZE;i++)
{
if(is_safe(i, row, col))
{
matrix[row][col] = i;
if(solve_sudoku())
return 1;
matrix[row][col]=0;
}
}
return 0;
}
int main()
{
if (solve_sudoku())
print_sudoku();
else
printf("No solution!\n");
return 0;
}
and this is whe code that I used to ask the user to enter a sodoku board:
int** ReadSoduko(int n) {
int** matrix = (int**) malloc((sizeof(int*)) * n);
for(int i = 0; i < n; ++i) {
matrix[i] = malloc(sizeof(int) * n);
}
printf("\nEnter your soduko board:\n");
for(int i = 0; i < n; ++i) {
printf("Enter row [%d]: ", i);
for(int j = 0; j < n; ++j) {
scanf("%d", &matrix[i][j]);
}
}
return matrix;
}
I suspect your problem is related to one thing: #define SIZE that you are probably forgetting to update when reading dynamically. Since you use SIZE in your loops, if that is not the real size of your matrix, then it probably won't work. I've changed 2 lines and added 3 other (the lines with comments at the end). Try it now.
#include <stdio.h>
#include <stdlib.h>
int SIZE; //changed here
int** matrix; //changed here
void print_sudoku()
{
int i, j;
for (i = 0; i < SIZE; i++)
{
for (j = 0; j < SIZE; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int number_unassigned(int* row, int* col)
{
int num_unassign = 0;
int i, j;
for (i = 0; i < SIZE; i++)
{
for (j = 0; j < SIZE; j++)
{
if (matrix[i][j] == 0)
{
*row = i;
*col = j;
num_unassign = 1;
return num_unassign;
}
}
}
return num_unassign;
}
int is_safe(int n, int r, int c)
{
int i;
for (i = 0; i < SIZE; i++)
{
if (matrix[r][i] == n)
return 0;
}
for (i = 0; i < SIZE; i++)
{
if (matrix[i][c] == n)
return 0;
}
return 1;
}
int solve_sudoku()
{
int row;
int col;
if (number_unassigned(&row, &col) == 0)
return 1;
int i;
for (i = 1; i <= SIZE; i++)
{
if (is_safe(i, row, col))
{
matrix[row][col] = i;
if (solve_sudoku())
return 1;
matrix[row][col] = 0;
}
}
return 0;
}
int** ReadSoduko(int n) {
int** matrix = (int**)malloc((sizeof(int*)) * n);
for (int i = 0; i < n; ++i) {
matrix[i] = (int*) malloc(sizeof(int) * n);
}
printf("\nEnter your soduko board:\n");
for (int i = 0; i < n; ++i) {
printf("Enter row [%d]: ", i);
for (int j = 0; j < n; ++j) {
scanf("%d", &matrix[i][j]);
}
}
return matrix;
}
int main()
{
printf("Size of matrix: "); //added this
scanf("%d", &SIZE); //added this
matrix = ReadSoduko(SIZE); //added this
if (solve_sudoku())
print_sudoku();
else
printf("No solution!\n");
return 0;
}
And don't forget that the matrix that you have declared statically doesn't have a solution! I have tested the same matrix, just replacing the 2 in first line by a 0 and it worked here.
I want to delete a consecutive row in a matrix. For example:
My matrix has 3 rows and 3 columns.
Elements of the matrix would be:
1 2 3
1 2 3
4 5 6
For this example the output should be:
1 2 3
4 5 6
I've tried an implementation but I have some problem, I know that I must use another matrix whose rows must be modified but I don't have any correct results till now.
How can I make it work?
#include <stdio.h>
#include <stdlib.h>
void readMatrix(int matrix[100][100], int noRows, int noCol);
void removeLine(int matrix[100][100], int noRows, int noCol);
void printMatrix(int matrix[100][100], int noRows, int noCol);
int main()
{
int noRows, noCol, matrix[100][100], i, j;
printf("n="); scanf("%d", &noRows);
printf("m="); scanf("%d", &noCol);
readMatrix(matrix, noRows, noCol);
printMatrix(matrix, noRows, noCol);
return 0;
}
void removeLine(int matrix[100][100], int noRows, int noCol)
{
int i, j;
int temp[100][100];
for (i = 0; i < noRows - 1; i++)
for (j = 0; j < noCol; j++)
{
if (matrix[i][j] == temp[i + 1][j])
{
matrix[i][j] == temp[i + 1][j];
}
}
}
void readMatrix(int matrix[100][100], int noRows, int noCol)
{
int i, j;
for (i = 0; i < noRows; i++)
for (j = 0; j < noCol; j++)
{
printf("\na[%d][%d]=", i, j); scanf("%d", &matrix[i][j]);
}
}
void printMatrix(int matrix[100][100], int noRows, int noCol)
{
int i, j;
for (i = 0; i < noRows; i++)
{
for (j = 0; j < noCol; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
You can't remove rows from the 2d array. You can shift rows to the left:
// Helper function to "delete" one row from the array
void deleteRow(int matrix[100][100], int row, int noRows, int noCol)
{
// Copy all rows after row to one row over
for (int i = row; i < noRows - 1; i++) {
// Use memcpy to copy entire row
memcpy(matrix[i], matrix[i+1], sizeof(int) * noCol);
}
}
// Remove repeats
void removeLines(int matrix[100][100], int *noRows, int noCol)
{
for (int i = 0; i < *noRows - 1; i++) {
// Use memcmp to compare entire row
if (memcmp(matrix[i], matrix[i + 1], sizeof(int) * noCol) == 0) {
deleteRow(matrix, i+1, *noRows, noCol);
i -= 1;
*noRows -= 1;
}
}
}
There is room for optimization here - especially when there are more than 1 repeats.
I seem to get input for the first matrix, but when I ask the user to enter input for second matrix, the program crashes..why is this? cant figure it out, I even tried allocating memory, outcome is the same...
#include <stdlib.h>
#include <stdio.h>
#define MAXCOLUMNS 10
// dealing with 2D arrays, passing to function etc
void read_input(int (*a)[MAXCOLUMNS], int n_rows, int n_columns);
void print_sum (int (*a)[MAXCOLUMNS], int (*b)[MAXCOLUMNS], int (*c)[MAXCOLUMNS], int n_rows, int n_columns);
int main() {
int i;
int rows;
int columns;
int (*two_d_array)[MAXCOLUMNS];
int (*two_d_array2)[MAXCOLUMNS];
int (*output)[MAXCOLUMNS];
printf("enter the number of rows\n");
scanf("%d", &rows);
printf("enter the number of columns\n");
scanf("%d", &columns);
printf("enter data into array number 1\n");
read_input(two_d_array, rows, columns);
printf("enter data for 2d array number 2\n");
read_input(two_d_array2, rows, columns);
print_sum(two_d_array, two_d_array2, output, rows, columns);
return 0;
}
void read_input(int (*a)[MAXCOLUMNS], int n_rows, int n_columns) {
int i;
int j;
for (i = 0; i < n_rows; ++i) {
for (j = 0; j < n_columns; ++j) {
printf("enter details for rows number %d and column number %d\n", i + 1, j + 1);
scanf("%d", (*(a+i)+j));
getchar();
}
}
}
void print_sum (int (*a)[MAXCOLUMNS], int (*b)[MAXCOLUMNS], int (*c)[MAXCOLUMNS], int n_rows, int n_columns) {
int i;
int j;
// computing sum
for (i = 0; i < n_rows; i++) {
for (j = 0; j < n_columns; j++) {
*(*(c+i)+j) = *(*(a+i)+j) + *(*(b+i)+j);
}
}
// printing sum
for (i = 0; i < n_rows; i++) {
printf("\n");
for (j = 0; j < n_columns; j++) {
printf("%d\t", *(*(c+i)+j));
}
}
}
for C99
#include <stdlib.h>
#include <stdio.h>
#define MAXCOLUMNS 10
void read_input(int rows, int cols, int a[rows][cols]);
void print_sum (int rows, int cols, int in1[rows][cols], int in2[rows][cols], int out[rows][cols]);
int main(void) {
int i, rows, columns;
printf("enter the number of rows\n");
scanf("%d", &rows);
printf("enter the number of columns\n");
scanf("%d", &columns);
//if(columns > MAXCOLUMNS){ fprintf(stderr, "too large!"); return 1); }
int array1[rows][columns];
int array2[rows][columns];
int array3[rows][columns];
printf("enter data into array number 1\n");
read_input(rows, columns, array1);
printf("enter data for 2d array number 2\n");
read_input(rows, columns, array2);
print_sum(rows, columns, array1, array2, array3);
return 0;
}
void read_input(int rows, int cols, int a[rows][cols]){
int i, j;
for (i = 0; i < rows; ++i) {
for (j = 0; j < cols; ++j) {
printf("enter details for rows number %d and column number %d\n", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
}
}
void print_sum (int rows, int cols, int a[rows][cols], int b[rows][cols], int c[rows][cols]){
int i, j;
for (i = 0; i < rows; i++){
printf("\n");,,
for (j = 0; j < cols; j++){
c[i][j] = a[i][j] + b[i][j];
printf("%d\t", c[i][j]);
}
}
}
for int ** (2D_ARRAY)
#include <stdio.h>
#include <stdlib.h>
int main(void){
int rows = 3;
int cols = 5;
int **array;
int r, c;
//allocate
array = malloc(rows * sizeof(int*));
for(r = 0; r < rows ; ++r){
array[r] = malloc(cols * sizeof(int));
}
//set
for(r = 0; r < rows ; ++r){
for(c = 0; c < cols ; ++c){
array[r][c] = r * 10 + c;
}
}
//print
for(r = 0; r < rows ; ++r){
for(c = 0; c < cols ; ++c){
printf("%02d ", array[r][c]);
}
printf("\n");
}
}
for int (**)[SIZE]
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int main(void){
int rows = 3;
int cols = 5;
int (**array)[MAX];
int r, c;
//allocate
array = malloc(rows * sizeof(int (*)[MAX]));
for(r = 0; r < rows ; ++r){
array[r] = malloc(sizeof(int[MAX]));
}
//set
for(r = 0; r < rows ; ++r){
for(c = 0; c < cols ; ++c){
(*array[r])[c] = r * 10 + c;
}
}
//print
for(r = 0; r < rows ; ++r){
for(c = 0; c < cols ; ++c){
printf("%02d ", (*array[r])[c]);
}
printf("\n");
}
}
for int (*)[SIZE]
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int main(void){
int rows = 3;
int cols = 5;
int (*array)[MAX];
int r, c;
//allocate
array = malloc(rows * sizeof(int[MAX]));
//set
for(r = 0; r < rows ; ++r){
for(c = 0; c < cols ; ++c){
array[r][c] = r * 10 + c;
}
}
//print
for(r = 0; r < rows ; ++r){
for(c = 0; c < cols ; ++c){
printf("%02d ", array[r][c]);
}
printf("\n");
}
}