I managed to do enter M, N, and accept its condition (0<N and M<=10), then create and apply desirable number into 2 matrices, which is called Matrix A and Matrix B, then the Matrix C is the plus of 2 Matrix A B.
The last one is the transpose, I have looked upon some sites, but since the ways they do are not like mine so I don't like to just "straightly" copy and paste it.
Here is my code:
#include <stdio.h>
int main()
{
int M, N, i, j;
printf ("Enter number M: ");
scanf("%d", &M);
printf ("Enter number N: ");
scanf("%d", &N);
if (0 < N && M <= 10) {
printf ("Accepted number M: %d\n", M);
printf("Accepted number N: %d\n", N);
}
else printf("Please enter valid number, which is 0 < N and M <= 10!");
int matrixA[M][N], matrixB[M][N], matrixC[M][N];
printf ("Enter matrix A: \n");
for (i=0; i<M; i++){
for (j=0; j<N; j++){
scanf("%d", &matrixA[i][j]);
}
}
printf ("Enter matrix B: \n");
for (i=0; i<M; i++){
for (j=0; j<N; j++){
scanf("%d", &matrixB[i][j]);
}
}
printf("Matrix A: \n");
for (i=0; i<M; i++){
for (j=0; j<N; j++){
printf("%d ", matrixA[i][j]);
}
printf("\n");
}
printf("Matrix B: \n");
for (i=0; i<M; i++){
for (j=0; j<N; j++){
printf("%d ", matrixB[i][j]);
}
printf("\n");
}
for (i=0; i<M; i++){
for (j=0; j<N; j++){
matrixC[i][j] = matrixA[i][j] + matrixB[i][j];
}
}
printf("Sum of matrix A and matrix B => Matrix C is: \n");
for (i=0; i<M; i++){
for (j=0; j<N; j++){
printf("%d ", matrixC[i][j]);
}
printf("\n");
}
int reverseMatrixC[N][M];
for (i=0; i<M; i++){
for (j=0; j<N; j++){
reverseMatrixC[j][i] = matrixC[i][j];
}
}
printf("Reverse columns and rows in matrix C: \n");
for (i=0; i<M; i++){
for (j=0; j<N; j++){
printf("%d ", reverseMatrixC[i][j]);
}
printf("\n");
}
}
The last one "Reverse columns and rows" are wrong and I don't know why, from my way of thinking, I'm switching M and N, then apply the previous Matrix C into a new variable which is reverseMatrixC, and then reverse i and j.
For example, I choose M = 2, N = 3.
and then I choose matrixA and matrixB these value:
1 2 3
4 5 6
The matrixC will display:
2 4 6
8 10 12
But when I transpose it, it'll display:
2 8 4
4 10 6
What I want is something like this:
2 8
4 10
6 12
You're very close. In fact, you're transposing matrixC correctly, the only problem is with how you're printing it out:
printf("Reverse columns and rows in matrix C: \n");
// reverseMatrixC has its rows and columns dimensions swapped, so N specifies its number of rows
for (i=0; i<N; i++){
// and M specifies its number of columns
for (j=0; j<M; j++){
printf("%d ", reverseMatrixC[i][j]);
}
printf("\n");
}
Also highly recommend to fix your indentation and use an IDE that formats for you, it makes reading code much easier.
Demo
Related
Firstly, a number N has to be input, and the matrix is of NxN dimensions then. The diagonal of the matrix has to be all 0, the elements of the matrix above the diagonal have to be from 1 to N diagonally, and the elements under the diagonal need to be filled from -1 to -N also diagonally. It should be something like this (if N=5):
But the problem that I have is that I print it out like this:
and I don't know how to fix it.
This is the code that I have:
`#include <stdio.h>
int main() {
int matrix[50][50], i, j, N;
printf("N: ");
scanf("%d",&N);
int k=0;
for(i=0; i<N; i++){
for(j=0; j<N; j++){
if(i==j){
matrix[i][j]=0;
}
else if(j>i && i!=j){
for(k=0; k<N; k++){
matrix[k-1][j]=k;
}
}
else if(j<i && i!=j){
for(k=0; k<N; k++){
matrix[i][k-1]=-k;
}
}
}
}
printf("Matrix:\n");
for (i=0; i<N; i++) {
for (j=0; j<N; j++)
printf("%4d", matrix[i][j]);
printf("\n");
}
return 0;
}`
I would really appreciate the help.
Here is you code modified, notice that 3 inner loops are removed with only one line.
second, you ask for the number N, however due to statically initialisation to maximum 50, you should as well verify that it is not. otherwise segmentation fault will happen.
or if you want to allow N >50 then better to do dynamic allocation on matrix.
#include <stdio.h>
int main() {
int matrix[50][50], i, j, N;
printf("N: ");
scanf("%d", &N);
if (N > 50){
printf("N should be smaller than 50, \n");
N = 50;
}
for(i=0; i<N; i++){
for(j=0; j<N; j++){
matrix[i][j]= j - i;
}
}
printf("Matrix:\n");
for (i=0; i<N; i++) {
for (j=0; j<N; j++)
printf("%4d", matrix[i][j]);
printf("\n");
}
return 0;
}
I already finished creating the sum, difference, and product. What I am having trouble with is the quotient. because when I run it, It will just give me 0.0. I tried making the arrA and arrB a float which makes the quotient work but the others (sum, difference, and product) do not work. Please help me on how I could get the quotient to work.
#include <stdio.h>
int main()
{
int arrA[2][2];
int arrB[2][2];
float arrSum[2][2];
float arrDiff[2][2];
float arrProd[2][2];
float arrQuo[2][2];
int i,j;
//1st Matrix
printf("Enter Elements of 1st Matrix\n");
for (i=1; i<3; i++) // size of rows
{
for(j=1; j<3; j++) // size of columns
{
printf("Enter a[%d][%d]: ",i,j);
scanf("%d", &arrA[i][j]); // to get value and save to array[][]
}
//printf("\n");
}
//Second Matrix
printf("Enter Elements of 2nd Matrix\n");
for (i=1; i<3; i++) // size of rows
{
for(j=1; j<3; j++) // size of columns
{
printf("Enter b[%d][%d]: ",i,j);
scanf("%d", &arrB[i][j]); // to get value and save to array[][]
}
//printf("\n");
}
// Sum of Matrix
printf("Sum of Matrix: \n");
for (i=1; i<3; i++) // size of rows
{
for(j=1; j<3; j++) // size of columns
{
arrSum[i][j] = arrA[i][j] + arrB[i][j];
printf("%.1f ", arrSum[i][j]);
}
printf("\n");
}
//Difference of Matrix
printf("Difference of Matrix: \n");
for (i=1; i<3; i++) // size of rows
{
for(j=1; j<3; j++) // size of columns
{
arrDiff[i][j] = arrA[i][j] - arrB[i][j];
printf("%.1f ", arrDiff[i][j]);
}
printf("\n");
}
//Product of Matrix
printf("Product of Matrix: \n");
for (i=1; i<3; i++) // size of rows
{
for(j=1; j<3; j++) // size of columns
{
arrProd[i][j] = arrA[i][j] * arrB[i][j];
printf("%.1f ", arrProd[i][j]);
}
printf("\n");
}
//Quotient of Matrix
printf("Quotient of Matrix: \n");
for (i=1; i<3; i++) // size of rows
{
for(j=1; j<3; j++) // size of columns
{
arrQuo[i][j] = arrA[i][j] / arrB[i][j];
printf("%.2f ", arrQuo[i][j]);
}
printf("\n");
}
return 0;
}
here is the result:
The output
here is what it should look like:
The supposed output
I wrote a programm for multiplying matrices together. For testing I entered:
A [1 0, 0 1] and B=[2 2, 2 2]
The result should be: [2 2, 2 2]. However, I got [2122123 2, 2 2]. Why does this happen? Maybe the value is an address in the storage?
#include <stdio.h>
int main(){
int A[10][10], B[10][10], C[10][10];
int n,m,r,s,k,sum,j,i=0;
printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d", &n,&m);
getchar();
printf("Enter values:\n");
for(i=0; i<n;i++){
for(j=0; j<m; j++){
scanf("%d", &A[i][j]);
getchar();
}
}
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d", &r,&s);
getchar();
if ( m != r )
printf("Matrices with entered orders can't be multiplied with each other.\n");
else{
printf("Enter values\n");
for(i=0; i<r;i++){
for(j=0; j<s; j++){
scanf("%d", &B[i][j]);
getchar();
}
}
for(i=0; i<n; i++){
for(j=0; j<s;j++){
for(k=0;k<r; k++){
sum=sum+ A[i][k]*B[k][j];
}
C[i][j]=sum;
sum=0;
}
}
printf("Product of entered matrices:-\n");
for(i=0; i<n;i++){
for(j=0; j<s; j++){
printf("%d\t",C[i][j] );
}
printf("\n");
}
}
return 0;
}
All in all I consider matrices of dimension nxm and rxs.
The line int n,m,r,s,k,sum,j,i=0; does not initialize sum to 0. It only sets i.
Set sum = 0; immediately before the loop over k, not after.
I am trying to write a program where I have two matrices and I multiply the two matrices and store it in a resultant matrix named "carr." For some weird reason, the matrix multiplication is not getting executed properly. Tried to find the issue for quite a while but couldn't find the error. Can anyone help? TIA for your time!
Here is the ss of the issue: https://snipboard.io/s9ifP4.jpg
#include <stdio.h>
int main()
{
int row1, column1, row2, column2,i,j,k, sum=0;
//START OF THE 1ST ARRAY//
printf("How many rows do you want for the first matrix? Ans: ");
scanf("%d", &row1);
printf("How many columns do you want for the first matrix? Ans: ");
scanf("%d", &column1);
int arr[row1][column1];
printf("Enter the elements of the first array:\n");
for(i = 0; i <row1; i++){
for(j=0; j < column1; j++){
scanf("%d", &arr[i][j]);
}
}
printf("\n----------------------------------------\n");
printf("The elements of the first array are:\n");
for(i = 0; i <row1; i++){
printf("[ ");
for(j=0; j < column1; j++){
printf("%d, ", arr[i][j]);
}
printf("]\n");
}
//END OF THE FIRST ARRAY//
printf("----------------------------------------\n");
//START OF THE 2ND ARRAY//
printf("\n**How many rows do you want for the second matrix?\n\nAlert: For matrix multiplication, the COLUMN of the 1st matrix MUST equal to the ROW of the 2nd matrix.\nAns: ");
scanf("%d", &row2);
printf("How many columns do you want for the second matrix? Ans: ");
scanf("%d", &column2);
int barr[row2][column2];
printf("Enter the elements of the second array:\n");
for(i = 0; i <row2; i++){
for(j=0; j < column2; j++){
scanf("%d", &arr[i][j]);
}
}
printf("\n----------------------------------------\n");
printf("The elements of the second array are:\n");
for(i = 0; i <row2; i++){
printf("[ ");
for(j=0; j < column2; j++){
printf("%d, ", arr[i][j]);
}
printf("]\n");
}
printf("----------------------------------------\n");
//END OF THE 2ND ARRAY//
//Everything above this part is okay. The problem starts from the Matrix multiplication part//
//MATRIX MULTIPLICATION//
//The resultant matrix where the values of the multiplied matrix is being held has row = ROW1 and column = COLUMN2.//
int carr[row1][column2];
if(column1 == row2)
{
for(i = 0; i < row1; i++){
for(j=0; j < column2; j++){
for(k=0; k < row2; k++){
sum = sum + arr[i][k] * barr[k][j];
}
carr[i][j] = sum;
sum=0;
}
}
}
else
{
printf("Matrix multiplication is not possible");
}
printf("\n----------------------------------------\n");
printf("The elements of the resultant array are:\n");
for(i = 0; i <row1; i++){
printf("[ ");
for(j=0; j < column2; j++){
printf("%d, ", carr[i][j]);
}
printf("]\n");
}
printf("----------------------------------------\n");
return 0;
}
Changing arr to barr fixes the issue. Thanks to #M Oehm for pointing out the error.
Hi, i am using a Function for the first time for my 2-D Array and so far i have been told it is right but i am having a problem with the 'For Loop'.
Specifically repeating the printed statement and numbers until the array is full, and also shows the specific array you are filling. (As the image shows).
This only prints 1 statement with 1 Array, whilst 4 numbers afterwards.. How could i get each number printed after each statement? I have tried the only way i know which made it worse.
Incorrectly Looped printed statement
Code below:
void DisplayArray(int a[2][2]);
int main()
{
int a[2][2], i, j, k;
/*Counter variables for the loop*/
printf ("***** Functions ***** \n");
for (i=0; i<2; i++)
for(j=0; j<2; j++)
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
scanf("%d", &a[i][j]);
DisplayArray(a);
return 0;
}
void DisplayArray(int a[2][2])
{
int i, j;
/*Displaying Array elements*/
printf("\n The 2-D Array contains : \n");
for(i=0; i<2; i++)
for(j=0; j<2; j++)
printf("%d \n" , a[i][j]);/*[i] name of 1 Array - [j] name of 2 Array*/
if(j==2)
printf("\n");
}
You forgot to properly define the scope of the inner for loops.
The lines
for (i=0; i<2; i++)
for(j=0; j<2; j++)
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
scanf("%d", &a[i][j]);
are equivalent to:
for (i=0; i<2; i++)
for(j=0; j<2; j++)
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
// Outside of both the for loops.
scanf("%d", &a[i][j]);
The indent expresses the intent to a human reader but not to the computer.
You need to use:
for (i=0; i<2; i++)
for(j=0; j<2; j++)
{ // Add
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
scanf("%d", &a[i][j]);
} // Add
To make the code more readable add an explicit scope for the outer for loops too.
for (i=0; i<2; i++)
{ // Add
for(j=0; j<2; j++)
{ // Add
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
scanf("%d", &a[i][j]);
} // Add
} // Add
Make similar changes to the other loops.
Your for loops are probably not acting as you intended, because without the curly braces, they will only act on the first statement.
void DisplayArray(int a[2][2]);
int main()
{
int a[2][2], i, j, k;
/*Counter variables for the loop*/
printf ("***** Functions ***** \n");
for (i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
scanf("%d", &a[i][j]);
}
}
DisplayArray(a);
return 0;
}
void DisplayArray(int a[2][2])
{
int i, j;
/*Displaying Array elements*/
printf("\n The 2-D Array contains : \n");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
printf("%d \n" , a[i][j]);/*[i] name of 1 Array - [j] name of 2 Array*/
if(j==2)
printf("\n");
}
}
}
You're missing some curly braces. Change to this:
for (i=0; i<2; i++)
for(j=0; j<2; j++) {
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
scanf(" %d", &a[i][j]);
DisplayArray(a);
}
and
for(i=0; i<2; i++)
for(j=0; j<2; j++) {
printf("%d \n" , a[i][j]);
if(j==2)
printf("\n");
}
Also, note that if(j==2) will never evaluate to true. When j is equal to 2, the loop is over. You should change this to 1. I would also suggest to remove \n in the innermost printf statement.
Complete code:
#include <stdio.h>
void DisplayArray(int a[2][2]);
int main()
{
int a[2][2], i, j, k;
/*Counter variables for the loop*/
printf ("***** Functions ***** \n");
for (i=0; i<2; i++)
for(j=0; j<2; j++) {
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
scanf(" %d", &a[i][j]);
DisplayArray(a);
}
return 0;
}
void DisplayArray(int a[2][2])
{
int i, j;
/*Displaying Array elements*/
printf("\n The 2-D Array contains : \n");
for(i=0; i<2; i++)
for(j=0; j<2; j++) {
printf("%d " , a[i][j]);/*[i] name of 1 Array - [j] name of 2 Array*/
if(j==1)
printf("\n");
}
}