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 4322136
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T08:50:06+00:00 2026-05-21T08:50:06+00:00

This program should return the weight of the shortest path from left to right(it

  • 0

This program should return the weight of the shortest path from left to right(it also can go over the top and over the bottom, so it’s like a horizontal cylinder) in two-dimentional array.(here is a full question_link)
I’m trying to check recursively by first going up,then right and finally down in the array.
By running this program I’m getting “Segmentation fault” if I uncomment the lines right direction and bottom direction.
If anyone can tell me what I’m doing wrong in my recursive function. Thanks in advance!

#include<iostream>
using namespace std;

int rec_path(int matrix[5][6], int r, int c){
static int sum = 0;
static int weight = -1;
    if (r == -1) 
    r = 4;

if (r == 5) 
    r = 0;

if (c == 6) {
    return weight;
    sum = 0;
}
//calculate sum 
sum += matrix[r][c];    
//check the up direction
rec_path(matrix, --r, ++c);
//check the right direction
//  rec_path(matrix, r, ++c);
//check the bottom direction
//  rec_path(matrix, ++r, ++c);
if (weight == -1) 
    weight = sum;
if ( weight < sum) {
    weight = sum;
}
}


int main(){
const int row = 5;
const int col = 6;
int matrix[row][col] = {{3,4,2,1,8,6},
                        {6,1,8,2,7,4},
                        {5,9,3,9,9,5},
                        {8,4,1,3,2,6},
                        {3,7,2,8,6,4}
                        };

cout << rec_path(matrix,0,0) << endl;
return 0;
}
  • 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-21T08:50:07+00:00Added an answer on May 21, 2026 at 8:50 am

    Here you go. This will just return the cost of the path, finding the actual path is just
    a simple modification of this.

    int rec_path(int matrix[5][6],int r,int c,int cost)
    {
        if(c==6) return cost;
        int ans=2e9;
        static const int dr[]={-1,0,1};
        for(int i=0;i<3;i++)
            ans=min(ans,rec_path(matrix,(5+(r+dr[i])%5)%5,c+1,cost+matrix[r][c]));
        return ans;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.