Delete duplicate rows and columns of matrix - arrays

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.

Related

Why is the multiplication doubling in this C loop?

The code is supposed to take inputs to form a 3x3 Matrix and then multiply each term by the diagonal element of that line, but, for some reason that i don't know, it multiplies two times by the diagonal when the column index is bigger than the row index.
#include <stdio.h>
#define R 3
int a[R][R], i, j;
int main(void) {
for (i = 0; i < R; i++) {
for (j = 0; j < R; j++) {
printf("\nInsira o n%i%i ", i, j);
scanf("%i", &a[i][j]);
}
}
for (i = 0; i < R; i++) {
for (j = 0; j < R; j++) {
a[i][j] = a[i][j] * a[i][i];
}
}
for (i = 0; i < R; i++) {
printf("\n");
for (j = 0; j < R; j++) {
printf("%i ", a[i][j]);
}
}
}
input:
9 8 7
6 5 4
3 2 1
output:
81 648 567
30 25 100
3 2 1
The diagonal value for a given row is being changed before that row has been fully multiplied, so once the column goes past the diagonal, the multiplies are using the new value of that diagonal rather than the old value.
You can fix it (and improve the speed) as follows:
for (i = 0; i < R; i++) {
int tmp = a[i][i];
for (j = 0; j < R; j++) {
a[i][j] *= tmp;
}
}
Also, as mentioned, both i and j should be local variables.

Is sum of every row and column in matrix same

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");
}

Creating a matrix that has its elements as the sum of the indices of that element

int main() {
int n = 0;
int matrix[n][n];
printf("Insert the order of the matrix:");
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
matrix[i][j] = i + j;
printf("The matrix is:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
}
for a 2x2 matrix the output should be 0 1 1 2, but it is 1 2 1 2, and for a 3x3 matrix it should be 0 1 2 1 2 3 2 3 4 but it shows 2 3 4 2 3 4 2 3 4
The issue is that my output is always just my first row of the matrix, repeated n times. Any help?
#include <stdio.h>
int main() {
int n = 0;
printf("Insert the order of the matrix:");
scanf("%d", &n);
int matrix[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
matrix[i][j] = i + j;
printf("The matrix is:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
}
}
You declare the array with size n equal zero. Then the length of a row is zero, hence the offset for each consecutive row is zero, too. That means all rows are stored at the same place. As a final result, the last row's values overwrite all the previous rows.
Do input n before declaring matrix[n][n].

Arranging columns in a matrix lexicographically

I've been trying to sort columns in a matrix (the dimensions are m,n <= 10) via the lexicographical order (if the columns share the same element, then we compare the elements in the row beneath etc.) with some additional conditions. I need to use functions to print the matrix, input random integers up to 5 as its elements and finally arrange the matrix. I think I got the printing and random inputs correctly but I can't figure out the sorting. Plus I can't use global variables which I have no idea how to do, since I haven't been shown. An example matrix would be :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m, n;
int mat[10][10];
void print_matrix()
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ", mat[i][j]);
}
printf("\n");
}
}
void random_int()
{
int i, j;
srand((unsigned)time(NULL));
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
mat[i][j] = rand() % 5;
}
}
}
void arrange()
{
int i, j, k, a;
for (j = 0; j < n; ++j)
{
for (i = 0; i < m; ++i)
{
for (k = i + 1; k < m; ++k)
{
if (mat[i][j] < mat[k][j])
{
a = mat[i][j];
mat[i][j] = mat[k][j];
mat[k][j] = a;
}
}
}
}
}
printf("Input the number of rows : ");
scanf("%d", &m);
printf("Input the number of columns: ");
scanf("%d", &n);
random_int(mat[m][n]);
print_matrix(mat[m][n]);
arrange(mat[m][n]);
print_matrix(mat[m][n]);
return 0;
}
Try this solution(will work for input 0-8 only), also used global variables:
There have multiple solutions. but is the easiest one.
I have converted each of the columns as an integer value. then bubble sorted the integers. After sorting. I have then converted the integer value to digits. (You have to know how to convert individual digits to multiple digit integer and multiple digit integers to single-digit.
Note I have added one(1) with each digit. Because the input can be zero(0). if you convert 0 0 2 1 to an integer will be only 21. the first two digits lost. So I have added 1. so 0 0 2 1 will be converted to 1132. I have done (added 1) for each input(deducted 1 after sorting). so it will not affect other inputs. Be careful input have to be from(0 to 8)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int m, n;
int mat[10][10];
int generatedNumber[10];
void print_matrix()
{
printf("The matrix is:\n");
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ", mat[i][j]);
}
printf("\n");
}
}
void random_int()
{
int i, j;
srand((unsigned)time(NULL));
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
mat[i][j] = rand() % 5;
}
}
}
void arrange()
{
int i, j, k, a;
for (j = 0; j < n; ++j)
{
int number = 0;
for (i = 0; i < m; ++i)
{
number = number * 10 + mat[i][j] + 1;///Adding one for avoiding zero(0)
}
generatedNumber[j] = number;
}
for(i = 0; i < n; i++)
{
for(j = 0; j < n-i-1; j++)
{
if( generatedNumber[j] > generatedNumber[j+1])
{
// swap the elements
int temp = generatedNumber[j];
generatedNumber[j] = generatedNumber[j+1];
generatedNumber[j+1] = temp;
}
}
}
for(i = 0; i < n; i++)///columwise
{
int generatedColumnvalue = generatedNumber[i];
for(j = m -1; j>= 0; j--)///row wise and fro last vaue to first
{
mat[j][i] = (generatedColumnvalue%10)-1;///removing extra added 1
generatedColumnvalue/=10;
}
}
}
int main()
{
printf("Input the number of rows : ");
scanf("%d", &m);
printf("Input the number of columns: ");
scanf("%d", &n);
random_int();
print_matrix();
arrange();
print_matrix();
return 0;
}

A program to find if the sum of the elements in the middle three rows equals to sum of elements in the middle three columns of matrix print

I am trying to write a C program to find if the sum of the elements in the middle three rows equals to sum of elements in the middle three columns of matrix print. So far am able to find the sum of all the columns and rows in the matrix.
#include<stdio.h>
void main()
{
static int array[10][10];
int i, j, m, n,sum = 0;
printf("Enter the order of the matrix\n");
scanf("%d %d", &m, &n);
if (m>=5&& n>=5)
{
printf("Enter the elements of the matrix\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
sum = sum + array[i][j] ;
}
printf("Sum of the %d row is = %d\n", i, sum);
sum = 0;
}
sum = 0;
for (j = 0; j < n; ++j)
{
for (i = 0; i < m; ++i)
{
sum = sum + array[i][j];
}
printf("Sum of the %d column is = %d\n", j, sum);
sum = 0;
}
}
else
{
printf("The matrix should be a 5 by 5 or bigger");
}
}
Try this
int mid1 = (m-3)/2;
int mid2 = (n-3)/2;
int sum1=0,sum2=0;
//suppose m is 9 index(0-8), so this for loop will add the index 3,4,5
for(i=mid1; i<mid1+3; i++)
{
for(j=0; j<n; j++)
{
sum1+=array[i][j];
}
}
for(i=0; i<m; i++)
{
for(j=mid2; j<mid2+3; j++)
{
sum2+=array[i][j];
}
}
if(sum1==sum2)
//equal
else
//not equal
Let's assume that the array has n rows and m columns. In this case you can calculate the sums of middle three rows and columns the following way
if ( n >= 3 && m >= 3 )
{
int k = ( n - 3 ) / 2;
int l = ( m - 3 ) / 2;
int cols_sum = 0;
int rows_sum = 0;
for ( i = k; i < k + 3; i++ )
{
for ( j = 0; j < m; j++ ) rows_sum += array[i][j];
}
for ( i = 0; i < n; i++ )
{
for ( j = l; j < l + 3; j++ ) cols_sum += array[i][j];
}
if ( rows_sum == cols_sum ) /* print appropriate message */;
}
Here is a demonstrative program
#include <stdio.h>
#define N 3
#define M 3
#define RANGE 3
int main( void )
{
int array[N][M] =
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
size_t n = N;
size_t m = N;
if ( n >= RANGE && m >= RANGE )
{
size_t k = ( n - RANGE ) / 2;
size_t l = ( m - RANGE ) / 2;
size_t i, j;
int cols_sum = 0;
int rows_sum = 0;
for ( i = k; i < k + RANGE; i++ )
{
for ( j = 0; j < m; j++ ) rows_sum += array[i][j];
}
for ( i = 0; i < n; i++ )
{
for ( j = l; j < l + RANGE; j++ ) cols_sum += array[i][j];
}
if ( rows_sum == cols_sum )
{
printf( "The sums of three middle rows and columns are equal "
"each other and have value %d\n", rows_sum );
}
else
{
printf( "The sums of three middle rows and columns are not equal "
"each other.\n"
"The sum of the rows has value %d "
"and the sum of the columns has value %d\n",
rows_sum, cols_sum );
}
}
return 0;
}
The output is
The sums of three middle rows and columns are equal each other and have value 45
Now all what you need is to provide the appropriate dimensions of the array and the user input of the data.
In you code you are asking user for row and column count, but you have already decided on that, moreover you cannot create a array dynamically with user input. However, there are ways you can work with to create arrays based on users request, we have Dynamic Memory Allocation concept to the rescue. For for time being, work with predefined size.
How to get row Data
In c, you have to fix on a row as i, in array[i][j] i is row and j is column reference, and loop through j to get all the row element values
How to get Column Data
same as above, loop through i and fix on j, exactly opposite.
#include<stdio.h>
int main()
{
/*This code works for only odd numbers and you have to make necessary changes to accommodate for even number of rows and columns*/
const int row = 5, colmn = 5;
static int array[row][colmn];
int i, j, m, n,sumRow = 0, sumColmn = 0;
int middleRow = row / 2, middleColmn = colmn / 2;
int howManyRows = 3;
printf("Enter the elements of the matrix\n");
/*
* first get elements from user
*/
for (i = 0; i < row; ++i)
{
for (j = 0; j < colmn; ++j)
{
printf("[%d][%d]",i,j);
scanf("%d", &array[i][j]);
}
}
/*
* printing elements to cross check the output
*/
for (i = 0; i < row; ++i)
{
for (j = 0; j < colmn; ++j)
{
printf("%d\t",array[i][j]);
}
printf("\n");
}
/*
* logic : get rows to adds (outer loop will loop through the rows to consider)
* inner loop is to get all the elements in each row
*/
for(int threeRows = 0 - howManyRows/2 ; threeRows <= howManyRows/2 ;threeRows++)
{
for (j = 0; j < row; ++j)
{
sumRow += array[j][middleColmn + threeRows];
}
}
/*
* its the same as above just the opposite
*/
for(int threeColmn = 0 - howManyRows/2; threeColmn <= howManyRows/2 ;threeColmn++)
{
for (j = 0; j < row ; ++j)
{
sumColmn += array[middleColmn + threeColmn][j];
}
}
printf("middleRow = %d middleColmn = %d sumRow = %d sumColmn = %d",middleRow, middleColmn,sumRow,sumColmn);
if(sumRow == sumColmn) printf("\nThey are equal");
else printf("\nnot equal");
return 0;
}

Resources