tags:

views:

794

answers:

11

I got a homework assignment today: "Create a program that calculates out how many ways you can add three numbers so that they equal 1000."

I think this code should work, but it doesn't write out anything.

using System;

namespace ConsoleApp02
{
    class Program
    {
        public static void Main(string[] args)
        {
            for(int a = 0; a < 1000; a++)
            {
                for(int b = 0; b < 1000; b++)
                {
                    for(int c = 0; c < 1000; c++)
                    {
                        for(int puls = a + b + c; puls < 1000; puls++)
                        {
                            if(puls == 1000)
                            {
                                Console.WriteLine("{0} + {1} + {2} = 1000", a, b, c);
                            }
                        }
                    }
                }
            }
            Console.ReadKey(true);
        }
    }
}

What am I doing wrong? Any tips or solution?

+15  A: 

Your innermost loop (iterating the puls variable) doesn't really make any sense, and because of the condition on it (puls < 1000) Console.WriteLine never runs.

Perhaps you should test whether A + B + C is 1000, instead.

Also, you'll find that you might be missing a couple particular combinations of numbers because of the bounds on your loops (depending on the problem statement.)

mquander
+1 for helping but not doing his homework for him.
Cj Anderson
Thanks! I tried to word my answer thoughtfully.
mquander
OK, I'll bite, what combinations are missing? I'm assuming only positive integers for A, B, and C.
Bob King
He didn't really make it clear whether 0 was allowed or not, so they might not really be missing. I qualified my response as such.
mquander
Yeah, perhaps natural numbers?
codemeit
Since this was from February, I don't mind saying that the inner loop not only makes no sense, it's flat out bad logic. Suppose I have a,b,c of 996,1,2. puls = 999. Fine. Suppose the loop bound were fixed, and puls++ set puls to 1000. now puls==1000. Print a,b,c as correct result: 996,1,2. But clearly, a+b+c != 1000. Yes, for future readers, make sure you check for actual solutions. i.e. as mquander says, check that a+b+c==1000.For every statement you should be able to answer: what am i doing, why am i doing it, what does it accomplish?
maxwellb
+1  A: 

You don't need the inner loop.

if (a+b+c == 1000)
   write
Coltin
A: 

Once the inner most loop gets to 1000, it breaks out of the for loop and never even checks if it is 1000 in the 'if' statement.

Dano
A: 

Your homework so just a hint. What is going on in the final for loop? Is that really what you want to do?

A: 

That code will return no answer.

The interior for loop adds a + b + c and puts the result into puls. However, you stop the loop before puls can get to 1000, and then test inside the for statement to see if puls equals 1000. So, you won't get an answer.

Just test puls against 1000 in the inner loop. Why?

mmr
A: 

You can replace your innermost for loop with just a test for if (a+b+c == 1000) {...}. Then you can add optimizations like - once a sum has been found with a combination, there is no need to continue with the inner loop.

Mohit Chakraborty
+1  A: 

In your final inner loop, "int puls = a + b + c; puls < 1000; puls++" you're ensuring that puls never = 1000, if puls is not less than 1000, it kicks out of the loop. That is why you are getting no values. But rethink your logic some as well.

Timothy Carter
A: 

The code does not cover the following cases

  • 1000 + 0 + 0
  • 0 + 10000 + 0
  • 0 + 0 + 10000

The inner most loop should be an if rather for.

You don't need {} if there is only one statement in the loop or if clause.

EDIT: Removed code answers

codemeit
Please, oh please, don't omit the {}. ;)
hometoast
because I'm lazy when I want to add another line.
hometoast
+5  A: 

On a separate note, this particular implementation, while it'll work (with the modifications suggested by the other answers), is quite a performance hit, as the complexity of your algorithm is O(n^3). In other words, you are going through the innermost check one bilion times.

Here's a hint how you can optimize it to at least O(n^2) or just one milion iterations: for each pair of a and b generated by the two outer for loops, there's only one value for c that will result in 1000.

Franci Penov
Also worth noting is that if a + b + c = 1000, then b + a + c also equals 1000. So you can probably cut out a lot of values by only checking for values of b that are >= a.
Kibbee
Lol, I didn't want to give him all the hints, just to nudge his mind in the right direction... :-)
Franci Penov
+1  A: 

If you get this assignment as a computer science student, you'd probably want to solve this using Dynamic Programming.

norheim.se
+2  A: 

Question doesn't specify that negative numbers are not allowed. Answer is infinite.

Peter Drier