Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8381897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:47:56+00:00 2026-06-09T16:47:56+00:00

Question is: Checking two matrices, if one is a sub matrix of another. The

  • 0

Question is: Checking two matrices, if one is a sub matrix of another.

The problem I am facing here is a for loop as commented as “//problem” in the code. When for the first time the program runs, the mentioned for loop does not work as it should.

#include <stdio.h>
#define N 10

int
main ()
{

  char matrix1[N][N], matrix2[N][N];
  int i, j, row1, col1, row2, col2, k, l, m, n, check = 0;

  //First Matrix
  printf ("Enter the data\n");
  printf ("Enter the size of rows\n");
  scanf ("%d", &row1);   
  printf ("Enter the size of columns\n");
  scanf ("%d", &col1);   
  printf ("Now enter the values please\n");

  //Putting Values In First Matrix
  for (i = 0; i < row1; i++)
    {
      for (j = 0; j < col1; j++)
        {
          printf ("Please enter the %dth row and %dth column\n", i + 1,
                  j + 1);
          scanf ("%s", &matrix1[i][j]);
        }
    }

  //Second Matrix
  printf ("Enter the data\n");
  printf ("Enter the size of rows\n");
  scanf ("%d", &row2);   
  printf ("Enter the size of columns\n");
  scanf ("%d", &col2);   
  printf ("Now enter the values please\n");

  //Putting Values In Second Matrix
  for (i = 0; i < row2; i++)
    {
      for (j = 0; j < col2; j++)
        {
          printf ("Please enter the %dth row and %dth column\n", i + 1,
                  j + 1);
          scanf ("%s", &matrix2[i][j]);
        }
    }

  //Checking Both Matrices
  for (i = 0; i < row1; i++)
    {
      for (j = 0; j < col1; j++)
        {
          if (matrix1[i][j] == matrix2[0][0])
            {
              k = i;
              l = j;
              for (m = 0; m < row2; m++)
                {
                  for (n = 0; n < col2; n++)
                    {           //problem
                      if (matrix1[k][l] == matrix2[m][n])
                        {
                          check++;
                          printf ("Checked\n");
                        }
                      l++;
                    }
                  l = j; 
                  k++;   
                }
            }
        }
      printf ("hello\n");
    }
  if (check == row2 * col2)
    {
      printf ("It exists\n");
    }
  else
    {
      printf ("It doesn't exist\n");
    }
}

Here is the output:

 Checked
 hello
 Checked
 Checked
 Checked
 Checked
 hello
 hello
 It doesn't exist
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:47:57+00:00Added an answer on June 9, 2026 at 4:47 pm

    You need to reset check to zero before starting to find sub-matrix.
    Also to break once you found it (or have flag to indicate if its found).

    As from your output, (assuming you are trying to find 2×2 matrix) it found it Checked printed times continuously, but its value would be 5 counting for 1st print as well, which makes your program to print "It does not exist".

    Like:

    int is_found = 0;
    ... //some code
    //Checking Both Matrices
    for (i = 0; i < row1; i++)
    {
      for (j = 0; j < col1; j++)
        {
          check = 0;   //reset check
          if (matrix1[i][j] == matrix2[0][0])
            {
          ... //your code to check matrix.
          ...
          }//if end
          if(check == row2*col2) 
          {
              is_found = 1;
          }
          ...
       } //for j end
       if(is_found)
          break;
       ...
    
     ...
     if(is_found)
        printf("It exists\n");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was checking out this question which has this code - (NSArray *) percentagesRGBArray:(float[])
This question here: MySQL: INSERT IGNORE or ON DUPLICATE KEY UPDATE with checking multiple
Question is already been asked in title. Here is a code: (function($){ var filter
I have a question about cloning within a loop, two problems actually and im
Actually this is a two parts question: First, is ip2long a good IP validator?
I have a WPF application with two PasswordBoxes, one for the password and another
This is actually a two part question. First,does the HttpContext.Current correspond to the current
I was checking Beej's guide to IPC and one line of code took my
I guess this is more or less a two-part question, but here's the basics
Pretty simple question. I have a few ASP RequiredFieldValdators checking some text boxes. Out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.