Answer to Question #305578 in C# for Jas

Question #305578

Write a program in C# to get the largest element of an array using a function.


Test Data:


Input the number of elements to be stored in the array: 5


Input 5 elements in the array:


element - 0: 1


element - 1: 2


element - 2: 3


element - 3: 4


element - 4: 5


Expected Output:


The largest element in the array is: 5

1
Expert's answer
2022-03-05T04:11:19-0500


using System;
using System.Collections.Generic;
using System.Globalization;


namespace App
{
    class Program
    {


        static int getLargestElementArray(int[] elements)
        {
            int largestElement = elements[0];
            for (int i = 1; i < elements.Length; i++)
            {
                if (elements[i] > largestElement) {
                    largestElement=elements[i];
                }
            }
            return largestElement;
        }


        static void Main(string[] args)
        {
            Console.Write("Input the number of elements to be stored in the array: ");
            int number = int.Parse(Console.ReadLine());
            int[] elements = new int[number];
            Console.WriteLine("Input {0} elements in the array:", number);
            for (int i = 0; i < elements.Length; i++)
            {
                Console.Write("element - {0}: ", i);
                elements[i] = int.Parse(Console.ReadLine());
            }
            int largestElement = getLargestElementArray(elements);
            Console.WriteLine("The largest element in the array is: {0} ", largestElement);


            Console.ReadLine();
        }


    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS