Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Finding a sub matrix question

  1. Dec 23, 2008 #1
    I need to enter a number and find how many sub matrices exist which the sum of their
    members equals the input numbers.

    for this matrix
    8 6 0 3
    0 2 2 1
    0 2 2 4
    and number 8 it outputs 9
    i can find 9 sub matrices which the sum of their members equals the input number.

    http://img357.imageshack.us/img357/7137/38991334cz2.gif

    how to do this thing
    ??

    For the given matrix

    8 6 0 3
    0 2 2 1
    0 2 2 4


    For example

    8 6 0
    0 2 2
    0 2 2

    8 6
    0 2
    0 2

    8
    0
    0

    8 6 0 3
    0 2 2 1

    8 6 0 3

    6 0 3
    2 2 1
    2 2 4

    6 0
    2 2
    2 2

    etc etc
    i tried to write a software
    for each cell of the matrix i sent it to a function
    which
    for each row size it cuts a column

    and for that specific matrix i sent it to a function
    which calculates the sum of this matrix

    its a wrong algorithm?
    i cant check if its the right one
    because i cant input my array into the functions
    Code (Text):

    #include <stdio.h>

    int main()   { //star
       int i,j,k,temp,sum3=0,total_sum=0,inp,sum2=0;
       int rows = 50;
       int cols = 50;
       int jndex,input,counter=0;
       int matrix[50][50];
    //   int matrix[rows][cols];
    //   int sum[rows][cols];
       int sum[50][50];
    //   int temp1[rows][cols];
       int temp1[50][50];
    //   int transpose [cols][rows];
       int transpose [50][50];
       int index,kndex,tndex,gndex,lndex;
       int rows_sum[50];

     printf("enter the size of a  matrix:");
       scanf("%d %d",&rows,&cols);
       for (index = 0; index < rows; index++)
       {
           rows_sum[index]=0;
          for (kndex = 0; kndex < cols; kndex++)
          {
             matrix[index][kndex] = 0;
             sum[index][kndex] = 0;
             temp1[index][kndex] = 0;
             transpose[index][kndex] = 0;
          }//end inner for
       }//end outer for
       printf("enter numbers in a row for a matrix:"); //stat input
       for (index =0;index<rows; index++)
       {
          for (kndex =0; kndex<cols; kndex++)
          {
             scanf("%d", &matrix[index][kndex]);
             transpose[kndex][index] = matrix[index][kndex];
          }
       }
       i = getchar();  //needed because of scanf()
       //end input
    printf(" enter number to search");
    scanf("%d", &inp);


                                                      for (index=0;index<rows; index++)
                                                            {
                                                             for (kndex =0; kndex<cols; kndex++)
                                                                      {
                                                                          sum3=search(inp ,i,j,rows,cols,matrix[][]);
                                                                  total_sum=total_sum+sum3;
                                         }
                                   }

       printf("%d",&counter);
     
       return 0;

    }//end main

    int search(int input,int cor_row,int cor_col,int rows ,int cols,int* matrix){//start search func

     int i,j,sum,counter=0;


     for ( i=rows;i>0;i--){
        for ( j=cols;j>0;j--){
      sum=sum_of_matrix(cor_row, cor_col,i ,j, matrix [][]);
      if (input==sum){
      counter++;
      }
        }
     }
     return counter;
    }//end search func


    int sum_of_matrix(int cor_row,int cor_col,int rows ,int cols,int* matrix [][]){

     int i ,j;
     int sum=0;
     for (i=cor_row;i<rows;i++){
         for (j=cor_col;i<cols;i++){
       sum=sum+matrix[i][j];
         }
     }
     return sum;
    }
     
     
  2. jcsd
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Can you help with the solution or looking for help too?
Draft saved Draft deleted



Similar Discussions: Finding a sub matrix question
  1. Determinate Matrix (Replies: 4)

Loading...