Resolved why doesn't work?

salih

New member
Joined
Sep 12, 2019
Messages
2
Programming Experience
Beginner
I wanted to print the numbers where t, p, and h were equal, but the program print all t numbers.
Where is my fault ?
thanks for your future answers and sorry for my english.
C#:
 static void Main(string[] args)
        {
            ulong num = 0, k = 0,s=0;
            ulong[] tnum = new ulong[10000];
            ulong[] pentnum = new ulong[10000];
            ulong[] hexnum = new ulong[10000];
            ulong[] sonuc = new ulong[10];
            while (pentnum[9999] == 0)
            {
               
                num++;
                tnum[k] = num * (num + 1) / 2;
                hexnum[k] = num * (3 * num - 1) / 2;
                pentnum[k] = num*(2 * num - 1);
                k++;
             

            }
        
            foreach (ulong t in tnum)
            {
               
                foreach (ulong h in pentnum)
                {
                    if(t == h)
                    {
                        foreach (ulong p in pentnum)
                        {
                            if(t == p && t == h)
                            {
                               Console.WriteLine(t);
                               
                            }
                        }
                    }
                }
               
            }
         
           
            Console.Read();
        }
 
You need to debug your code. Set a breakpoint at the top of the code and step through it line by line, examining the state at each step. As soon as the state doesn't match your expectations, you've found an issue and you can examine that in more detail. That's the point at which you would ask for help, when you can explain the specific problem. If you never find a state that doesn't match expectations but end up with the wrong result then it's your expectations that need further examination.
 
I think it be more helpful if you explain line by line what your code is meant to do, as opposed to what it is doing? I like to comprehend, that your code is not doing what you think it is doing, hence the question.

What's the purpose of your nested loops?

Why do you loop foreach (ulong h in pentnum) on line 23 and again on line 27 foreach (ulong p in pentnum)?

but the program print all t numbers.

It doesn't print all t numbers. It prints all the t numbers where t equals the iterated indexed number of pentnum. As you can see, h does not contain 3, 10, 21, 36 etc in pentnum. Only pentnum numbers are printing because they meet the conditions to evaluate your statement of t == h. If you expect a desired and alternative result, I suggest doing as stated above and step into your debugger as I've done and study what your logic is doing, as-too ask a more direct question.

Screenshot_18.jpg
Screenshot_19.jpg

Edit : fixed a typo
 
Last edited:
Sometimes it only takes an extra pair of eyes to evaluate what you are doing. So if its safe to assume you have now resolved your issue, you can and should be able to mark your thread as resolved.
 
Back
Top Bottom