+ 2

List and for loop integration

How Can you populat the content of the list automatically. For example, a FOR LOOP will print out a list of items until the condition is met. Can use the for loop content to populate the list.

8th May 2018, 7:10 PM
Carlos M. Saurith
Carlos M. Saurith - avatar
11 Respostas
+ 2
var list = new List<int> { 1, 2, 3, 6, 10, 1, 5, 1 }; var lastItem = list.Last(); Console.WriteLine({0}, lastItem); This list was manually populated. So the for loop will print out a given set of numbers e.i. `Int numOne = 10; for (int i = 0; i < numOne; i++) { Console.WriteLine(i); }` i will therefor = to 0 1 2 3 4 5 6 7 8 9 so the question is can I use the data from the FOR LOOP in the list?
9th May 2018, 4:38 AM
Carlos M. Saurith
Carlos M. Saurith - avatar
+ 1
If you want to use a for-loop to populate a list. You need to know how many inputs the user want to give. Most of the time this is not a problem because you can expect the user to know how many inputs there will be. or the source is something that can be counted. Have a look at my code in the playground. https://code.sololearn.com/ccb0zeEet0jB
10th May 2018, 8:40 PM
sneeze
sneeze - avatar
+ 1
This is a very interesting solution to the problem but... would this be more demanding on the power as it would have to create a list of numbers to then match the condition. What would be another theory to this ? Perhaps instead of a list just create a mathematical equation that would out put the solution?
13th May 2018, 4:39 PM
Carlos M. Saurith
Carlos M. Saurith - avatar
0
Can you give an example ? Your question is unclear to me.
8th May 2018, 8:53 PM
sneeze
sneeze - avatar
0
You mention that the input must be predefined, the user only has to enter one number and that's all. but the FOR LOOP, there is a set sequence that is required. for( int i=100; i>userinput ; i=+200); so for example: userinput = 600 output as a list = 100, 300, 500 so would this still work with your code?
11th May 2018, 5:10 AM
Carlos M. Saurith
Carlos M. Saurith - avatar
0
Yes you can Notice that the three parts of a for-loop all have their own role. https://code.sololearn.com/cTtqG2Wv05ZT For (initial_number, stop_statement, stepsize) These three statement can be defined independent. Initial_number is the number where the counting start with stop_statement : if this statement turns true the loop stops stepsize : defines the increment of the counter defined in the first statement.
11th May 2018, 9:20 PM
sneeze
sneeze - avatar
0
What was the original question? I did not understand what you are trying to achieve.
13th May 2018, 4:43 PM
sneeze
sneeze - avatar
0
😅 there is a pattern of numbers for example 6,12, 18, 24, 30.... the use would input a number e.g. 10. the loop would carry on till it hits 12 because it is the closest, the user should be able to input any number and find a match or something close to it within the condition/loop. There is a list of numbers and the user wants to see if the number that they inputed in is in the list and it if not what is the closest number in that list to it
13th May 2018, 5:35 PM
Carlos M. Saurith
Carlos M. Saurith - avatar
0
Where is the pattern defined ? by the user OR in code. Is the pattern a calculation or predefined array ?
13th May 2018, 5:46 PM
sneeze
sneeze - avatar
0
The list is predefined by the code so that is the reason for the FOR LOOP it creates a list with the conditions already set.
13th May 2018, 5:50 PM
Carlos M. Saurith
Carlos M. Saurith - avatar
0
https://code.sololearn.com/c4egAqR8AnsH Hope I did understood what you want to do. This could also be done using a while-loop
13th May 2018, 7:59 PM
sneeze
sneeze - avatar