There are two 2D arrays and r and c are their row and column, i and j are used for integer variables.
Transpose of matrix is running well till the value r and c are entered.
#include <stdio.h>
int main() {
// your code goes here
int a[50][50], b[50][50], i, j, r, c;
printf("Enter the value of R and c");
scanf("%d%d", &r, &c);
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
scanf("%d", &a[i][j]);
}
}
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
b[i][j] = a[i][j];
}
}
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
printf("%d\n", b[i][j]);
}
printf("\n");
}
return 0;
}
You are not "transposing" the matrix. You are simply copying one array to another. To transpose a matrix, you need to convert rows to columns:
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
b[j][i]=a[i][j];
}
}
and reverse c and r in the loop condition when printing:
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
{
printf("%d\n",b[i][j]);
}
printf("\n");
}
Note that your arrays are capable of handling 50x50. So you need to ensure the input values of r and c don't exceed these limits.
Your code is
#include <stdio.h>
int main() {
// your code goes here
int a[50][50], b[50][50], i, j, r, c;
printf("Enter the value of R and c\n");
scanf("%d%d", &r, &c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%3d",a[i][j]);
}
printf("\n");
}
printf("\n");
for(i = 0; i < c; i++)
{
for(j = 0; j < r; j++)
{
printf("%3d", a[j][i]);
}
printf("\n");
}
return 0;
}
Related
here below i have given multiplication of matrices in c language using for loop but can any help me make a more simplified version or can any help me make it using while loop
i want a simplified version
i want a code in while loop
:) just learning
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[10][10], b[10][10], mul[10][10], r, c, i, j, k;
system("cls");
printf("enter the number of row=");
scanf("%d", &r);
printf("enter the number of column=");
scanf("%d", &c);
printf("enter the first matrix element=\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("enter the second matrix element=\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
scanf("%d", &b[i][j]);
}
}
printf("multiply of the matrix=\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
mul[i][j] = 0;
for (k = 0; k < c; k++)
{
mul[i][j] += a[i][k] * b[k][j];
}
}
}
//for printing result
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d\t", mul[i][j]);
}
printf("\n");
}
return 0;
}
It will not make code simpler only harder to read.
One of the loops example:
printf("multiply of the matrix=\n");
i = 0;
while (i < r)
{
j = 0;
while(j < c)
{
mul[i][j] = 0;
k = 0;
while(k < c)
{
mul[i][j] += a[i][k] * b[k][j];
k++;
}
j++;
}
i++;
}
This is the code i am talking about. It checks if a matrix is sparse or not . If it is then it changes it to sparse form and transposes it. When I run it with Vscode it runs and displays till it is sparse matrix but doesn't show its sparse form and transpose form but on online compiler it runs successfully till the end.
#include <stdio.h>
int checksparse();
int changematrix();
int main()
{
int a[100][100];
int i, j, r, c;
printf("Enter number of rows and columns in the matrix\n");
scanf("%d%d", &r, &c);
printf("\nEnter the elements in the matrix");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("\na[%d][%d] = ", i, j);
scanf("%d", &a[i][j]);
}
}
int check = checksparse(a, r, c);
if (check == 1)
{
printf("\nIts a sparse matrix\n");
changematrix(a, r, c);
}
else if (check == 0)
{
printf("\nIts not a sparse matrix");
}
return 0;
}
int checksparse(int array[100][100], int r, int c)
{
int counter = 0, flag = 1;
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
if (array[i][j] == 0)
counter++;
}
}
if (counter > ((r * c) / 2))
{
return flag;
}
else
{
flag = 0;
return flag;
}
}
int changematrix(int array[100][100], int r, int c)
{
int b[100][100], temp, counter = 0;
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
if (array[i][j] != 0)
{
b[temp][0] = i;
b[temp][1] = j;
b[temp][2] = array[i][j];
temp++;
counter++;
}
}
}
printf("\nSparse Matrix : \n");
for (int i = 0; i < counter; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ", b[i][j]);
printf("\t");
}
printf("\n");
}
printf("\nTranspose of Sparse Matrix : \n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < counter; j++)
{
printf("%d ", b[j][i]);
printf("\t");
}
printf("\n");
}
return 0;
}
Could someone help me please?
I need to perform the sum of two matrices that the data will be sent by the user and print the result in a new matrix.
I managed to capture the data from the two arrays, but when I try to add the two, the code does not print the sum, where is the error?
Thanks
#include <stdio.h>
#include <stdlib.h>
void sum(int *mat_A, int *mat_B, int *mat_C);
int main() {
int mat_A[4][4];
int mat_B[4][4];
int mat_C[4][4];
int i, j, value;
printf("\nEnter integer values for the elements of matrix A: \n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("\nElement[%d][%d] = ", i, j);
scanf_s("%d", &value);
mat_A[i][j] = value;
}
}
printf("\nEnter integer values for the elements of matrix B: \n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("\nElement[%d][%d] = ", i, j);
scanf_s("%d", &value);
mat_B[i][j] = value;
}
}
calc_soma(*mat_A, *mat_B, *mat_C);
printf("\nSum of matrices A with B: \n\n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
value = mat_C[i][j];
printf("%d", value);
}
printf("\n");
}
return 0;
}
void sum(int *mat_A, int *mat_B, int *mat_C) {
int i, j;
int value;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
value = *mat_A + *mat_B;
*mat_C = value;
}
}
}
You need to declare the parameters to sum() as 2-dimensional arrays, not int *, which is a pointer to a 1-dimensional array. Then use i and j as array indexes.
You're also calling the function with the wrong name calc_soma.
#include <stdio.h>
#include <stdlib.h>
void sum(int mat_A[4][4], int mat_B[4][4], int mat_C[4][4]);
int main() {
int mat_A[4][4];
int mat_B[4][4];
int mat_C[4][4];
int i, j, value;
printf("\nEnter integer values for the elements of matrix A: \n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("\nElement[%d][%d] = ", i, j);
scanf_s("%d", &value);
mat_A[i][j] = value;
}
}
printf("\nEnter integer values for the elements of matrix B: \n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("\nElement[%d][%d] = ", i, j);
scanf_s("%d", &value);
mat_B[i][j] = value;
}
}
sum(mat_A, mat_B, mat_C);
printf("\nSum of matrices A with B: \n\n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
value = mat_C[i][j];
printf("%d", value);
}
printf("\n");
}
return 0;
}
void sum(int mat_A[4][4], int mat_B[4][4], int mat_C[4][4]) {
int i, j;
int value;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
value = mat_A[i][j] + mat_B[i][j];
mat_C[i][j] = value;
}
}
}
I wrote a code that tries to multiply two matrices and put them in another result matrix. The code is working (I think) but it prints a very strange output .. I think it has to do with one of the functions pointing to something diffrent than the values, not sure though.
I tried checking each function in separete, also inizialzing each matrix with {0}.
#include <stdio.h>
#define SIZE 5
//#pragma warning(disable:4996)
//#define _CRT_SECURE_NO_WARNINGS
void read_mat(int mat[][SIZE])
{
int i, j, k = 0;
char s[100]; // assign input str to 's'
fgets(s,25,stdin); // recieving only the first 25 numbers
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
if (s[k] == '\0') { //if the string is just \0 -- the end of the string
mat[i][j] = 0;
}
else { // is there are values in s
if (s[k] != '0') { // binary matrix -- only 0 or 1
mat[i][j] = 1;
k++;
}
else {
mat[i][j] = 0;
++k;
}
}
}
}
}
void mult_mat(int mat_a[][SIZE], int mat_b[][SIZE], int result_mat[][SIZE])
{
int i, j, k;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
for (k = 0; k < SIZE; k++) {
result_mat[i][j] += mat_a[i][k] * mat_b[k][j]; // by definition of matrix multiplication
k++;
}
}
}
}
void print_mat(int mat[][SIZE])
{
int i, j, k = 0;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
printf("%3d", &(mat[i][j])); // printing each value with a 3*space
}
printf("\n");
}
}
int main()
{
int mat_a[SIZE][SIZE] = {0}, mat_b[SIZE][SIZE] = {0}, result_mat[SIZE][SIZE] = {0}; // initializing matricies to {0}
printf("Please Enter Values For Matrix 1\n");
read_mat(mat_a);
printf("Please Enter Values For Matrix 2\n");
read_mat(mat_b);
mult_mat(mat_a, mat_b, result_mat);
printf("The First Matrix Is :- \n");
print_mat(mat_a);
printf("The Second Matrix Is :- \n");
print_mat(mat_b);
printf("The Resultant Matrix Is :- \n");
print_mat(result_mat);
return 0;
}
the outout I am getting:
enter image description here
thanks!
improve your print function
void print_mat(int mat[][SIZE])
{
int i, j, k = 0;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
printf("(%3d), ", &(mat[i][j])); // printing each value with a 3*space
}
printf("\n");
}
}
I am trying to implement Bubble sort in C and have come this far but its nor sorting properly.
#include<stdio.h>
int main()
{
int n, i, j, a[5], b, temp;
printf("Enter the number of elements to be sorted\n");
scanf("%d", &n);
for(i = 0; i < n; ++i)
{
printf("%d - Enter the elements - ", i);
scanf("%d", &a[i]);
}
for(i = 0; i < n; ++i)
{
for(j = 0; j < n+1; ++j)
{
if(a[i] > a[i+1])
{
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
}
for (i = 0; i < n; ++i)
{
printf("%d\n", a[i]);
}
return 0;
}
Input
2
12
1
13
Output
2
1
12
13
What am I missing ?
So now that the drama is behind us, the problem with your code was that you were not using the proper index in your inner loop. In addition your inner loop counter's conditional check was not correct. Also, as I mentioned in a comment to your question, you have a flaw in your code (which I have not fixed) where you initialize your array prior to asking the user how many elements they want to enter. This can lead to an index out of bounds exception if the user enters a number greater than 5.
#include<stdio.h>
int main()
{
int n, i, j, a[5], b, temp;
printf("Enter the number of elements to be sorted\n");
scanf("%d", &n);
for(i = 0; i < n; ++i)
{
printf("%d - Enter the elements - ", i);
scanf("%d", &a[i]);
}
for(i = 0; i < n; i++)
{
for(j = 0; j < n-1; j++)
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for (i = 0; i < n; ++i)
{
printf("%d\n", a[i]);
}
return 0;
}
your second loop is not proper.
for(j=0;j<n-i-1;j++){
}
/*By your approach , in inner loop you are not checking the each elements . So change i to j in swapping , and limit of j should be till n-1*/
#include<stdio.h>
int main()
{
int n, i, j, a[10], b, temp=0;
printf("Enter the number of elements to be sorted\n");
scanf("%d", &n);
for(i = 0; i < n; ++i)
{
printf("%d - Enter the elements - ", i);
scanf("%d", &a[i]);
}
for(i = 0; i < n; ++i)
{
for(j = 0; j < n-1; ++j) // notice limit , also complexity can be reduced by changing to(j<n-i-1)
{
if(a[j] > a[j+1])
{
temp = a[j]; // changed variable
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for (i = 0; i < n; ++i)
{
printf("%d\n", a[i]);
}
return 0;
}
/*You May check this code it will help you*/
#include<stdio.h>
void bubble_sort(int a[],int n);
void bubble_sort(int a[],int n)
{
int i,j;
int temp;
for(i=0;i<n;i++)
{
for (j=0; j<=n-i-1;j++)
{
if (a[j]>a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
main()
{
int a[10];
int i,n;
printf("Enter the number\n");
scanf("%d",&n);
printf("Enter the number\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
bubble_sort(a,n);
printf("sorted elements are\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
#include<stdio.h>
int main()
{
int n, i, j, a[5], b, temp;
printf("Enter the number of elements to be sorted\n");
scanf("%d", &n);
for(i = 0; i < n; ++i)
{
printf("%d - Enter the elements - ", i);
scanf("%d", &a[i]);
}
for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
{
if(a[j] > a[j+1]) //change the varible instead of i to j
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for (i = 0; i < n; ++i)
{
printf("%d\n", a[i]);
}
return 0;
}
I've tried to cover all possible conditions to reduce passes and comparisons for bubble sort to reduce the overall time taken. Here goes my code...
#include <stdio.h>
#include <conio.h>
void bubbleSort(int n){
int arr[n],i,j,temp=0;
int swapFlag = 0;
printf("\nInsert %d elements:\n",n);
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
}
printf("Insert complete.\n\n");
printf("Your array looks like:\n");
for(i=0;i<n;i++){
printf("%d ",arr[i]);
}
//Bubble Sort Algorithm
for(i=0;i<n-1;i++){
swapFlag = 0;
for(j=0;j<n-i-1;j++){
if(arr[j]>arr[j+1]){
swapFlag = 1;
//Swapping unordered pairs
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
//Condition to reduce number of passes & comparisons
if(swapFlag == 0){
break;
}
}
printf("\n\nAfter sorting the array looks like:\n");
for(i=0;i<n;i++){
printf("%d ",arr[i]);
}
}
void main(){
int n;
printf("Enter number of array elements: ");
scanf("%d",&n);
bubbleSort(n);
getch();
}
Result:-