This code how to find the smallest number.
My question is now how to find the largest number?

int iSmallest = int.MaxValue;
            int iSmallestLocation = 0;      
            int[] iArray = new int[5];      


            for (int i = 0; i < iArray.Length; ++i)
            {
                Console.Write("\nEnter a value: ");
                iArray[i] = int.Parse(Console.ReadLine());
            }

            Console.WriteLine();
            foreach(int iValue in iArray)
                Console.Write("{0}, ", iValue);

            for (int i = 0; i <
                iArray.Length; ++i)
            {

                if (iArray[i] > iSmallest)
                {
                    iSmallest = iArray[i];

                    iSmallestLocation = i;
                }
            }

            Console.WriteLine("\nThe lowest number is at location {0} and is {1}",
                iSmallestLocation, iSmallest);

            Console.ReadKey();

Recommended Answers

All 4 Replies

int[] iArray = new int[5];

for (int i = 0; i < iArray.Length; ++i) {
    Console.Write("\nEnter a value: ");
    iArray[i] = int.Parse(Console.ReadLine());
}

Console.WriteLine("Maximum value is {0}", iArray.Max());
Console.WriteLine("Minimum value is {0}", iArray.Min());

If you need to do it the way you are, just set iLrgest to Int32.MinValue, and compare iArray < iLargest.

how to put random number instead of putting a number in line 9 of my code...

i know that i need this:

Random rNumber = new Random;

Random r = new Random();
for (int i = 0; i < iArray.Length; i++) {
    iArray[i] = r.Next();
}

This puts a number from 0 to Int32.MaxValue-1 into the array.

this is what i end up with...
how to do a reverse without using an Array.Reverse method (line 22)

Console.WriteLine("{0: 40}", "ICA21 - Arrays");

            int[] iArray = new int[20];
            Random rNumber = new Random(101);
            int iLargest = Int32.MinValue;
            int iLLocation = 0;

            Console.WriteLine("\nHere are the grades...\n");
            for (int i = 0; i < iArray.Length; ++i)
            {
                Console.Write("{0}, ", i);
                iArray[i] = rNumber.Next(101);
            }

            Console.WriteLine();
            foreach (int iValue in iArray)
            {
                Console.Write("{0}, ", iValue);
            }

            Console.WriteLine("\n\nHere are the grades in reverse...\n");
            for (int i = iArray.Length; i < 0; ++i)
            {
                Console.Write("{0}, ", i);
                iArray[i] = rNumber.Next(101);
            }
            for (int i = 0; i < iArray.Length; ++i)
            {
                if (iArray[i] > iLargest)
                {
                    iLargest = iArray[i];
                    iLLocation = i;
                }
            }

            Console.WriteLine("\nThe highest grade is {0} at location {1}", iLargest, iLLocation);

            Console.ReadLine();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.