전체 코드

using System.ComponentModel;
using System.Numerics;
using System.Threading;

namespace CSharp
{
    
    class Program
    {
        static int GetHighestScore(int[] scores)
        {
            int maxValue = 0;  // 일단 0으로 초기화
            // 가장 큰 숫자를 기억해서 비교하면서 업데이트 함
            foreach (int score in scores)
            {
                if (score > maxValue)
                {
                    maxValue = score;  // 더 큰 값이 나오면 업데이트
                }
            }
            return maxValue;  // 최종 최대값 반환
        }
        static int GetAverageScore(int[] scores)
        {
            if (scores.Length == 0)
                return 0;  // 빈 배열 처리 (안전장치)

            int sum = 0;
            foreach (int score in scores)
                sum += score;

            return sum / scores.Length;  // 총합을 길이로 나눈 정수 평균 반환
        }
        static int GetIndexOf(int[] scores, int value)
        {
            for (int i = 0; i < scores.Length; i++)
            {
                if (scores[i] == value)
                    return i;  // 값 찾으면 바로 인덱스 반환
            }
            return -1;  // 찾지 못하면 -1 반환
        }

        static void Sort(int[] scores)
        {
            for (int i = 0; i < scores.Length; i++)
            {

                int minIndex = i;  // 현재 위치부터 탐색 시작

                // [i ~ scores.Length -1] 제일 작은 숫자가 있는 index를 찾는다
                for (int j = i; j < scores.Length; j++)
                {
                    if (scores[j] < scores[minIndex])
                        minIndex = j;  // 더 작은 값 발견 시 minIndex 갱신
                }

                // 현재 위치와 최소값 위치 교환 (Swap)
                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 };
            int highestScores = GetHighestScore(scores);
            int averageScore = GetAverageScore(scores);
            int IndexOf = GetIndexOf(scores, 20);

            Sort(scores);

        }
    }
}

📈 1. 최대값 찾기 - GetHighestScore

static int GetHighestScore(int[] scores)
{
    int maxValue = 0;  // 일단 0으로 초기화

    foreach (int score in scores)
    {
        if (score > maxValue)
        {
            maxValue = score;  // 더 큰 값이 나오면 업데이트
        }
    }
    return maxValue;  // 최종 최대값 반환
}

배운 점 정리

  • 배열을 순회하며 가장 큰 값을 찾는 기본적인 알고리즘
  • foreach를 활용해 요소 하나씩 접근
  • 초기값을 0으로 설정했지만, 만약 점수가 음수일 수도 있다면 int.MinValue로 설정하는 게 더 안전함

📊 2. 평균값 계산 - GetAverageScore

static int GetAverageScore(int[] scores)
{
    if (scores.Length == 0)
        return 0;  // 빈 배열 처리 (안전장치)

    int sum = 0;
    foreach (int score in scores)
        sum += score;

    return sum / scores.Length;  // 총합을 길이로 나눈 정수 평균 반환
}

배운 점 정리

  • 빈 배열을 예외 처리하는 습관은 매우 중요
  • 정수 나누기는 소수점이 버려진다는 점 주의 (정확한 평균을 원하면 double로 변환 필요)

🔎 3. 특정 값의 인덱스 찾기 - GetIndexOf

static int GetIndexOf(int[] scores, int value)
{
    for (int i = 0; i < scores.Length; i++)
    {
        if (scores[i] == value)
            return i;  // 값 찾으면 바로 인덱스 반환
    }
    return -1;  // 찾지 못하면 -1 반환
}

배운 점 정리

  • 순차 탐색(Linear Search) 기본 구현
  • 값이 없을 때는 -1 반환하는 패턴 익히기
  • 자료구조 라이브러리 메서드에도 이런 패턴이 많음

📥 4. 배열 정렬 - Sort (선택 정렬)

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;  // 더 작은 값 발견 시 minIndex 갱신
        }

        // 현재 위치와 최소값 위치 교환 (Swap)
        int temp = scores[i];
        scores[i] = scores[minIndex];
        scores[minIndex] = temp;
    }
}

배운 점 정리

  • 정렬 알고리즘 중 선택 정렬 구현
  • i 위치부터 끝까지 탐색해 최소값 찾고 swap
  • 시간복잡도 O(N²), 비효율적이지만 직접 구현해보는 경험이 중요

profile
李家네_공부방

0개의 댓글