Click here to Skip to main content
15,895,777 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
If row i is an integer multiple of row j, then you have
to output
p#q where p is the minimum of (i,j) and q is the maximum of (i,j).

If there are multiple possibilities for i and j, you have to print for the
smallest i and the smallest j.


Otherwise, you have to output
0#0

please,I recommend to solve in C language compiler because compiler gives so many errors

What I have tried:

this program has so many errors
// C# program to compute submatrix
// query sum in O(1) time
#include<stdio.h>
static int M = 4;
static int N = 5;
     
    // Function to preprocess input mat[M][N].
    // This function mainly fills aux[M][N]
    // such that aux[i][j] stores sum of
    // elements from (0,0) to (i,j)
    static int preProcess(int [,]mat, int [,]aux)
    {
        // Copy first row of mat[][] to aux[][]
        for (int i = 0; i < N; i++)
            aux[0,i] = mat[0,i];
         
        // Do column wise sum
        for (int i = 1; i < M; i++)
            for (int j = 0; j < N; j++)
                aux[i,j] = mat[i,j] + aux[i-1,j];
         
        // Do row wise sum
        for (int i = 0; i < M; i++)
            for (int j = 1; j < N; j++)
                aux[i,j] += aux[i,j-1];
                 
        return 0;
    }
     
    // A O(1) time function to compute sum
    // of submatrix between (tli, tlj) and
    // (rbi, rbj) using aux[][] which is
    // built by the preprocess function
    static int sumQuery(int [,]aux, int tli,
                        int tlj, int rbi, int rbj)
    {
        // result is now sum of elements
        // between (0, 0) and (rbi, rbj)
        int res = aux[rbi,rbj];
     
        // Remove elements between (0, 0)
        // and (tli-1, rbj)
        if (tli > 0)
            res = res - aux[tli-1,rbj];
     
        // Remove elements between (0, 0)
        // and (rbi, tlj-1)
        if (tlj > 0)
            res = res - aux[rbi,tlj-1];
     
        // Add aux[tli-1][tlj-1] as elements
        // between (0, 0) and (tli-1, tlj-1)
        // are subtracted twice
        if (tli > 0 && tlj > 0)
            res = res + aux[tli-1,tlj-1];
     
        return res;
    }
     
    // Driver code
    public static void Main ()
    {
        int [,]mat = {{1, 2, 3, 4, 6},
                      {5, 3, 8, 1, 2},
                      {4, 6, 7, 5, 5},
                      {2, 4, 8, 9, 4}};
                         
        int [,]aux = new int[M,N];
         
        preProcess(mat, aux);
         
        int tli = 2, tlj = 2, rbi = 3, rbj = 4;
         
        Console.Write("\nQuery1: " +
                      sumQuery(aux, tli, tlj, rbi, rbj));
         
        tli = 0; tlj = 0; rbi = 1; rbj = 1;
         
        Console.Write("\nQuery2: " +
                      sumQuery(aux, tli, tlj, rbi, rbj));
         
        tli = 1; tlj = 2; rbi = 3; rbj = 3;
         
        Console.Write("\nQuery3: " +
                      sumQuery(aux, tli, tlj, rbi, rbj));
    }
}
Posted
Updated 15-Apr-22 0:39am
Comments
Graeme_Grant 15-Apr-22 3:05am    
This reads like a mix of C# & C code ... both are not the same so copy & paste will produce multiple errors. I doubt very much that the code will compile.

You need to start from scratch and not plagerise other people's code. Then once you have done that, and you get the code to compile and have questions, then post the updated code and question and we will try and point you in the right direction.

This is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be "good code" in the target language – they are based on very different frameworks, and what makes something work in one language does not always "translate" directly into another.
So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it'll be a nightmare to debug if it doesn’t work "straight out of the box".
Instead, use the source code as a specification for a new app written in and for the target language / framework and write it from scratch using the original as a "template". You will get a much, much better result that will save you a lot of time in the long run.

Stop wasting your time finding "similar" code in a different language and assuming that it'll easily translate to C and you can hand it in as your own work and try doing it for yourself: this isn't a complex task at all and it's set you get you thinking about how to solve problems for yourself.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
The comment already indicates that the program originally from
another language (C#). Although there are many keywords as well
in C you have to consider what they mean and whether they make sense.

When working with matrices, it makes a lot of sense to look at the matrix more often
take a look to check all the steps. It's best to write one
function for it.

As long as the dimensions are static in C it is easy to attach a matrix to a
to hand over the subprogram. Should one be in need of a dynamic solution
to consider whether it would not be better to implement this with C++.

The code should look something like this in C:

C
#include<stdio.h>

// static int M = 4;
const int M = 4;
// static int N = 5;
const int N = 5;

int preProcess(int mat[M][N], int aux[M][N]);

void showmat(int mat[M][N])
{
   for (int i = 0; i < M; i++) {
      for (int j = 0; j < N; j++) {
         printf("%3d ", mat[i][j]);
      }
      puts("");
   }
}


// Start here
//public static void Main()
int main()
{
   int mat[M][N] = { 
      { 1, 2, 3, 4, 6 }, 
      { 5, 3, 8, 1, 2 }, 
      { 4, 6, 7, 5, 5 }, 
      { 2, 4, 8, 9, 4 } };
   int aux[M][N];

   showmat(mat);

   preProcess(mat, aux);

...

return 0;
}
 
Share this answer
 
Why don't you try to write your own algorithm for that. It is definitely a simple task.
 
Share this answer
 
Comments
merano99 15-Apr-22 6:10am    
The author seems to be very unclear here: The task was actually "You are given a 3x3 matrix", but it was
declared differently here. How the syntax of C works is also extremely unclear. Under such conditions it's
going to be a challenge. Nothing is said about the mathematical background.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900