+ 1
How to store inputed Array in while loop?
Since, we don't know the numbers, it is inputed by the users, so i want to store the input array numbers in while loop and do calculations.
3 Answers
+ 4
One way can be to take input from the user about how many elements are wanted in the array (you don't need to ask exactly this, but you get the point). For example, if you want to take the number of students in a class and their marks in computer science, then you can simply ask how many students there are in the class
int num_students = Convert.ToInt32(Console.ReadLine());
int[] students = new int[num_students];
for (int i = 0; i < num_students; i++) {
students[i] = Convert.ToInt32(Console.ReadLine());
}
But if you don't want to ask the number of elements, you can instead use the `List` collection present in the System.Collections.Generic namespace. It can expand and shrink in size according to your needs. There is a section in the SoloLearn course on the List collection
https://www.sololearn.com/learning/3039/
+ 2
Like XXX said. If you use an array. You are bound to fixed size. Either you choose a very large number like 100, which you know the user is never going to reach.
Or ask the number of inputs up front.
If you want to have "unlimited" inputs. Do use a list.