Click here to Skip to main content
15,868,093 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
Hi all!

C#
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

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.

C#
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);
}
 
Share this answer
 
I would prefer to use a List<int> rather than an array. Try this little Console app

C#
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
 
Share this answer
 
try this..,

this code may helps you

C#
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
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900