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;
}
Related
Im trying to sort a matrix by the sum of its row's digits, from highest to lowest. I dont know if i explained that correctly so here's some photos explaining it.
This is what my code outputs. Basically, it asks you for m and n, which are the dimensions of the matrix. In this example it's a 3x4, 3 rows and 4 columns. Then, the matrix should be sorted by rows, by the sum of row's digits. Which means, instead of what's being outputted in the picture above, the correct result should be this:
I have no idea how to sort this from highest to lowest, i have been trying for hours to no avail.
Here's my code:
#include <stdio.h>
#define N 30
void main(){
double a[N][N], s[N], p;
int i, j, m, n, max;
while(1){
printf("\nm, n? ");
scanf("%d%d", &m, &n);
if(m <= 0 || m > N || n <=0 || n > N)
break;
for(i = 0; i < m; i++){
printf("%2d. row? ", i+1);
for(j = 0; j < n; scanf("%lf", &a[i][j++]));
}
for(i = 0; i < m; i++)
for(s[i] = j = 0; j < n; s[i] += a[i][j++]);
for(j = 0; j < n - 1; j++){
for(max = i, j = i+1; j < n; j++)
if(s[j] > s[max])
max = i;
if(max != j){
p = s[j];
s[j] = s[max];
s[max] = p;
for(j = 0; j < m; j++){
p = a[j][i];
a[j][i] = a[j][max];
a[j][max] = p;
}
}
}
printf("New matrix: \n");
for(i = 0; i < m; i++){
for(j = 0; j < n; printf("%8.2lf", a[i][j++]));
printf("\n");
}
for(j = 0; j < m; j++)
printf("-------------");
printf("\n");
for(j = 0; j < m; printf("%8.2f \n", s[j++]));
printf("\n");
}
}
You can sort the rows of the matrix from highest to lowest, using a simple bubble sort algorithm.Your code modified below:
int main() {
double a[N][N], s[N], p;
int i, j, m, n, max;
while (1) {
printf("\nm, n? ");
scanf("%d%d", & m, & n);
if (m <= 0 || m > N || n <= 0 || n > N)
break;
for (i = 0; i < m; i++) {
printf("%2d. row? ", i + 1);
for (j = 0; j < n; scanf("%lf", & a[i][j++]));
}
for (i = 0; i < m; i++)
for (s[i] = j = 0; j < n; s[i] += a[i][j++]);
for (i = 0; i < m - 1; i++) { // modified here
for (j = i + 1; j < m; j++) { // modified here
if (s[j] > s[i]) { // modified here
p = s[i];
s[i] = s[j];
s[j] = p;
for (int k = 0; k < n; k++) {
p = a[i][k];
a[i][k] = a[j][k];
a[j][k] = p;
}
}
}
}
printf("New matrix: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; printf("%8.2lf", a[i][j++]));
printf("\n");
}
for (j = 0; j < m; j++)
printf("-------------");
printf("\n");
for (j = 0; j < m; printf("%8.2f \n", s[j++]));
printf("\n");
}
return 0;
}
Here's how i modified your code to achieve that:
Initialize a loop variable i to 0.
In the outer loop, run the inner loop j from i+1 to m-1.
In the inner loop, compare the sum of the row i with the sum of row
j. If the sum of row j is greater than the sum of row i, swap the
rows using a temporary variable.
After the inner loop finishes, increment the value of i by 1. Repeat
the outer loop until i becomes equal to m-1.
Output:
You can just use qsort to let it handle the sorting and item swapping. Then you only need to write the code for comparing two rows with each other.
Given something like this:
int matrix[3][4] =
{
{1,2,3,4},
{5,6,7,8},
{9,1,2,3},
};
You'd call qsort as:
qsort(matrix, 3, sizeof(int[4]), compare);
The only complexity is implementing the comparison callback function. There's two things to consider there:
We've told qsort that we have an array of 3 items, each of type int[4]. So the void pointers it passes along to us will actually be pointers to type int[4]. That is: int(*)[4].
qsort sorts in ascending order by default, where the item considered "less" ends up first. So we need to tweak that to get the largest item first.
Example:
int compare (const void* obj1, const void* obj2)
{
const int (*ptr1)[4] = obj1;
const int (*ptr2)[4] = obj2;
size_t sum1=0;
size_t sum2=0;
for(size_t i=0; i<4; i++)
{
sum1 += (*ptr1)[i];
sum2 += (*ptr2)[i];
}
if(sum1 > sum2) // largest sum considered "less" for qsort
return -1;
else
return 1;
return 0;
}
sum1 < sum2 would have placed the smallest row first.
Full example:
#include <stdio.h>
#include <stdlib.h>
int compare (const void* obj1, const void* obj2)
{
const int (*ptr1)[4] = obj1;
const int (*ptr2)[4] = obj2;
size_t sum1=0;
size_t sum2=0;
for(size_t i=0; i<4; i++)
{
sum1 += (*ptr1)[i];
sum2 += (*ptr2)[i];
}
if(sum1 > sum2) // largest sum considered "less" for qsort
return -1;
else
return 1;
return 0;
}
void print_matrix(size_t col, size_t row, int matrix[col][row])
{
for(size_t i=0; i<col; i++)
{
for(size_t j=0; j<row; j++)
{
printf("%d,", matrix[i][j]);
}
puts("");
}
}
int main (void)
{
int matrix[3][4] =
{
{1,2,3,4},
{5,6,7,8},
{9,1,2,3},
};
print_matrix(3,4,matrix);
puts("");
qsort(matrix, 3, sizeof(int[4]), compare);
print_matrix(3,4,matrix);
}
I have a problem. I need to write a program that checks if every row has the same sum, and if every column has same sum.
Example:
3 3 3
3 3 3
In this example output should be "YES" for rows and "YES" for columns.
Another example:
4 5 6
6 4 5
In this example, output should be "YES" for rows because sum of 1st and 2nd row is the same (15), and it should be "NO" for columns because sum is not the same (10,9,11).
I have made code that checks first row and first column than it compares if they are same as the other ones. I have made it to check if its not but I don't know how to check it if it is, I mean how to output "YES" for both cases.
This is my code:
#include <stdio.h>
int main() {
int mat[100][100];
int n, m;
int i, j;
int sumak = 0;
int sumar = 0;
int sumarp = 0;
int sumakp = 0;
do {
printf("Unesite brojeve M i N: ");
scanf("%d %d", &m, &n);
} while (m < 0 || m > 100 || n < 0 || n > 100);
printf("Unesite clanove matricee: ");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &mat[i][j]);
}
}
// suma of first row
for (i = 0; i < 1; i++) {
for (j = 0; j < n; j++) {
sumarp = sumarp + mat[i][j];
}
sumarp = sumarp + mat[i][j];
}
// sum of all rows
for (i = 1; i < m; i++) {
for (j = 0; j < n; j++) {
sumar = sumar + mat[i][j];
}
if (sumarp != sumar) {
printf("NE");
} else {
sumar = 0;
continue;
}
}
// sum of the first column
for (j = 0; j<1; j++) {
for (i = 0; i< m;i++ ) {
sumakp = sumakp + mat[i][j];
}
sumakp = sumakp + mat[i][j];
}
// sum of every column
for (j = 1; j < n; j++) {
for (i= 0; i < m; i++) {
sumak = sumak + mat[i][j];
}
if (sumakp == sumak) {
sumak=0;
continue;
}
if(sumakp!=sumak)
{
printf("NE");
return 0;
}
else{
printf("DA");
}
}
}
So if someone can explain me how to do the rest of it.
Thank you!
Use a variable to indicate whether any of the rows or columns didn't match. Then check it at the end of the loop.
Here's how to do it for rows. Columns are similar, just switch the order of the i and j loops.
int all_matched = 1;
for (int i = 0; i < m; i++) {
int sumar = 0;
for (int j = 0; j < n; j++) {
sumar += mat[i][j];
}
if (sumar != sumarp) {
all_matched = 0;
break;
}
}
if (all_matched) {
printf("EQ\n");
} else {
printf("NE\n");
}
I need to delete (not skip while printing) the rows and columns of a matrix that appear more than once in program, and I should print only first row from the top that appears more than once or the first column from the left that appears more than once.
Example input:
1 2 3 2
4 5 6 5
1 2 3 2
7 8 9 8
After deleting:
1 2 3
4 5 6
7 8 9
Here's my code:
#include <stdio.h>
int main() {
int i, j, m, n,row,col, mat[200][200];
scanf("%d %d", &m, &n);
row = m; col = n;
for (i = 0; i < m; i++)
for (j = 0; j < m; j++)
scanf("%d", &mat[i][j]);
for (i = 0; i < m; i++)
for (j = 0; j < m; j++) {
if (mat[i][j] == mat[i++][j++])
row--;
if (mat[j][i] == mat[j++][i++])
col--;
}
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
return 0;
}
Do you have any idea how to make the algorithm work for this task? Mine has mistakes.
Would you please try the following:
#include <stdio.h>
#define ROWS 200
#define COLS 200
#define TRUE 1
#define FALSE 0
/*
* delete k'th row from the m x n matrix
*/
void deleterow(int mat[ROWS][COLS], int m, int n, int k)
{
int i, j;
for (i = k; i < m - 1; i++) {
for (j = 0; j < n; j++) {
mat[i][j] = mat[i + 1][j];
}
}
}
/*
* delete k'th columns from the m x n matrix
*/
void deletecol(int mat[ROWS][COLS], int m, int n, int k)
{
int i, j;
for (j = k; j < n - 1; j++) {
for (i = 0; i < m; i++) {
mat[i][j] = mat[i][j + 1];
}
}
}
int main() {
int i, j, m, n,row,col, mat[ROWS][COLS];
int iref, jref; // reference indexes to compare
int match; // flag to show if the row/col duplicates
// read input matrix
scanf("%d %d", &m, &n);
row = m; col = n;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &mat[i][j]);
// examine row by row
for (iref = 0; iref < m; iref++) {
// compare rows below iref and remove the row if duplicates
for (i = iref + 1; i < m; i++) {
match = TRUE;
for (j = 0; j < n; j++) {
if (mat[i][j] != mat[iref][j]) {
match = FALSE;
break;
}
}
if (match) {
deleterow(mat, m, n, i);
m--;
}
}
}
// examine column by column
for (jref = 0; jref < n; jref++) {
// compare columns more right than jref and remove the col if duplicates
for (j = jref + 1; j < n; j++) {
match = TRUE;
for (i = 0; i < m; i++) {
if (mat[i][j] != mat[i][jref]) {
match = FALSE;
break;
}
}
if (match) {
deletecol(mat, m, n, j);
n--;
}
}
}
// see the result
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%2d%s", mat[i][j], j == n - 1 ? "\n" : " ");
}
}
return 0;
}
Output with the provided example:
1 2 3
4 5 6
7 8 9
[Explanation]
As for the operations of rows:
First, focus on the top row as a "reference". The row is indexed by
the variable iref which is assigned to 0 at first.
Then compare the remaining rows with the reference, changing
the row index i from iref+1 (just below the reference row) to n-1
(the bottom row).
If a row duplicates with the reference, remove the row with the
deleterow() function and decrement the row size m by one.
The modification of m affects the for loops which compare the
loop variables with m, meaning the matrix size is updated immediately.
This is a preferable nature of the for loop (IMHO).
If the comparizon reaches the bottom row, increment iref and repeat
the comparisons again.
Finally every row has been compared to each other and the duplicates have
been deleted.
Then perform the similar operations with columns.
The sum of rows part just doesn't work properly . Any suggestions?
Also if the main diagonal is i==j, what will be the opposite diagonal ? How do i define it?
int main (void) {
int A[5][5];
int B[5];
int x=0,sum=0;
int n,m,i=0,j;
printf("Enter rows and columns : \n");
scanf("%d %d",&n,&m);
printf("Enter matrix : \n");
for (i = 0 ; i < n ; i++) {
for (j = 0 ; j < m ; j++) {
scanf("%d",&A[i][j]);
}
}
/* Sum of rows Problem */
for(i = 0 ; i < n ; i++) {
B[i] = 0;
for(j = 0 ; j < m ; j++) {
B[i] = B[i] + A[i][j];
++i;
}
}
for(i = 0 ; i < n ; i++) {
for(j = 0 ; j < m ; j++) {
printf("The sum of rows %d \n", B[j]);
}
}
return 0;
}
Actually, you just have to remove ++i inside the inner loop, and the program runs fine.
Code:
int main (void) {
int A[5][5];
int B[5];
int x=0,sum=0;
int n,m,i=0,j;
printf("Enter rows and columns : \n");
scanf("%d %d",&n,&m);
printf("Enter matrix : \n");
for (i = 0 ; i < n ; i++) {
for (j = 0 ; j < m ; j++) {
scanf("%d",&A[i][j]);
}
}
/* Sum of rows Problem */
for(i = 0 ; i < n ; i++) {
B[i] = 0;
for(j = 0 ; j < m ; j++) {
B[i] = B[i] + A[i][j]; //Removed the stray ++i from here.
}
}
for(i = 0 ; i < n ; i++)
{
printf("The sum of row %d is %d \n",i+1,B[i]);
}
return 0;
}
And answering your second question, the opposite diagonal is i == size - j- 1 if size is the size of the array.
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;
}