The size of the square matrix is entered as standard input (the range of input values is 2 to 10). Create a program that prints the unit matrix of the input size.
The unit matrix is a matrix whose diagonal from the top left to the bottom right (main diagonal) is 1 and all others are zero.
ex) input: 5
result :
1 0 0 0 0\n
0 1 0 0 0\n
0 0 1 0 0\n
0 0 0 1 0\n
0 0 0 0 1\n
my code:
int main(){
int n;
scanf("%d", &n);
int **matrix = malloc(sizeof(int *) * n);
for (int i = 0; i < n; i++)
{
matrix[i] = malloc(sizeof(int) * n);
memset(matrix[i], 0, sizeof(int) * n);
}
for (int i = 0; i < n; i++)
{
matrix[i][i] = 1;
printf("%d ", matrix[i][i]);
}
printf("\n");
for (int i = 0; i < n; i++)
{
free(matrix[i]);
}
free(matrix);
return 0;
}
.. what 's the error?
You need a double loop to print the output:
for (int i = 0; i < n; i++)
{
matrix[i][i] = 1; // set identity
}
for (int j=0; j<n; j++)
{
for (int i = 0; i < n; i++)
{
printf("%d ", matrix[j][i]);
}
printf("\n");
}
One error is to think yo need a data strucure to print the output requested.
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int n;
if (scanf("%d", &n) != 1 || n < 2 || 10 < n) {
perror("Input error!\n\n");
return EXIT_FAILURE;
}
for (int i = 0; i < n; ++i) {
for (int k = 0; k < n; ++k)
printf(i == k ? "1 " : "0 ");
putchar('\n');
}
}
Related
This below is my ds hw code.
#include <stdio.h>
typedef struct
{
int row;
int col;
int val;
}term;
void CreateTriplet(term t[],int a[][10],int m, int n)
{
int i, j, k = 0;
t[0].row = m;
t[0].col = n;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j ++)
{
if(a[i][j] == 0)
continue;
k++;
t[k].row = i;
t[k].col = j;
t[k].val = a[i][j];
}
}
t[0].val = k;
}
void printTriplet(term t[], int k)
{
int i;
for(i = 0 ; i < k ; i++)
{
printf("%d\t", t[i].row);
printf("%d\t", t[i].col);
printf("%d\n", t[i].val);
}
}
void findTranspose(term t1[], term t2[])
{
int i, j, k;
t2[0].row = t1[0].col;
t2[0].col = t1[0].row;
t2[0].val = t1[0].val;
k = 1;
for (i = 0; i < t1[0].col; i++)
{
for (j = 1; j <=t1[0].val; j++)
{
if (t1[j].col == i)
{
t2[k].row = t1[j].col;
t2[k].col = t1[j].row;
t2[k].val = t1[j].val;
k++;
}
}
}
}
int main()
{
int a[10][10], i, j, m, n, zero=0, nonzero;
term t1[101], t2[102];
scanf("%d %d", &m, &n);
for(i = 0; i < m ;i++)
{
for(j= 0; j < n ;j++)
{
scanf("%d]\n", &a[i][j]);
if(a[i][j]==0)
zero++;
}
}
if (i*j != i*i && j*j) /* I have no idea how to modify this section for the output*/
{
printf("Input matrix has wrong size. Please input again. \n");
}
else
{
nonzero = m * n - zero;
printf("Sparse matrix by triplet form:\n");
CreateTriplet(t1, a, m, n);
printTriplet(t1, nonzero+1);
findTranspose(t1, t2);
printf("Transpose of the sparse matrix:\n");
printTriplet(t2, nonzero+1);
}
return 0;
}
The main problem is in the section (if statement.)
When I was writing the if conditional, the input size was 6 * 6. Also, the elements in the matrix are not 6 * 6. My idea is to let the condition determine row * col != input elements. I don't know how to modify the if statement. Can someone help me?
**Input**
55
15 0 0 22 0
0 0 3 0 0
0 0 0 -6 0
0 0 0 0 7
91 0 0 0 0
**Output**
Sparse matrix by triplet form:
5 5 6
0 0 15
0 3 22
1 2 3
2 3 -6
3 4 7
4 0 91
Transpose of the sparse matrix:
5 5 6
0 0 15
0 4 91
2 1 3
3 0 22
3 2 -6
4 3 7
**Input**
66
15 0 0 22 0 -15 100
0 11 3 0 0 0
0 0 0 -6 0 0
0 0 0 0 0 0
91 0 0 0 0 0
0 0 28 0 0 0
1 0 0 0 0 0
**Output**
Input matrix has wrong size. Please input again.
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int row;
int col;
int val;
} term;
void CreateTriplet (term t[],int a[][10],int m, int n) {
int i, j, k = 0;
t[0].row = m;
t[0].col = n;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j ++) {
if(a[i][j] == 0)
continue;
k++;
t[k].row = i;
t[k].col = j;
t[k].val = a[i][j];
}
}
t[0].val = k;
}
void printTriplet(term t[], int k) {
int i;
for(i = 0 ; i < k ; i++) {
printf("%d\t", t[i].row);
printf("%d\t", t[i].col);
printf("%d\n", t[i].val);
}
}
void findTranspose(term t1[], term t2[]) {
int i, j, k;
t2[0].row = t1[0].col;
t2[0].col = t1[0].row;
t2[0].val = t1[0].val;
k = 1;
for (i = 0; i < t1[0].col; i++) {
for (j = 1; j <=t1[0].val; j++) {
if (t1[j].col == i) {
t2[k].row = t1[j].col;
t2[k].col = t1[j].row;
t2[k].val = t1[j].val;
k++;
}
}
}
}
int main (int argc, char * *argv, char * *envp) {
int a[10][10], i, j, m, n, zero = 0, nonzero;
term t1[101], t2[102];
int ok_or_not = 1;
while (ok_or_not == 1) {
scanf ("%d %d", &m, &n);
if (m * n != m * m) {
printf ("Input matrix has wrong size. Please input again. \n");
}
else {
ok_or_not = 0;
}
}
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d]\n", &a[i][j]);
if (a[i][j] == 0)
zero++;
}
}
if (i * j != i * i && j * j) { /* I have no idea how to modify this section for the output*/
} else {
nonzero = m * n - zero;
printf("Sparse matrix by triplet form:\n");
CreateTriplet(t1, a, m, n);
printTriplet(t1, nonzero + 1);
findTranspose(t1, t2);
printf("Transpose of the sparse matrix:\n");
printTriplet(t2, nonzero + 1);
}
return (EXIT_SUCCESS);
}
In this code I took two 1D arrays and kept multiplicand and multipliers in those.
Then I kept the multiplication results in a 2d array to add them.
{
int n, m, i, j;
printf("Enter multiplicand(n) size: ");
scanf("%d", &n);
printf("Enter multiplier(m) size: ");
scanf("%d", &m);
int a[n], b[m];
printf("Enter multiplicands: ");
for(i = 0; i<n; i++)
{
scanf("%d", &a[i]);
}
printf("Enter multipliers: ");
for(i = 0; i<m; i++)
{
scanf("%d", &b[i]);
}
int c[m][n+m] , k = 0 , l = m+n-1 , p = 2;
for(i = 0; i<m; i++)
{
for(j = 0; j<m+n; j++)
{
c[i][j] = -1;
}
}
for(i = m-1; i>=0; i--)
{
for(j = n-1; j>=0; j--)
{
c[k][l] = a[j]*b[i];
l--;
}
l = m+n-p;
p++;
k++;
}
for(i = 0; i<m; i++)
{
for(j = 0; j<m+n; j++)
{
printf("%d ", c[i][j]);
}
printf("\n");
}
}
I was able to get -1's on the top left but I want 0's in the right bottom of the numbers
So, if I input a[5] = {1 , 1 , 1 , 1 , 1} , b[5] = {1 , 1 , 1 , 1 , 1}
I should get:
-1 -1 -1 -1 -1 1 1 1 1 1
-1 -1 -1 -1 1 1 1 1 1 0
-1 -1 -1 1 1 1 1 1 0 0
-1 -1 1 1 1 1 1 0 0 0
-1 1 1 1 1 1 0 0 0 0
Here is some cleaner code with print outs. I really don't understand what you are trying to do. The whole -1 loop is confusing and the output example is not clear.
I think you are trying to just add the multiplication result to a new array? But what does the -1 mean?
#include <stdio.h>
int main()
{
int n, m, i,l, j,k,p;
n =5;
m = 5;
int a[5] = {1}, b[5] = {1};
int c[m][n+m];
k = 0;
l = m+n-1;
p = 2;
for(i = 0; i<m; i++)
{
printf("\n");
for(j = 0; j<m+n; j++)
{
c[i][j] = -1;
printf("[%i][%i]:%i ",i,j,c[i][j]);
}
}
printf("\n");
for(i = m-1; i>=0; i--)
{
printf("\n");
for(j = n-1; j>=0; j--)
{
c[k][l] = a[j]*b[i];
printf("[%i][%i]: %i x %i = %i ",k,l,a[j],b[i],c[k][l]);
l--;
}
l = m+n-1;
k++;
}
printf("\n\n");
for(i = 0; i<m; i++)
{
for(j = 0; j<m+n; j++)
{
printf("%d ", c[i][j]);
}
printf("\n");
}
}
If you explain more about what you are trying to do then I can edit this to help.
For example, if your 'a' array is {2,4} and your 'b' array is {3,6}. What is the expected output of the 'c' array.
c = { ? }
So I'm trying to write a program that makes matrix from random numbers and user can input the number of lines and columns
#include <stdio.h>
#include <stdlib.h>
int main() {
int c, x, n, m;
printf("write the number of lines\n");
scanf("%d", &n);
for (c = 1; c <= n; c++) {
x = rand() % 100 + 1;
printf("%d\n", x);
}
return 0;
}
This is what I have, I need it to generate columns, but I don't know how. m would be the number of columns
This is one of many ways to do it
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j, rows, columns;
int **matrix;
printf("write the number of rows\n");
scanf("%d", &rows);
printf("write the number of columns\n");
scanf("%d", &columns);
matrix = malloc(rows * sizeof *matrix);
if (matrix == NULL)
return -1;
for (i = 0 ; i < rows ; i++)
{
matrix[i] = malloc(columns * sizeof(int));
if (matrix[i] == NULL)
{
int k;
for (k = i ; k >= 0 ; k--)
free(matrix[k]);
free(matrix);
return -1;
}
for (j = 0 ; j < columns ; j++)
matrix[i][j] = rand() % 100;
}
for (i = 0 ; i < rows ; i++)
{
for (j = 0 ; j < columns ; j++)
printf("%5d", matrix[i][j]);
printf("\n");
}
for (i = 0 ; i < rows ; i++)
{
if (matrix[i] == NULL)
continue;
free(matrix[i]);
}
free(matrix);
return 0;
}
I need to move the largest number of randomly generated matrix to the right lower corner of matrix, but I don't know how. Can someone help.
This is my code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j, rows, columns;
int **matrix;
printf("write the number of rows\n");
scanf("%d", &rows);
printf("write the number of columns\n");
scanf("%d", &columns);
matrix = malloc(rows * sizeof *matrix);
for (i = 0 ; i < rows ; i++)
{
matrix[i] = malloc(columns * sizeof(int));
for (j = 0 ; j < columns ; j++)
matrix[i][j] = rand() % 100;
}
for (i = 0 ; i < rows ; i++)
{
for (j = 0 ; j < columns ; j++)
printf("%5d", matrix[i][j]);
printf("\n");
}
for (i = 0 ; i < rows ; i++)
free(matrix[i]);
free(matrix);
return 0;
}
Try the following
int max = matrix[0][0];
for (i = 0 ; i < rows ; i++)
{
for (j = 0 ; j < columns ; j++)
{
if ( max < matrix[i][j] ) max = matrix[i][j];
}
}
matrix[rows-1][columns-1] = max;
If you need to exchange the right lower element of the matrix with the maximum element then the code can look like
int max_i = 0;
int max_j = 0;
for (i = 0 ; i < rows ; i++)
{
for (j = 0 ; j < columns ; j++)
{
if ( matrix[max_i][max_j] < matrix[i][j] ) max_i = i, max_j = j;
}
}
int tmp = matrix[rows-1][columns-1];
matrix[rows-1][columns-1] = matrix[max_i][max_j];
matrix[max_i][max_j] = tmp;
You can generate the matrix with the requested feature this way
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j, rows, columns, largest, largestRow, largestColumn, swapValue;
int **matrix;
printf("write the number of rows\n");
scanf("%d", &rows);
printf("write the number of columns\n");
scanf("%d", &columns);
largest = 0;
largestRow = 0;
largestColumn = 0;
matrix = malloc(rows * sizeof *matrix);
if (matrix == NULL)
return -1;
for (i = 0 ; i < rows ; i++)
{
matrix[i] = malloc(columns * sizeof(int));
if (matrix[i] == NULL)
{
int k;
for (k = i - 1 ; k >= 0 ; k--)
free(matrix[k]);
free(matrix);
return -1;
}
for (j = 0 ; j < columns ; j++)
{
matrix[i][j] = rand() % 100;
if (matrix[i][j] > largest)
{
largestRow = i;
largestColumn = j;
largest = matrix[i][j];
}
}
}
swapValue = matrix[rows - 1][columns - 1];
matrix[rows - 1][columns - 1] = largest;
matrix[largestRow][largestColumn] = swapValue;
for (i = 0 ; i < rows ; i++)
{
for (j = 0 ; j < columns ; j++)
printf("%5d", matrix[i][j]);
printf("\n");
}
for (i = 0 ; i < rows ; i++)
free(matrix[i]);
free(matrix);
return 0;
}
I'm having a problem with matrix.
When i'm trying to print the last matrix ( Invertible matrix ) to file ( matrixrez.txt) program prints only first column.
Why?
Matrix.txt:
1 0 1 1 2 0
1 0 2 0 3 0
2 1 1 1 2 3
Here's my code:
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *file, *outf;
int matrixA[3][3], matrixB[3][3], a[3][3];
int trash[3];
int i, j, k, sum;
float determinant = 0;
i = j = k = sum = 0;
file = fopen("matrix.txt", "rt");
outf = fopen("matrixrez.txt", "w+");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
fscanf(file, "%d", &matrixA[i][j]);
}
for (k = 0; k < 3; k++) fscanf(file, "%d", &trash[k]);
}
fseek(file, 0, SEEK_SET);
for (i = 0; i < 3; i++) {
for (k = 0; k < 3; k++) fscanf(file, "%d", &trash[k]);
for (j = 0; j < 3; j++) {
fscanf(file, "%d", &matrixB[i][j]);
}
}
/* Matrix multiplication */
for ( i = 0 ; i < 3 ; i++ )
{
for ( j = 0 ; j < 3 ; j++ )
{
for ( k = 0 ; k < 3 ; k++ )
{
sum = sum + matrixA[i][k]*matrixB[k][j];
}
a[i][j] = sum;
sum = 0;
}
}
for(i=0;i<3;i++)
determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));
for (i = 0; i < 3; i++) {
printf("\n");
for (j = 0; j < 3; j++) {
printf("%d ", matrixA[i][j]);
}
}
printf("\n");
for (i = 0; i < 3; i++) {
printf("\n");
for (j = 0; j < 3; j++) {
printf("%d ", matrixB[i][j]);
}
}
printf("\n");
printf("The resultant matrix is:: \n");
fprintf(outf,"The resultant matrix is:: \n");
for (i = 0; i < 3; i++) {
printf("\n");
fprintf(outf,"\n");
for (j = 0; j < 3; j++) {
printf("%d ", a[i][j]);
fprintf(outf,"%d ", a[i][j]);
}
}
printf("\n\n Inversion of Matrix: \n");
fprintf(outf,"\n\n Inversion of Matrix: \n");
for(i=0;i<3;i++){
for(j=0;j<3;j++)
printf("%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3] [(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
fprintf(outf,"%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
fprintf(outf,"\n");
printf("\n");
}
printf("\n");
system("pause");
return 0;
}
I get matrixrez.txt:
The resultant matrix is::
2 4 3
3 6 6
3 9 3
Inversion of Matrix:
4.00
-1.67
-0.67
I think something wrong is with line at the end of a file (if I'm not mistaken) :
fprintf(outf,"%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
You need to put braces around what you meant to have as your for loop block at the end:
printf("\n\n Inversion of Matrix: \n");
fprintf(outf,"\n\n Inversion of Matrix: \n");
for(i=0;i<3;i++){
for(j=0;j<3;j++)
{ /* YOU NEED THIS BRACE */
printf("%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3] [(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
fprintf(outf,"%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
} /* AND YOU NEED THIS BRACE */
fprintf(outf,"\n");
printf("\n");
}
You do not have brackets around the inner for loop. So, it loops through i just fine but each time it cycles through j without calling the fprintf just the printf
The code should be
printf("\n\n Inversion of Matrix: \n");
fprintf(outf,"\n\n Inversion of Matrix: \n");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3] [(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
fprintf(outf,"%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
fprintf(outf,"\n");
printf("\n");
}
}