By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,367 Members | 1,281 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,367 IT Pros & Developers. It's quick & easy.

sub matrix

P: 36
suppose there is a matrix size colsXrows and i need to find number os sub matrices with the sum N .

this is the code for search sub matrices just on cols and rows, (sub matrices size>1)
Expand|Select|Wrap|Line Numbers
  1.            case 6:
  2.                printf("enter sum to search for ==>");
  3.                scanf("%d",&n);
  4.                for(i=0; i<cols; i++)
  5.                    for(j=0; j<rows; j++)
  6.                    {
  7.                        sum=0;
  8. //////////////////////////////////////////////////////////
  9.                        for(k=j; (k<rows)&&(sum<=n); k++)
  10.                        {
  11.                            sum=sum+arr[i][k];
  12.                        if((sum==n)&&(j<k))
  13.                          {                                    //==>  search for sub matrix sum N on rows.
  14.                            printf("#*****************#\n");
  15.                            sum=0;
  16.                          }
  17.                        }
  18.                        sum-0;
  19. //////////////////////////////////////////////////////////
  20.  
  21.                        for(k=i; (k<cols)&&(sum<=n); k++)
  22.                        {
  23.                            sum=sum+arr[k][j];
  24.                        if((sum==n)&&(i<k))
  25.                          {                                    //==>  search for sub matrix sum N on columns.
  26.                            printf("#*****************#\n");                    
  27.                            sum=0;
  28.                          }
  29.                        }
  30.                        sum=0;
  31. //////////////////////////////////////////////////////////
  32.  
in case of matrix 2X2 with the values {1,1,1,1}

for sub marices sum=2 need to get 4 sub matrices.

but something wrong in this code i gettin just 3 .
why is that ?
TNX ..........
Aug 19 '09 #1
Share this Question
Share on Google+
1 Reply


P: 36
never mind...... , find the prob on line 18 ; " sum-0 " S:
tnx anyway .
Aug 19 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.