Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C++ Algorithmn

Posted on 2004-10-22
3
Medium Priority
?
369 Views
Last Modified: 2012-05-05
Hi People, I need your help,

I would like to know where to find documentation on c++ Algorithm. For exemple, how many ways can I represent a 5 dollar bill if I have loonies, quarters, dimes and nickles ?


Documentation would be greatly appreciated.

Thank You,


C-Bin
0
Comment
Question by:C-begin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Assisted Solution

by:Crius
Crius earned 80 total points
ID: 12385358
There are many places to find various C++ algorithms.

http://www.codeguru.com is a great place. You can find the C++ algorithms page from their home page, or just follow this link: http://www.codeguru.com/Cpp/Cpp/algorithms/.

They also have a lot of sample programs and non-algorithm specific solutions for a wide variety of problems.

http://www.codeproject.com is a great palce that deals mostly with MFCs. Their algorithm section is located at http://www.codeproject.com/cpp/#Algorithms.

MSDN is another good place to find algorithms and samples. If you don't have it installed on your computer, you can go to their website at http://msdn.microsoft.com. Typing "algorithms" into their search page will bring up quite a few of them. You can of course be more specific in your searches.

Finally, try google. If you know the general type of algorithm you want, you can find it that way.
0
 
LVL 9

Accepted Solution

by:
jhshukla earned 120 total points
ID: 12386721
read the comments before count_combos function.

/*
  File: change.cpp
  Date: 08.13.2003
  Author: Jaydutt Shukla
  Purpose: This program asks for amount of money (in cents) and outputs the number of
  ways in which change can be given back for amounts <= MAX
*/

#include <iostream>
#include <iomanip>
using namespace std;

const int MAX=2000; //will calculate # of combos only if amount <= MAX. requires very deep recursion

const int denoms[]={1, 5, 10, 25, 50, 100, 200, 500, 1000, 2000, 5000, 10000}; //const array for denominations

unsigned long  count_combos(int, int=0);

int main(){
    int cents=0, copy=0;
   
    cout << "Enter amount in cents: ";
    cin >> cents;
    copy = cents;
    if(cents <= MAX){
      cout << "Most efficient change is: \n";
      if(cents ==0){
          cout << "0 x $   0.00" << endl;
          cout << "Change can be given in " << count_combos(copy) << " different ways" << endl;
          return 0;
      }
      for(int i=11; i>=0; i--){
          //setiosflags(ios::fixed);
          if(cents/denoms[i])
            cout << cents/denoms[i] << " x " << "$ "
                 << setprecision(2) << setiosflags(ios::fixed) << setw(6)
                 << denoms[i]/100.0 << endl;
          cents %= denoms[i];
      }
      
      cout << "Change can be given in " << count_combos(copy) << " different way(s)" << endl;
    }
    else
      cout << "Amount exceeds the limit." << endl;
   
    return 0;
}

/*
 Recursive function:
 Start with pennies and denom d
 counter = 1;
 Determine how many d's do you want. range = [0, amount/d). use a for loop
   recurse with remaining pennies and next higher denomination
   Add this number to counter
   Increment # of d's

 ... and there are stopping conditions.
 */
/* After you understand the working of this fcn:
   to extend this fcn for higher denominations append those to the const array denoms;
   change MAX to the max amount you want
   */
unsigned long count_combos(int amount, int d){ //d == index into denoms array
    unsigned long count = 1;
   
    if(amount > MAX) return 1;   //do not exceed MAX denomination
   
    if(amount == 0) return 1;
    if(denoms[d] > amount) //Example: there is no way you can give out 1 cent using nickels
      return 0;
    /* The only problem occurs when starting amount == 0
       should the answer be 0 or 1? I chose 1. see previous line */
   
    for(int i=0; i<amount; i += denoms[d+1])
      count += count_combos(amount - i, d + 1);
   
    return count;
}
0
 

Author Comment

by:C-begin
ID: 12425322
hey guys thank you very much,

I will try to read a bit more, for now....how do I split the points ?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Make the most of your online learning experience.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Progress

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question