전체 코드
namespace Rookiss_CSharp
{
class Program()
{
static int GetHighestScore(int[] scores)
{
int maxScore = 0;
foreach (int score in scores)
{
if (score > maxScore)
maxScore = score;
}
return maxScore;
}
static int GetAverageScore(int[] scores)
{
if (scores.Length == 0)
return 0;
int sumScore = 0;
foreach (int score in scores)
{
sumScore += score;
}
return sumScore / 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;
}
static void Swap(int[] scores, int indexA, int indexB)
{
int temp = scores[indexA];
scores[indexA] = scores[indexB];
scores[indexB] = temp;
}
static void Sort(int[] scores)
{
for (int i = 0; i < scores.Length; i++)
{
// [i ~ scores.Length - 1] 제일 작은 숫자가 있는 index를 찾는다.
int minIndex = i;
for (int j = i; j < scores.Length; j++)
{
if (scores[j] < scores[minIndex])
minIndex = j;
}
// swap
Swap(scores, i, minIndex);
}
}
static void Main(string[] args)
{
int[] scores = new int[5] { 10, 30, 40, 20, 50 };
foreach (int score in scores)
{
Console.Write($"{score} "); // 10 30 40 20 50
}
Console.WriteLine();
Sort(scores); // 오름차순 정렬
foreach (int score in scores)
{
Console.Write($"{score} "); // 10 20 30 40 50
}
}
}
}
출처
[C#과 유니티로 만드는 MMORPG 게임 개발 시리즈] Part1: C# 기초 프로그래밍 입문| Rookiss - 인프런 강
현재 평점 4.9점 수강생 6,987명인 강의를 만나보세요. 기초 프로그래밍 지식이 없는 사람들을 위한 C# 프로그래밍 기초 강의입니다. 문법 암기 위주의 수업이 아니라, 최대한 필요한 부분만을 요
www.inflearn.com
50강 연습문제
'C# > 기본 문법' 카테고리의 다른 글
| C# List (0) | 2025.11.20 |
|---|---|
| C# 다차원 배열, GetLength() (0) | 2025.11.20 |
| C# 배열 (0) | 2025.11.13 |
| C# TextRPG2 (0) | 2025.11.13 |
| C# 접근 제한자 , 어셈블리란? (0) | 2025.11.05 |