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

The Archive Base Latest Questions

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

I’m writing a program in C to do a simple dynamic programming algorithm where

  • 0

I’m writing a program in C to do a simple dynamic programming algorithm where you return the minimum number of coins needed to add up to a certain amount. Here’s my code:

#include <limits.h>
#include <stdlib.h>
#include <stdio.h>

/*
This function returns the minimum number of stamps required for a given value.
It assumes that the given array contains the available stamp sizes, and that it
always contains 1, so a solution is always possible
*/
int min_number_of_stamps(const int* array, size_t array_size, int request) {

    /* Construct a table with dimensions (array_size+1)*(request+1) */ 
    int numRows = array_size + 1;
    int numCols = request + 1;
    int **DPtable; 
    DPtable = malloc(numRows*sizeof(int));
    int i;
    for (i = 0; i < numRows; i++) {
        DPtable[i] = malloc(numCols*sizeof(int));
    }
    printf("%d",DPtable[4][0]);
    int r, c, useIt, loseIt;
    for (r = 0; r < numRows; r++) {
        for (c = 0; c < numCols; c++) {
            printf("%d,%d\n", r, c);
            if (c==0) {
                printf("1\n");
                //if the amount of change is 0, 0 coins are needed 
                DPtable[r][c] = 0;
            } 
            else if ((r==0) || c < array[r-1]) {
                printf("2\n");
                //if there are no coins or if the change needed is less than 
                //the smallest coin available, then 'infinity' coins are needed
                DPtable[r][c] = INT_MAX;
            } 
            else { 
                printf("3\n");
                useIt = DPtable[r][c-array[r-1]] + 1;
                loseIt = DPtable[r-1][c];
                if (useIt <= loseIt) { 
                    //if 'use it' requires fewer coins than 'lose it,' then 
                    //'use it' coins are needed. 
                    DPtable[r][c] = useIt;
                } 
                else { 
                    //if 'lose it' requires fewer coins, 'lose it' coins are needed 
                    DPtable[r][c] = loseIt; 
                }
            }
        }
    }

    return DPtable[numRows][numCols];

}

int main() {
    const int array[] = {1,5,10,25};
    const int* stamps = &array[0];
    printf("%d", min_number_of_stamps(stamps, 4, 44));
}

I’m getting a segfault when my inner for loop gets to the case where r=4 and c=0. I left my debugging print statements in because I’m lazy, but you can see where I got stuck. If I access the same place in the array outside of my for loops, there’s no problem. But in the for loop, I get a `Segmentation fault: 11′ message after it outputs “4,0” for the array element and “1” for the if case it’s in. Can anyone see what I’m missing?

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

    You’re allocating dpTable incorrectly. It should be

    DPtable = malloc(numRows*sizeof(int*));
    

    See if that fixes the problem.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am writing an app with both english and french support. The app requests
I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string

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.