-
I don't immediately understand your algorithm description. I will ponder it while I attend to some chores for XMas.
Did your instructional material or instructor mention what algorithm they wanted used?
Would you post it, in English? That would be a good starting point.
Would you edit your program post in this area I've shown, and move it over to someplace near the left side of the page?
Right now, it "breaks the display width tables", for the forum. I can't even see all of it without constantly scrolling back and forth, when they're like this.
Code:
//Something like this would be good:
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;
}
}
-
i tried to change the code again
Code:
#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;j<cols;j++){
sum=sum+matrix[i][j];
}
}
return sum;
}
-
i alligned to the left
is that ok now?
i wasnt given an algorithm
Code:
#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;j<cols;j++){
sum=sum+matrix[i][j];
}
}
return sum;
}
-
-
Ok, in this matrix, would just 1, 4, 3 (a triangular shaped sub domain), be an answer if the search was for 8 sum?
0100
4300
0000
Or, would the answer be 0, 1, 4, 3 ?
In other words, can the answer be an irregular shaped sub matrix, or just a rectangular or square shaped one?
Code looks much better. Be careful with that, because it alienates a lot of people on the forums.
-
the answer is how many square or rectangle matrices exist
which the sum of their cells equals the input number
-
First, let me say that I don't know the standard answer (if there is one), to this problem/puzzle. So my posts may be right (probably close), but may be quite off, sometimes.
Check for rectangles/squares. This is the toughie:
Starting from our chosen square, sum up the squares to your right and below, only. You don't need to check squares above you, or to your left - they will already have been found by previous checks. These checks *must* include a downward *and* a right column check to them. All others possible checks, will already have been made.
So you have to go 1 sqr to the right, then 1 sqr down, then 1 square left (right below the starting square now), and back to the starting square. (sqr = square). 4 sqr's in all.
Continue like this, adding 1 to the sqr's on the rows until your sum > target, or you reach the end of the row. Then add one more row, and repeat the above, until you have no more rows whose sqr's sum to <= target value.
eg:
1,1 (one right, and 1 down, one back, and the original sqr) - 4 sqr's total
**
**
1,2: (one right, two down - 6 sqr's total
**
**
**
1,3:
**
**
**
**
2,1 (two right, 1 down, 1 back left, 1 below, and the original sqr), 6 sqr's total
***
***
2,2:
***
***
***
etc.
3,1:
****
****
etc.
Each sequence can be broken out of when their sum > target value.
Two for loops would be a great place to start:
In the above, I see the blue digit as a row, and the red one, as the column.
Then in this code, you'd have to add just your extra row or column logic needed for this
puzzle.
Code:
for(row = sqr's row; row < maxrows; row++) {
for(col = sqr's col; col < maxcols; col++) {
sum += matrix[row][col];
if(sum > target)
break;
}
}
//sqr's row or col is the starting sqr's row or starting sqr's column
The above would find every possible sub matrix, including the row and column check, but may need some extra code for it. Good starting place, though.
-
i wrote the code that you described
i entered the example which i was given and instead of 9 i get 0.
i marked the code b comments
"search code"
"end search code"
??
Code:
#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);
//search code
for(i =0; i < rows; i++) {
for(j = 0; j <cols; cols++) {
sum2 += matrix[i][j];
if(sum > inp){
break;
}
else
{
if (sum==inp){
counter++;
}
}
}
}
printf("%d",counter);
//end search code
return 0;
}//end main
-
That code was meant as a guideline, and not actual "paste it in verbatim" code for your program.
The important thing is to see if that set of for loops can "rough" it in. We can give it a tweak or two later.
One thing we need to do is to save the sub's we find, so we know exactly what the program is doing right or wrong.
I'll give it a look see, in a few minutes. Won't be much though - Santa won't wait. :)
-
i tried to build this code
but i need to put an array in a function
it not working
Code:
#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;
}
-
This is my start on the SubMatrix program. Obviously, it's not done, and won't run as is.
I set the target value for 5, but you can make it anything you want, later.
I'll let you think about it, and work on it more, later today/tonight. Xmas calls.*
Code:
/* SubMatrix.c Find all sub matrices that meet a specified target sum
Adak, December, 2008
*/
#include <stdio.h>
#define rows 3
#define cols 3
int sub[25] = { 0 };
int m[rows][cols] = {
//Columns Rows
//0 1 2
//==================
{ 1,4,1 }, //0
{ 5,0,1 }, //1
{ 3,5,3 } }; //2
/* our fives are:
1) 00 01, 2) 01 02, <02 01> 3)02 12 13 First Row Initial Square
4) 10 5) 10 11, 6) 11 21 Second Row " "
7) 21 Third Row " "
I want this matrix because it's smaller, and has more complex sub matrices, imo.
*/
int main(void) {
int i, j, k, n, r, c, sum, trgt, counter; //trgt == target
//Print the original matrix
printf("\n\n Original Matrix: \n");
printf(" ================ \n");
printf(" %d %d %d \n", m[0][0],m[0][1],m[0][2]);
printf(" %d %d %d \n", m[1][0],m[1][1],m[1][2]);
printf(" %d %d %d \n", m[2][0],m[2][1],m[2][2]);
sum = k = 0;
trgt = 5;
for(i =0, counter = 0; i < rows; i++) {
for(j = 0; j <cols; j++) {
sum += m[i][j];
sub[k] = i; //track where we've been-row and column
sub[++k] = j;
if(sum > trgt){
sum = 0; //start a new track list of row and column
k = 0;
break;
}
else
{
if (sum==trgt) {
counter++;
printf("\n %d: %d%d ", counter, i, j);
sum = 0;
sub[++k] = i;
sub[++k] = j;
//now print the entire sqr's list in sub[] - then reset it to all zero's
//********** Working Here ******************************
for(n = 0; n <= k; n++)
//etc.
k = 0;
}
}
}
}
printf("\n\n Program Complete - Press Enter to Continue ");
i = getchar();
return 0;
} //end of main
*If you think Xmas takes the Christ out of Christmas, you don't know about the origins of Xmas.
-
i get an output 1: 01
01 is square which equals 5
there are many other possibilities to get 5 in this matrix
if i will change the search value to anything.
and
set m[rows][cols] to be 50 X50
will it work
what things i need to do on this program in order to complete it?
-
i tried to fix my code
but it doesnt even end
it just dont move
??
Code:
#include <stdio.h>
int main() { //star
int ia ,ja;
int suma=0;
int cor_row;
int cor_col;
int total_sum;
int ii,jj,ssum,ccounter=0;
int i,j,k,temp;
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", &input);
for (index=0;index<rows; index++)
{
for (kndex =0; kndex<cols; kndex++)
{
for ( ii=rows-1;ii>=0;ii--){
for ( jj=cols-1;jj>=0;jj--){
for (ia=index;ia<ii;ia++){
for (ja=kndex;ja<jj;i++){
suma=suma+matrix[ia][ja];
}
}
if (input==suma){
counter++;
}
}
}
total_sum=total_sum+counter;
}
}
printf("%d",&counter);
return 0;
}//end main
-
This program is a long way from being finished. I've never programmed for this problem before.
There are bit field tricks that can work nicely on this, but I'd have my work cut out for me to do that type of program, and I doubt you could use it.
This is the latest incarnation of the program. This part *only* is trying to solve across each row, for now. not the columns or other rectangles/squares.
The first error I see is the failure to "bridge" over to squares with zero's, and consider them as a part of the solution, when the sum is right. I know why that is, but ran out of time to fix it.
You're welcome to give it a shot!
Code:
/* SubMatrix.c Find all sub matrices that meet a specified sum
Adak December, 2008
*/
#include <stdio.h>
#define rows 3
#define cols 3
int sub[25] = { 0 };
int m[rows][cols] = {
/*Columns Rows
//0 1 2
//==================
*/
{ 1,4,1 }, //0
{ 5,0,1 }, //1
{ 3,5,3 } }; //2
/* our fives are:
1) 00 01, 2) 01 02, <02 01> 3)02 12 13 First Row Initial Square
4) 10 5) 10 11, 6) 11 21 Second Row " "
7) 21 Third Row " "
I want this matrix because it's smaller, and has more complex sub matrices, imo.
*/
int main(void) {
int i, j, k, n, r, c, sum, trgt, counter; //trgt == target
//Print the original matrix
printf("\n\n Original Matrix: \n");
printf(" ================ \n");
printf(" %d %d %d \n", m[0][0],m[0][1],m[0][2]);
printf(" %d %d %d \n", m[1][0],m[1][1],m[1][2]);
printf(" %d %d %d \n", m[2][0],m[2][1],m[2][2]);
sum = k = 0;
trgt = 5;
for(i = 0, counter = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
sum += m[i][j];
sub[k++] = i; //track where we've been-row and column
sub[k++] = j;
if(sum > trgt){
sum = 0; //start a new track list of row and column
if(k > 2)
--j;
k = 0;
continue;
}
else if (sum==trgt) {
counter++;
printf("\n %d: ", counter);
sum = 0;
//now print the entire sqr's list in sub[], up to k
for(n = 0; n < k; n += 2)
printf(" %d%d", sub[n], sub[n+1]);
printf("\n");
if(k > 2)
--j;
for(n = 0; n < k; n++)
sub[n] = 0;
k = 0;
} //end of else if sum == trgt
//else
} //end of j < cols
sum = 0; //handles row changes
k = 0;
} //end of i < rows
printf("\n\n\t Program Complete - Press Enter to Continue ");
i = getchar();
return 0;
} //end of main
-
Until we get this program up and working right, it isn't a good idea to try and include it in your bigger program - that will just result in hurting your bigger program for no gain.
I'm thinking there's an easier way to do this whole program, and still leave it understandable for you.
Have patience and we'll get there, if possible. :)