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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:48:08+00:00 2026-06-14T02:48:08+00:00

I have written a code to calculate the minimum number of coins using Greedy

  • 0

I have written a code to calculate the minimum number of coins using Greedy algorithm and Dynamic algorithm, but the Dynamic algorithm part doesn’t work properly. There is a Null value going to array, I can’t find it. Please HELP me. I need a answer as soon as possible.

#include <stdio.h>
int n;
int denom[]={1,2,4,5,20,25};
int coinCounter(int n);
int main(){

     printf("Please Enter a Number : ");
     scanf("%d",&n);
    int coinmin,orin,i;
    orin=n;
    i=coinmin=0;
     for(i=(sizeof(denom)/4)-1;i>=0;i--){

         coinmin =coinmin+n/denom[i];
          n=n%denom[i];
      }

    printf("Coin Min By Greedy Algorithm : %d\n",coinmin);
    printf("Dynamic Algorithm : %d\n",coinCounter(orin));
    return 0;
}

int coinCounter(int n){
    int opt[n];
    int largest[n];
    int i,j,a;
    i=j=0;
    for(j=1;j<=n;j++){

        opt[j]=10000;
         //printf("xxn");
        for(i=(sizeof(denom)/4)-1;i>=0;i--){

            if(denom[i]==j){

                opt[j]=1;
                largest[j]=j;
            }
            else if(denom[i]<j){
                a=opt[j-denom[i]]+1;
            }
            if(a<opt[j]){
                opt[j]=a;
                largest[j]=denom[i];
            }
        }

    }
     return opt[n];

}

I edited the Code as following, but the answer is not coming

int coinCounter(int n){
    int opt[n];
    int largest[n];
    int i,j,a;
    i=j=0;
    for(j=1;j<n;j++){

        opt[j]=10000;
         printf("xxn");
        for(i=(sizeof(denom)/4)-1;i>=0;i--){

            if(denom[i]==j){

                opt[j]=1;
                largest[j]=j;
            }
            else if(denom[i]<j){
                a=opt[j-denom[i]]+1;
            }
            if(a<opt[j]){
                opt[j]=a;
                largest[j]=denom[i];
            }
        }

    }
     return opt[n-1];

}

hey these are the results I’m Getting

Please Enter a Number : 8
Coin Min By Greedy Algorithm : 3
Dynamic Algorithm : 1

Another answer I’m getting I can’t figure out what I’m doing wrong

Please Enter a Number : 71
Coin Min By Greedy Algorithm : 4
Dynamic Algorithm : 3
  • 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-06-14T02:48:09+00:00Added an answer on June 14, 2026 at 2:48 am
    int coinCounter(int n){
        int opt[n];
        int largest[n]; <---- Don't do this. This does not work like you expect it to.
    

    change to

    int coinCounter(int n){
            int opt[n];
            int *largest = malloc(sizeof(int)*n);
    

    Edit:

    Another bug with the algorithm

    your variable “a” is not initialized and you are using it in if condition.

    think of the case when denom[i]>j
    your variable “a” would not be initialized
    so depending upon what garbage value it has , results will vary
    bug is here , but it shows up when you change opt allocation, because that allocation changes the condition. What I want to say is – if (X<Y), depends on both X and Y. Problem is with X, but because you change Y, the condition changes and you get different result

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

Sidebar

Related Questions

I have written a small piece of code to calculate quadratic equations, but if
I have written a code to calculate the hardisk size, but for some reason
I have written some code that loads an XML document using an XmlDocument object
I have written some code for displaying a drop down list, but the code
I have written this JavaScript that calculate words, but I am unable to convert
I am using ImageMagick for Perl use Image::Magick; I have written functional code to
I have written a C++ code to calculate the reflection of solar radiation (based
I have written the below code to calculate mouse moved co-ordinates $(document).mousemove(function(e){ console.log(e.pageX +
I have written code that opens 16 figures at once. Currently, they all open
I have written code in Java to access web cam,and to save image... I

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.