배열
namespace CSharpPractice
{
class Player
{
}
class Monster
{
}
internal class Program
Player player;
Monster monster;
static void Main(string[] args)
{
//배열
int[] scores = new int[5] {1,2,3,4,5}; //5개를 담을수있는 바구니 scores를 생성
int[] scores2 = scores; //참조타입과 복사타입
scores2[0] = 9999;
for (int i = 0; i < scores.Length; i++)
{
Console.WriteLine(scores[i]);
}
foreach (int score in scores) //scores안에 있는 배열을 score를 한개씩 순회하면서 값을 뽑는다
{
Console.WriteLine(score);
}
}
}
}
배열 문제풀
namespace CSharpPractice
{
class Player { }
class Monster { }
internal class Program //자료구조와 컬렉션
{
static int GetHighestScore(int[] scores)
{
//가장 큰 값 반환
int maxValue = 0;
foreach (int score in scores)
{
//하나씩 돌면서 가장 큰 숫자를 찾아보자.
//돌면서 이전에 만난숫자보다 큰값인지 아닌지 확인해야함. (큰숫자를 기억)
if (score > maxValue)
{
maxValue = score;
}
}
return maxValue;
}
static int GetAverageScore(int[] scores)
{
//0으로 나누면 안되니까 안전장치로 return 끝내기
if (scores.Length == 0)
{
return 0;
}
//평균 값 반환
int sum = 0;
foreach (var score in scores)
{
sum += score;
}
return sum / scores.Length;
}
static int GetIndexOf(int[] scores, int value)
{
//원하는 값이 있는지 확인 없으면 -999
for (int i = 0; i <scores.Length; i++)
{
if (scores[i] == value) //우리가 원하는 값(value에 넣은 값)이라면 return i번째
{
return i;
}
}
return -999; //아니라면 -999
}
static void Sort(int[] scores)
{
//배열 오름차순으로 정렬
for (int i = 0; i < scores.Length; i++)
{
int minimumIndex = i;
for (int j = i; j < scores.Length; j++)
{
if (scores[j] < scores[minimumIndex])
{
minimumIndex = j;
}
}
// swap
int temp = scores[i];
scores[i] = scores[minimumIndex];
scores[minimumIndex] = temp;
}
}
static void Main(string[] args)
{
int[] scores = new int[5] { 10, 30, 40, 20, 50 };
int highestScore = GetHighestScore(scores);
Console.WriteLine(highestScore);
int everageScore = GetAverageScore(scores);
Console.WriteLine(everageScore);
int index = GetIndexOf(scores, 20);
Console.WriteLine(index);
Sort(scores);
}x
}
}

Sort는 좀 어렵던데... 해석해보자.
static void Srot(int[] scores) 해석
scores 배열을 오름차순으로 정렬
for (int i = 0; i < scores.Lenght; i++) //배열 순회
{
int minIndex = i; //가장 작은 값을 찾기위한 int minIndex
for (int j = i; j < scores.Length; j++) //i 위치부터 j끝까지 순회하며 작은 값 찾음
{
if (scores[j] < scores[minIndex]) //scores[J]가 현재 찾은 가장 작은 값보다 작은지 확인
{
minIndex = j; //맞다면 minIndex를 j로 교체
}
//가장 작은 값 minIndex를 찾았으니 이제 스왑을 해야함
int temp = scores[i]; //값이 사라지므로 temp라는 저장 변수 생성
scores[i] = scores[minIndex];
socre[minIndex] = temp; //값 교환
}
}
namespace CSharpPractice
{
class Program
{
class Map
{
int[ , ] tiles = {
{1, 1, 1, 1, 1 },
{1, 0, 0, 0, 1 },
{1, 0, 0, 0, 1 },
{1, 0, 0, 0, 1 },
{1, 1, 1, 1, 1 },
};
public void Render()
{
var defaultColor = Console.ForegroundColor;
for (int y = 0; y < tiles.GetLength(1); y++)
{
for (int x = 0; x < tiles.GetLength(0); x++)
{
if (tiles[y, x] == 1)
{
Console.ForegroundColor = ConsoleColor.Red;
}
else
{
Console.ForegroundColor = ConsoleColor.Green;
}
Console.Write('★');
}
Console.WriteLine();
}
Console.ForegroundColor = defaultColor;
}
}
static void Main(string[] args)
{
Map map = new Map();
map.Render();
}
}
}

GetLength(1)은 다차원배열의 앞부분
GetLength(0)은 다차원 배열 0~4라 0
'C#.' 카테고리의 다른 글
| C# 컬렉션 맛보기 (0) | 2023.11.16 |
|---|---|
| C# 람다 (0) | 2023.11.15 |
| Unity Disacards - C# (0) | 2023.11.05 |
| 이것이 C#이다 10장) 배열 Array [2] (0) | 2023.09.04 |
| 이것이 C# 이다 10장) 배열 Array [1] (0) | 2023.08.31 |