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 check if a matrix rows contain numbers without repetition in C

2 Answers

0 votes
#include <stdio.h>
#include <stdbool.h>

#define ROWS 3
#define COLS 3

// Function to check if a row contains unique numbers
bool isRowUnique(int row[], int len) {
    int uniqueNumbers[1024] = {0}; // Array to track seen numbers

    for (int i = 0; i < len; i++) {
        if (uniqueNumbers[row[i]]) {
            return false; // Found a repetition
        }
        uniqueNumbers[row[i]] = 1;
    }
    return true;
}

// Function to check if all matrix rows are unique
bool areMatrixRowsUnique(int matrix[ROWS][COLS]) {
    for (int i = 0; i < ROWS; i++) {
        if (!isRowUnique(matrix[i], COLS)) {
            return false; // If any row has repetitions, return false
        }
    }
    return true; // All rows are unique
}

int main() {
    // Matrix // Work for number form 0 up to 1023
    int matrix[ROWS][COLS] = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 8} // This row contains a repetition
    };

    if (areMatrixRowsUnique(matrix)) {
        printf("All rows contain unique numbers.\n");
    } else {
        printf("Some rows contain repeated numbers.\n");
    }

    return 0;
}


/*
run:

Some rows contain repeated numbers.

*/

 



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

#define ROWS 3
#define COLS 3

// Function to find the maximum number in the matrix
int findMaxValue(int matrix[ROWS][COLS]) {
    int maxVal = matrix[0][0];
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            if (matrix[i][j] > maxVal) {
                maxVal = matrix[i][j];
            }
        }
    }
    return maxVal;
}

// Function to check if a row contains unique numbers
bool isRowUnique(int row[], int len, int maxVal) {
    bool *uniqueNumbers = (bool*)calloc(maxVal + 1, sizeof(bool)); // Allocate memory dynamically

    if (!uniqueNumbers) { // Check memory allocation
        printf("Memory allocation failed!\n");
        return false;
    }

    for (int i = 0; i < len; i++) {
        if (uniqueNumbers[row[i]]) {
            free(uniqueNumbers); // Free allocated memory before returning
            return false; // Found a repetition
        }
        uniqueNumbers[row[i]] = true;
    }

    free(uniqueNumbers); // Free allocated memory after use
    return true;
}

// Function to check if all matrix rows are unique
bool areMatrixRowsUnique(int matrix[ROWS][COLS]) {
    int maxVal = findMaxValue(matrix); // Get max number for dynamic allocation

    for (int i = 0; i < ROWS; i++) {
        if (!isRowUnique(matrix[i], COLS, maxVal)) {
            return false; // If any row has repetitions, return false
        }
    }
    return true; // All rows are unique
}

int main() {
    // Matrix with large numbers - More general solution
    int matrix[ROWS][COLS] = {
        {31000, 23898, 18370},
        {12004, 56230, 37123},
        {18370, 89231, 18370} // This row contains a repetition
    };

    if (areMatrixRowsUnique(matrix)) {
        printf("All rows contain unique numbers.\n");
    } else {
        printf("Some rows contain repeated numbers.\n");
    }

    return 0;
}



/*
run:

Some rows contain repeated numbers.

*/

 



answered May 23, 2025 by avibootz
...