본문 바로가기
C#

배열 연습문제

by Mostlove 2023. 7. 6.
728x90
반응형

namespace CSharp1
{
    internal class Program
    {
        //가장 높은 값
        static int GetHightestScore(int[] scores)
        {
            int maxValue = 0;
            for (int i = 0; i < scores.Length; i++)
            {
                if (maxValue < scores[i])
                {
                    maxValue = scores[i];
                }
            }
            return maxValue;
        }
        //평균 값
        static int GetAverageScoreScore(int[] scores)
        {
            int average = 0;
            for (int i = 0; i < scores.Length; i++)
            {
                average += scores[i];
            }
            if (scores.Length < 0) return 0;

            return average /scores.Length;
        }
        //찾는 값 몇번 째 인지
        static int GetIndexOf(int[] scores, int value)
        {
            int indexNum = 0;
            for (int i = 0; i < scores.Length; i++)
            {
                if (scores[i] == value)
                {
                    indexNum = i;
                }
            }
            return indexNum;
        }
        static void Sort(int[] scores)
        {
            for (int i = 0; i < scores.Length; i++)
            {
                int minIndex = i;
                for(int j = i; j < scores.Length; j++)
                {
                    if (scores[j] < scores[minIndex])
                    {
                        minIndex = j;
                    }
                }
                int temp = scores[i];
                scores[i] = scores[minIndex];
                scores[minIndex] = temp;
            }
        }
        static void Main(string[] args)
        {
            //배열 
            int[] scores = new int[5] { 10, 30, 40, 20, 50 };
            Console.WriteLine(GetHightestScore(scores));
            Console.WriteLine();
            Console.WriteLine(GetAverageScoreScore(scores));
            Console.WriteLine();
            Console.WriteLine(GetIndexOf(scores,30));
            Console.WriteLine();
            Sort(scores);

        }
    }
}

반응형

'C#' 카테고리의 다른 글

List  (0) 2023.07.09
다차원 배열  (0) 2023.07.09
배열  (0) 2023.07.06
TextRPG(객체지향 버전)  (0) 2023.07.06
문자열  (0) 2023.07.05