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);
}
}
}
static int GetHighestScore(int[] scores)
{
int maxValue = 0; // 일단 0으로 초기화
foreach (int score in scores)
{
if (score > maxValue)
{
maxValue = score; // 더 큰 값이 나오면 업데이트
}
}
return maxValue; // 최종 최대값 반환
}
✅ 배운 점 정리
foreach를 활용해 요소 하나씩 접근int.MinValue로 설정하는 게 더 안전함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로 변환 필요)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; // 현재 위치부터 탐색 시작
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;
}
}
✅ 배운 점 정리