0

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();
2
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by josephP
Featured Replies
  • 1

    [code]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());[/code] If you need to do it the way you are, just set iLrgest to Int32.MinValue, and compare … Read More

  • 1

    [code]Random r = new Random(); for (int i = 0; i < iArray.Length; i++) { iArray[i] = r.Next(); }[/code] This puts a number from 0 to Int32.MaxValue-1 into the array. Read More

1
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.

0

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;

1
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.

Edited by Momerath: n/a

0

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();
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.