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

  • Home
  • SEARCH
  • 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 6757371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:38:36+00:00 2026-05-26T13:38:36+00:00

i want to to calculate minimum sum in given two dimensional array #include<iostream> #include<limits.h>

  • 0

i want to to calculate minimum sum in given two dimensional array

#include<iostream>
#include<limits.h>
using namespace std;
#define R 3
#define C 3
int Min(int x,int y,int z){
   if(x<y){
        return (x<z)?x:z;
   }
   else
       return (y<z)?y:z;
   }
int mincost(int cost[R][C],int m,int n){

    int i,j;
    int t[R][C];
    t[0][0]=cost[0][0];
    for(i=1;i<=m;i++)
        t[i][0]=t[i-1][0]+cost[i][0];
    for(j=1;j<=n;j++)
t[0][j]=t[0][j-1]+cost[0][j];
    for(i=1;i<=m;i++){
        for(j=1;j<=n;j++){
            t[i][j]=Min(t[i-1][j-1],t[i-1][j],t[i][j-1]+cost[i][j]);
        }
    }
      return t[m][n];

}

int main(){

    int cost[R][C]={{1,2,3},
                    {4,8,2},
                    {1,5,3}};
    cout<<mincost(cost,2,2)<<endl;


    return 0;
}

from starting point (0,0) to some point (m,n) for this array it equals 8,but output shows me 1,why?what is wrong with this code?
algorithm in words

Given a cost matrix cost[][] and a position (m, n) in cost[][], write a function that returns cost of minimum cost path to reach (m, n) from (0, 0). Each cell of the matrix represents a cost to traverse through that cell. Total cost of a path to reach (m, n) is sum of all the costs on that path (including both source and destination). You can only traverse down, right and diagonally lower cells from a given cell, i.e., from a given cell (i, j), cells (i+1, j), (i, j+1) and (i+1, j+1) can be traversed. You may assume that all costs are positive integers.

  • 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-05-26T13:38:36+00:00Added an answer on May 26, 2026 at 1:38 pm

    I see that this is a dynamic programming solution.

    you have a typo here:

    t[i][j]=Min(t[i-1][j-1],t[i-1][j],t[i][j-1]+cost[i][j]);
    

    it should be:

    t[i][j]=Min(t[i-1][j-1],t[i-1][j],t[i][j-1]) + cost[i][j];
    

    basically it worked like t[i][j] = t[i-1][j-1].

    Note: A good way to debug these problems is to print the intermediate matrix (here: t).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to calculate the age of a person given the date of birth
I want to calculate the sum of 1/1 + 1/2 + 1/3 + ...
I want to calculate the angle between two vectors a and b. Lets assume
I want to calculate the difference between two times, one of which is the
Let's say I want to calculate a hash of a data structure, using a
I have a coordinate from Core Location and want to calculate the co-ordinate given
I want to calculate the maximum altitude, minimum altitude, and average altitude of the
I want to calculate the variance in a dataset collected at a given time
I want to calculate the average CPU usage % between two points of time.
I want to calculate the power of 2 using recursion. Here is my code:

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.