C# How Can I Replicate PHP Array Addition in C#?

  • Thread starter Thread starter adjacent
  • Start date Start date
  • Tags Tags
    Array
AI Thread Summary
The discussion revolves around converting a PHP code snippet that collects multiples of 5 from 0 to 100 into C#. The original PHP code uses an array to store results, while the C# version employs the List class to achieve similar functionality. The final C# code allows user input for the limit and multiple, iterating through numbers to find and display the multiples. However, concerns are raised about the inefficiency of the code, as many iterations yield no results. Suggestions include improving the algorithm to ensure every iteration adds a useful result and considering the use of an array instead of a List. Additionally, there is a discussion about whether storing the numbers is necessary if the primary goal is simply to output them.
adjacent
Gold Member
Messages
1,552
Reaction score
62
The title.
A friend of me told me the code in php
Code:
<?php 
for($i=0; $i<=100; $i++) 
{ 
     if($i%5==0) 
     { 
          $out[] = $i; 
     } 
}  
foreach($out as $val) 
{ 
   echo $val."<br>"; 
} 
?>
The result is the multiples of 5 in the first 100.
How do I reproduce this part:"$out[] = $i; " in C#?
I have searched everywhere but couldn't find how to do that.
 
Last edited:
Technology news on Phys.org
DrZoidberg said:
Use the List class.
http://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx

Code:
List<int> out= new List<int>();
...
out.Add(i);
Oh, thank you so much. This is my final code :smile:
Code:
while (true)
            {
                int limit;
                int multiple;
                Console.Write("Limit:");
                int.TryParse(Console.ReadLine(), out limit);
                Console.Write("Multiple:");
                int.TryParse(Console.ReadLine(), out multiple);
                List<int> no = new List<int>();
                for (int i = 1; i <= limit; i++)
                {
                    if (i % multiple == 0)
                    {
                        no.Add(i);
                    }
                }

                foreach (object o in no)
                {
                    Console.WriteLine(o);
                }
            }
 
This code is inefficient. Out of many iterations of the loop, only a small number produce a useful result. Can you improve that, ideally so that every iteration results in a addition to the list?

Further, can you use an array, as you seem to have wanted initially, rather than a list?

Finally, if the goal is to output the numbers, do you really need to store them?
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top