Click here to Skip to main content
14,669,657 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi all!

static void Main(string[] args)
        {
            int[] numbers_in_array = { 5, 7, 4, 3, 2 };
            Console.WriteLine(FunctionReturnMinimumValue(numbers_in_array));
            Console.ReadLine();
        }
        static int FunctionReturnMinimumValue(int[] matrix)
        {
            int minimum = matrix[0];
            for (int i = 0; i < matrix.Length; i++)
            {
                if (matrix[i] < minimum)
                    minimum = matrix[i];
            }
            return minimum;
        }


what i need here is to write numbers into array
i know how to do it with pre defined numbers like in this example, but
when i need to enter numbers into console that is the problem. :)

Dont know how to do it,
can anyone plz help me with this.
Thx
Posted

Rate this:
Please Sign up or sign in to vote.

Solution 2

Probably this can help. Its a snippet that creates an array depending on what you set on your count variable. And from there it loops (count) times to accept an input and adds it to the array.

string x;
int count = 5;
int[] yourArray = new int[count];
for (int i = 0; i < count; i++)
{
   x = Console.ReadLine();
   yourArray[i] = Convert.ToInt32(x);
}
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

I would prefer to use a List<int> rather than an array. Try this little Console app

class Program
    {
        static void Main(string[] args)
        {
            List<int> numbers = new List<int>();
            bool IsDone = false;
            do
            {
                Console.Write("Enter a number to add to the list, else enter any letter to continue: ");
                int numberToAdd = 0;
                bool result = int.TryParse(Console.ReadLine(), out numberToAdd);
                if (result)
                {
                    numbers.Add(numberToAdd);
                }
                else
                {
                    IsDone = true;
                }
            } while (IsDone == false);
            Console.WriteLine("The Minimum Value in your list = {0}", FunctionReturnMinimumValue(numbers));
        }
        static int FunctionReturnMinimumValue(List<int> matrix)
        {
            int minimum = matrix[0];
            for (int i = 0; i < matrix.Count - 1; i++)
            {
                if (matrix[i] < minimum)
                    minimum = matrix[i];
            }
            return minimum;
        }
    }


Hope this helps
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

try this..,

this code may helps you

string strarray = "1,2,3,4,5,6,7";
string[] splitarr = strarray.Split(',');
Int32[] intarr = new Int32[splitarr.Length];
for (int innlop = 0; innlop < splitarr.Length; innlop++)
    intarr[innlop] = Convert.ToInt32(splitarr[innlop]);



user need to enter values with a delimiter(",")

thanks
   
Comments
blink_Lom 28-Apr-11 3:59am
   
thx...this helped a lot
Groulien 28-Apr-11 4:01am
   
If this solution worked for you, please use the 'Accept as solution' button.
Rajesh Anuhya 29-Apr-11 0:22am
   
how it's down voted??
walterhevedeich 29-Apr-11 0:41am
   
Voted 5 to counter.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100