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,895 questions

51,826 answers

573 users

How to input n and find continuity of n zeros (0) in a matrix ([5][10]) in C

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
#define ROW 5
#define COL 10
 
void print_matrix(int m[][COL]);
void set_to_random_places(int m[][COL], int n, int sum);
int continuity_empty_places_exist(int m[][COL], int places);

int main(void)
{
    int matrix[ROW][COL] = { {0}, {0} };
         
    set_to_random_places(matrix, 1, 15);
    print_matrix(matrix);
    printf("\n");
    
    if (continuity_empty_places_exist(matrix, 5))
        printf("\ncontinuity places exist\n");
    else
        printf("\ncontinuity places not exist\n");    
    
    if (continuity_empty_places_exist(matrix, 7))
        printf("\ncontinuity places exist\n");
    else
        printf("\ncontinuity places not exist\n");    
   
    return 0;
}
 
int continuity_empty_places_exist(int m[][COL], int places)
{
    int i, j, counter = 1;
       
    for (i = 0; i < ROW; i++)
    {
        counter = 1;
        for (j = 0; j < COL; j++)
        {
            if (m[i][j] == 0)
                 counter++;
            else
                counter = 1;
            if (counter == places) return 1;
        }
    }
    return 0;
}   

void set_to_random_places(int m[][COL], int n, int sum)
{
    int counter = 1, i, j;
    
    srand(time(NULL));
    while (counter <= sum)
    {
        i = rand() % 5; // 0 - 4
        j = rand() % 10; // 0 - 9
        if (m[i][j] != n)
        {
            m[i][j] = n;
            counter++;
        }
    }
}
void print_matrix(int m[][COL])
{
    int i, j;
       
    for (i = 0; i < ROW; i++)
    {
        for (j = 0; j < COL; j++)
             printf("%4i", m[i][j]);
           
        printf("\n");
    }
}
 
 
/*
run:
 
   0   1   1   0   0   0   1   0   0   0
   1   0   0   1   0   0   0   1   0   0
   1   0   0   0   0   1   1   0   0   0
   1   0   1   0   1   0   0   0   0   0
   0   1   0   0   1   0   1   0   0   0


continuity places exist

continuity places not exist

*/ 


answered Sep 28, 2014 by avibootz

Related questions

1 answer 267 views
2 answers 130 views
1 answer 134 views
1 answer 149 views
1 answer 168 views
...