Thread: C++ problem w/ sums

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    3

    Question C++ problem w/ sums

    How can I use C++ to solve the following problem?

    List how many ways the numbers 9, 13, 17, 25, 31, and 35 can be added exactly 6 times to equal 100.

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Explain the problem more clearly.

    Can the numbers be used more than once?

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    3
    Yes, they can be. I thought about using an array and something with recursion, but I just can't figure it out.

  4. #4
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Attempt to write some code and then post it. It is important you understand the problem first!!

    Remember to use the code tags!!

    Mr. C.

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Think nested loops, an array and an if statement!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    3
    Thanks for the help. It wasn't as hard as I thought it would be. ^^; I've been able to come up with the following so far:

    Code:
    // points.cpp
    #include <iostream.h>
    
    int main()
    {
      int p[6] = {9,13,17,25,31,35};
      int a, b, c, d, e, f, sum=0;
    
      for (a = 0; a < 6; a++)
        for (b = 0; b < 6; b++)
          for (c = 0; c < 6; c++)
            for (d = 0; d < 6; d++)
              for (e = 0; e < 6; e++)
                for (f = 0; f < 6; f++)
    	    {
    	      sum=p[a]+p[b]+p[c]+p[d]+p[e]+p[f];
    	      
                  if (sum == 100)
    	        cout << p[a] <<" + "<< p[b] <<" + "<< p[c] <<" + "<< p[d] <<" + "<< p[e] <<" + "<< p[f] <<" = 100" << endl;
                 }
      return 0;
    }
    I have two followup questions:

    1. How can I eliminate repeats? (ie: 35+25+9+9+9+13 and 35+25+9+9+13+9, and many others)

    2. Is it possible to do the whole thing in one loop?
    Last edited by Kaoru; 09-16-2002 at 01:17 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM