Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to find repeated rows of a matrix in C

3 Answers

0 votes
#include <stdio.h>
#include <stdbool.h>
 
#define ROWS 7
#define COLS 3
 
// Function to check if two rows are identical
bool areRowsEqual(int row1[], int row2[], int cols) {
    for (int i = 0; i < cols; i++) {
        if (row1[i] != row2[i]) {
            return false;
        }
    }
    return true;
}
 
int main() {
    int matrix[ROWS][COLS] = {
        {1, 2, 3},
        {4, 5, 6},
        {1, 2, 3},
        {7, 8, 9},
        {4, 5, 6},
        {0, 1, 2},
        {4, 5, 6}
    };
 
    printf("Repeated rows:\n");
 
    // Check for repeated rows
    for (int i = 0; i < ROWS; i++) {
        for (int j = i + 1; j < ROWS; j++) {
            if (areRowsEqual(matrix[i], matrix[j], COLS)) {
                printf("Row %d and Row %d are identical.\n", i, j);
            }
        }
    }
 
    return 0;
}
 

 
/*
run:
 
Repeated rows:
Row 0 and Row 2 are identical.
Row 1 and Row 4 are identical.
Row 1 and Row 6 are identical.
Row 4 and Row 6 are identical.
 
*/

 



answered May 23, 2025 by avibootz
edited May 23, 2025 by avibootz
0 votes
#include <stdio.h>
#include <stdbool.h>

#define ROWS 7
#define COLS 3

// Function to compare two rows of a matrix
bool areRowsEqual(int* row1, int* row2, int cols) {
    for (int j = 0; j < cols; j++) {
        if (row1[j] != row2[j]) {
            return false;
        }
    }
    return true;
}

// Function to find and print repeated rows of a matrix
void findRepeatedRows(int matrix[][COLS], int rows, int cols) {
    printf("Repeated rows:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = i + 1; j < rows; j++) {
            if (areRowsEqual(matrix[i], matrix[j], cols)) {
                printf("Row %d is the same as Row %d\n", i, j);
            }
        }
    }
}

int main() {
    int matrix[ROWS][COLS] = {
        {1, 2, 3},
        {4, 5, 6},
        {1, 2, 3},
        {7, 8, 9},
        {4, 5, 6},
        {0, 1, 2}, 
        {4, 5, 6}
    };

    findRepeatedRows(matrix, ROWS, COLS);

    return 0;
}


/*
run:

Repeated rows:
Row 0 is the same as Row 2
Row 1 is the same as Row 4
Row 1 is the same as Row 6
Row 4 is the same as Row 6

*/

 



answered May 23, 2025 by avibootz
0 votes
#include <stdio.h>
#include <stdbool.h>

#define ROWS 7
#define COLS 3

// Function to find and print repeated rows of a matrix
void findRepeatedRows(int matrix[][COLS], int rows, int cols) {
    for (int i = 0; i < rows - 1; i++) {
        for (int j = i + 1; j < rows; j++) {
            int is_duplicate = 1;
            for (int k = 0; k < cols; k++) {
                if (matrix[i][k] != matrix[j][k]) {
                    is_duplicate = 0;
                    break;
                }
            }
            if (is_duplicate) {
                // Row j is a duplicate of row i
                printf("Row %d is a duplicate of row %d\n", j, i);
            }
        }
    }
}


int main() {
    int matrix[ROWS][COLS] = {
        {1, 2, 3},
        {4, 5, 6},
        {1, 2, 3},
        {7, 8, 9},
        {4, 5, 6},
        {0, 1, 2}, 
        {4, 5, 6}
    };

    findRepeatedRows(matrix, ROWS, COLS);

    return 0;
}


/*
run:

Row 2 is a duplicate of row 0
Row 4 is a duplicate of row 1
Row 6 is a duplicate of row 1
Row 6 is a duplicate of row 4

*/

 



answered May 23, 2025 by avibootz
...