Suppose in Gauss Elimination Method the first value of matrix, A[0][0]=0
Then how can I swap row 1 of matrix "A" with row 2, so that i get the correct result ?
Assuming a simpel 2d-array build the way as shown below: just exchange the rows.
#include <stdio.h>
#include <stdlib.h>
#define S_MATRIX_DIM 5
int main()
{
int **A;
int *tmp;
int i, j, entry;
entry = 0;
A = malloc(S_MATRIX_DIM * sizeof(int *));
for (i = 0; i < S_MATRIX_DIM; i++) {
A[i] = malloc(S_MATRIX_DIM * sizeof(int));
for (j = 0; j < S_MATRIX_DIM; j++) {
A[i][j] = entry++;
}
}
puts("Matrix A =");
for (i = 0; i < S_MATRIX_DIM; i++) {
for (j = 0; j < S_MATRIX_DIM; j++) {
printf("%d,", A[i][j]);
}
putchar('\n');
}
tmp = A[0];
A[0] = A[1];
A[1] = tmp;
puts("Matrix A, row exchanged =");
for (i = 0; i < S_MATRIX_DIM; i++) {
for (j = 0; j < S_MATRIX_DIM; j++) {
printf("%d,", A[i][j]);
}
putchar('\n');
}
for (i = 0; i < S_MATRIX_DIM; i++) {
free(A[i]);
}
free(A);
exit(EXIT_SUCCESS);
}
keep a temp value from the same type;
swap value by value,
example:
temp=a[i][j];
a[i][j]=a[i+1][j];
a[i+1][j]=temp;
Related
I need to multiply two square matrixes A and B 15x15.
Unfortunately, I'm getting this kind of error.
I know the problem is in pointers while calculating matrix C.
C[i][j] += *(A + k) * *(B + k)
I hope you can explain me what's wrong. I'm a beginner xD.
Thank you in advance.
#include <stdio.h>
#define N 15
#define _CRT_SECURE_NO_WARNINGS
int main() {
int A[N][N];
int B[N][N];
int C[N][N];
printf("Input matrix A.\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("Enter your element:\n");
scanf_s("%d", &A[i][j]);
}
printf("\n");
}
printf("Input matrix B.\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("Enter your element:\n");
scanf_s("%d", &B[i][j]);
}
printf("\n");
}
printf("Matrix A.\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d\t", A[i][j]);
}
printf("\n");
}
printf("Matrix B.\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d\t", B[i][j]);
}
printf("\n");
}
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
C[i][j] = 0;
for (int k = 0; k < 14; k++) {
C[i][j] += *(A + k) * *(B + k);
k++;
}
}
}
printf("Your result:\n");
printf("Matrix C.\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d\t", C[i][j]);
}
printf("\n");
}
return 0;
}
The problem in the multiplication is that A+k and B+k have type int (*)[15] which means dereferencing it once only makes a pointer out of them; furthermore, you need to take row and column items individually, which means A[i][k] and B[k][j], right? (also, there's no point on using confusing syntax, as the underlying operation is exactly the same).
Here's a fixed and improved version:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define N 15
/* Improvement 1 (type abstraction) */
typedef int NxN_int_matrix[N][N];
/* Improvement 2 (input function & wrapper) */
#define input_matrix(var) input_matrix_ex((var), #var)
static void input_matrix_ex(NxN_int_matrix dst, char *name)
{
printf("Input matrix %s.\n", name);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
/* Improvement 3 (nicer prompt) */
printf("%s[%2d][%2d]: ", name, i, j);
fflush(stdout);
scanf_s("%d", &dst[i][j]);
}
}
printf("\n");
}
/* Improvement 4 (print function) */
#define print_matrix(var) print_matrix_ex(#var, (var))
static void print_matrix_ex(char *name, NxN_int_matrix M)
{
printf("Matrix %s.\n", name);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d\t", M[i][j]);
}
printf("\n");
}
}
/* Improvement 5 (move multiplication to a function too, and fix it) */
static void mult_matrix(NxN_int_matrix dst, NxN_int_matrix a, NxN_int_matrix b)
{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
/* Improvement 6 (don't write out intermediate values) */
int tmp = 0;
for (int k = 0; k < N; k++)
tmp += a[i][k] * b[k][j];
dst[i][j] = tmp;
}
}
}
int main()
{
NxN_int_matrix A, B, C;
input_matrix(A);
input_matrix(B);
print_matrix(A);
print_matrix(B);
mult_matrix(C, A, B);
printf("Your result:\n");
print_matrix(C);
return 0;
}
/* Possible further improvements:
* - using a transposed B might make multiplication faster
*/
Write a program which will accept 2-dimensional square matrix and find out the transpose of it.
Program should not make use of another matrix
Hi I am trying to transpose a 2*2 matrix without using another matrix.
Is there anything wrong with my transpose logic?
I am a newbie
#include <stdio.h>
int main()
{
int mat[2][2];
int i, j, temp;
for (i = 0; i < 2; i++) {
printf("\nEnter elements of %d row of first matrix: ", i + 1); //i+1 so that it can print 1 row, 2 row, 3 row etc
for (j = 0; j < 2; j++) { //loop inside to loop to get value for a[0][0],a[0][1],a[0][2]
scanf("%d", &mat[i][j]);
}
}
printf("The matrix\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
//transpose logic using same matrix
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
}
printf("The transpose of the matrix is\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
}
EDIT: I found an easier way to do it however I still don't understand why my transpose logic by using this
temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
cannot get it to transpose.
Below is my corrected answer
#include <stdio.h>
int main()
{
int mat[2][2];
int i, j, temp;
for (i = 0; i < 2; i++) {
printf("\nEnter elements of %d row of first matrix: ", i + 1);//i+1 so that it can print 1 row, 2 row, 3 row etc
for (j = 0; j < 2; j++) {//loop inside to loop to get value for a[0][0],a[0][1],a[0][2]
scanf("%d", &mat[i][j]);
}
}
printf("The matrix\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
printf("The transpose of the matrix is\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[j][i]);
}
printf("\n");
}
}
Your program is a good attempt, but transposing the matrix is like reversing an array: you must stop half way to avoid swapping the transposed values twice, leading to the original matrix as you observe.
You should stop the inner loop when j == i, hence change the inner loop to:
for (j = 0; j < i; j++) { // j < i instead of j < 2
Here is a modified version:
#include <stdio.h>
int main() {
int mat[2][2];
int i, j, temp;
for (i = 0; i < 2; i++) {
printf("\nEnter elements of %d row of first matrix: ", i + 1);
for (j = 0; j < 2; j++) {
if (scanf("%d", &mat[i][j]) != 1)
return 1;
}
}
printf("The matrix:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
//transpose logic using same matrix
for (i = 0; i < 2; i++) {
for (j = 0; j < i; j++) {
temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
}
printf("The transpose of the matrix is:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
return 0;
}
Your corrected answer does not transpose the matrix at all, it merely outputs the transposed matrix. The matrix mat in memory is unchanged.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(){
int N;
scanf("%d", &N);
int **arr;
arr = (int**)malloc(sizeof(int) * N);
for(int i = 0; i < N; i++){
arr[i] = (int*)malloc(sizeof(int) * N);
}
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
if(i == 0 || j == 0)
arr[i][j] = 1;
else
arr[i][j] = arr[i - 1][j] + arr[i][j - 1];
}
}
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
printf("%d ", arr[i][j]);
}
printf("\n");
}
for(int i = 0; i < N; i++)
free(arr[i]);
free(arr);
return 0;
}
This code makes a 2-dimensional array with a pointer; I want assign value arr[i][j] = 1. However, an error EXC_BAD_ACCESS occurs in XCode on a Mac. I don't know how to solve the problem. What is my mistake? The assigning for loop is where the trouble occurs.
I have taken two 2d arrays but the output is very different from expected.It should merge two 2d arrays into one 1d array.I want to merge two arrays.Not add.Everywhere the information and examples are about merging two 1D arrays.Please help someone.I don't know where I am wrong.
#include <stdio.h>
void print(int a[][3],int m);
int main()
{
int array1[3][3];
int array2[3][3];
int arraySum[6][3];
int k = 0; //put into array;
int l = 10;
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
array1[i][j] = ++k; //fill from 1 to 10
array2[i][j] = ++l; //fill from 11 - 19
}
}
/*merge arrays*/
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 3; j++)
{
(i < 3) ? arraySum[i][j] = array2[i][j] : arraySum[i][j] = array1[i-3][j];
//fill arraySum with array2 and append with array1.
//just so that arraySum does not have any order
}
}
printf("Arrays before sorting");
printf("Array 1: ");
print(array1,3);
printf("Array2: ");
print(array2,3);
printf("arraySum");
print(arraySum,6);
/* bubble sort*/
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 3; j++)
{
for(int k = i+1; k < 6; k++)
{
for(int m = 0; m < 3; m++)
{
if(arraySum[i][j] > arraySum[k][m])
{
//swap
int temp = arraySum[i][j];
arraySum[i][j] = arraySum[k][m];
arraySum[k][m] = temp;
}
}
}
}
}
printf("\n\n Merged Array after sorting");
print(arraySum,6);
return 0;
}
void print(int a[][3],int m)
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < 3; j++)
{
printf("%d" , a[i][j]);
}
}
}
I want to merge two arrays.Not add.Everywhere the information and examples are about merging two 1D arrays.Please help someone.I don't know where I am wrong.
Please try this code,To Merge two 2d arrays into one 1d array in C
#include <stdio.h>
void print(int a[][3],int m);
int main()
{
int array1[3][3];
int array2[3][3];
int arraySum[6][3];
int k = 0; //put into array;
int l = 10;
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
array1[i][j] = ++k; //fill from 1 to 10
array2[i][j] = ++l; //fill from 11 - 19
}
}
/*merge arrays*/
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 3; j++)
{
(i < 3) ? (arraySum[i][j] = array2[i][j]) : (arraySum[i][j] = array1[i-3][j]);
//fill arraySum with array2 and append with array1.
//just so that arraySum does not have any order
}
}
printf("Arrays before sorting");
printf("Array 1: ");
print(array1,3);
printf("Array2: ");
print(array2,3);
printf("arraySum");
print(arraySum,6);
/* bubble sort*/
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 3; j++)
{
for(int k = i+1; k < 6; k++)
{
for(int m = 0; m < 3; m++)
{
if(arraySum[i][j] > arraySum[k][m])
{
//swap
int temp = arraySum[i][j];
arraySum[i][j] = arraySum[k][m];
arraySum[k][m] = temp;
}
}
}
}
}
printf("\n\n Merged Array after sorting");
print(arraySum,6);
return 0;
}
void print(int a[][3],int m)
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < 3; j++)
{
printf("%d" , a[i][j]);
}
}
}
I hope this code will be usefull.
Thank you.
I believe you're trying to be too smart with ternary operator, you can do it simpler way:
if (i < 3)
arraySum[i][j] = array2[i][j];
else
arraySum[i][j] = array1[i-3][j];
Listen to your compiler it would've told you what was wrong if you've compiled with -Wall -Wextra.
And if you insist on using ternary then this would probably be clearer:
arraySum[i][j] = (i < 3) ? array2[i][j] : array1[i-3][j];
I have to write a program which multiply matrix using pointers.
My program does it correctly, but there are two problems:
1) I can do only one multiplication - when I want next one there is error
2) when I input data of first and second matrix i have to put one more
char to make my program work.
What should I change in my code to solve those problems?
#include <stdio.h>
#include <stdlib.h>
while (z--) { // the number of sets
scanf ("%d %d", &n1, &m1); // first matrix n1 x m1
int **A = malloc(n1 * sizeof(int*));
for (i = 0; i < n1; i++) {
*(A+i) = malloc(m1 * sizeof(int));
}
for (i = 0; i < n1; i++) {
for (j = 0; j < m1; j++) {
scanf("%d ", (A+i*m1+j));
}
}
scanf("%d %d", &n2, &m2); // second matrix n2 x m2
int **B = malloc(n2 * sizeof(int*));
for (i = 0; i < n2; i++) {
*(B+i) = malloc(m2 * sizeof(int));
}
for (i = 0; i < n2; i++) {
for (j = 0; j < m2; j++) {
scanf("%d ", (B+i*m2+j));
}
}
if (m1 != n2) {
printf ("ERROR\n");
} else {
int **C = malloc(n1 * sizeof(int*)); //matrix with product
for (i = 0; i < n1; i++) {
*(C+i) = malloc(m2 * sizeof(int));
}
for (i=0; i<n1; i++) {
for (j=0; j<m2; j++) {
*(C+i*m2+j) = 0;
}
}
for (k = 0; k < n2; k++) {
for (i = 0; i < n1; i++) {
for (j = 0; j < m2; j++) {
C1 = 0;
A1 = *(A+i*m1+k);
B1 = *(B+k*m2+j);
C1 = (C1+A1*B1)%1000;
temp = *(C+i*m2+j);
*(C+i*m2+j) = temp+C1;
}
}
}
for (i = 0; i < n1; i++) {
for (j = 0; j < m2; j++) {
printf("%d ", *(C+i*m2 +j));
}
printf("\n");
}
for (i = 0; i < n1; i++) { // free third matrix
free(*(C+i));
}
free(C);
}
for (i = 0; i < n1; i++) { // free first matrix
free(*(A+i));
}
free(A);
for (i = 0; i < n2; i++) { // free second matrix
free(*(B+i));
}
free(B);
}
return 0;
You write to memory beyond the allocated size. This invokes undefined behavior, a good explanation for the erratic behavior you observe.
The offending code is this:
for (k=0; k<n2; k++){
for (i=0; i<n1; i++) {
for (j=0; j<m2; j++) {
C1=0;
A1=*(A+i*m1+k);
B1=*(B+k*m2+j);
C1=(C1+A1*B1)%1000;
temp=*(C+i*m2+j);
*(C+i*m2+j)=temp+C1;
}
}
}
You assume that the matrices are allocated as a single packed 2D array.
Since you allocate them as arrays of pointers to arrays of int, you cannot use this code. Use the much simpler version below:
for (k = 0; k < n2; k++){
for (i = 0; i < n1; i++) {
for (j = 0; j < m2; j++) {
C[i][j] += A[i][k] * B[k][j] % 1000;
}
}
}
It is not clear why you compute modulo 1000. If you really mean that, you should use this:
C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % 1000;
If you are not supposed to use the [] syntax, replace it with the equivalent for pointer dereferencing, not 2D array addressing. I understand what they are trying to teach you, but a diagram would be more effective than this.
*(*(C+i)+j) = (*(*(C+i)+j) + *(*(A+i)+k) * *(*(B+k)+j)) % 1000;
As you may have noticed, the [] syntax is the same for 2D arrays and arrays of pointers to arrays, but the code generated for pointer arithmetic and dereferencing is quite different.
There are other places where you need to change the indexing method, here is the corrected code:
while (z--) { // the number of sets
scanf("%d %d", &n1, &m1); // first matrix n1 x m1
int **A = malloc(n1 * sizeof(int*));
for (i = 0; i < n1; i++) {
*(A+i) = malloc(m1 * sizeof(int));
for (j = 0; j < m1; j++) {
scanf("%d ", *(A+i)+j);
}
}
scanf("%d %d", &n2, &m2); // second matrix n2 x m2
int **B = malloc(n2 * sizeof(int*));
for (i = 0; i < n2; i++) {
*(B+i) = malloc(m2 * sizeof(int));
for (j = 0; j < m2; j++) {
scanf("%d ", *(B+i)+j);
}
}
if (m1 != n2) {
printf ("ERROR\n");
} else {
int **C = malloc(n1 * sizeof(int*)); //matrix with product
for (i = 0; i < n1; i++) {
// using calloc so matrix is initialized to 0
*(C+i) = calloc(m2, sizeof(int));
}
for (k = 0; k < n2; k++) {
for (i = 0; i < n1; i++) {
for (j = 0; j < m2; j++) {
*(*(C+i)+j) = (*(*(C+i)+j) + *(*(A+i)+k) * *(*(B+k)+j)) % 1000;
}
}
}
for (i = 0; i < n1; i++) {
for (j = 0; j < m2; j++) {
printf("%d ", *(*(C+i)+j));
}
printf("\n");
}
for (i = 0; i < n1; i++) { // free third matrix
free(*(C+i));
}
free(C);
}
for (i = 0; i < n1; i++) { // free first matrix
free(*(A+i));
}
free(A);
for (i = 0; i < n2; i++) { // free second matrix
free(*(B+i));
}
free(B);
}
return 0;