Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 |
Tags
- 힙영역
- 차이점
- std::unordered_map
- 스택영역
- MonoBehaviour
- 프래그멘테이션
- 공부
- vector
- 스택
- 알고리즘
- 트리
- 자료구조
- 인프런
- std::map
- 큐
- 반복문
- Queue
- 멀티쓰레드
- 기술면접
- 배열
- rookiss
- c#
- static_cast
- 리스트
- 벡터
- 해쉬맵
- map
- list
- 객체지향
- thread
Archives
- Today
- Total
호빵의 IT 개발소
[C#] 배열 맛보기 본문
배열의 심화과정을 바로 들어가게 되면 어렵기 때문에 예제를 통해 간단한 배열을 맛보는 시간을 가지겠습니다.
배열은 [ ]를 사용하여 표현할 수 있습니다.
1가지 다른 방법이 있는데 지금은 scores 배열 5개 선언을 하고 밑에 scores[0] ~ [4]값을 각각 적어주었는데 배열의 숫자가 많지 않다면 예시와 같이 2가지 방법을 적어주어도 같은 의미가 됩니다.
예)
int[] scores = new int[] { 10, 20, 30, 40, 50 }
int[] scores = { 10, 20, 30, 40, 50 }
for문을 좀더 예제보다 좀더 새련되게 바꾸고 싶다면 foreach문을 사용합니다.
예)
foreach (int score in scores) //int score in scores는 예제의 for문과 같은 의미입니다.
{
Console.WriteLine(score);
}
class Program
{
static void Main(string[] args)
{
// 배열
int[] scores = new int[5]; //int형식의 배열을 5개 가지고 있다.
//0 1 2 3 4
scores[0] = 10; //[0]에는 10을 저장함
scores[1] = 20; //[1]에는 20을 저장함
scores[2] = 30; //[2]에는 30을 저장함
scores[3] = 40; //[3]에는 40을 저장함
scores[4] = 50; //[4]에는 50을 저장함
for (int i = 0; i < scores.Length; i++)
{
Console.WriteLine(scores[i]); //[0] ~ [4]안에 있는 값을 순서대로 출력한다.
}
}
}

※연습문제
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) //배열 안에 있는 값의 평균
{
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) //찾는 값이 배열안에 몇번째에 있는지 찾는 함수, 없으면 -1반환
{
for (int i = 0; i < scores.Length; i++)
{
if (scores[i] == value)
return i;
}
return -1;
}
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
int temp = 0;
scores[i] = scores[minIndex];
scores[minIndex] = temp;
}
}
static void Main(string[] args)
{
// 배열
int[] scores = new int[5] { 10, 30, 40, 20, 50 }
int highestScore = GetHighestScore(scores);
Console.WriteLine(highestScore); //50출력
int averageScore = GetAverageScore(scores);
Console.WriteLine(averageScore); //30출력
int index = GetIndexOf(scores, 20);
Console.WriteLine(index); //3 출력
Sort(scores); // scores = {10, 20, 30, 40, 50}으로 정렬
}
}
---------------------------------------------------------------------------------------------------------------------------
참고 : [인프런] Rookiss님의 [C#과 유니티로 만드는 MMORPG 게임 개발 시리즈] Part1: C# 기초 프로그래밍 입문
'자료구조와 알고리즘 > 자료구조와 알고리즘 맛보기' 카테고리의 다른 글
| [C#] 연결 리스트 (0) | 2022.01.11 |
|---|---|
| [C#] 동적 배열 구현 (0) | 2022.01.11 |
| [C#] 배열, 동적 배열, 연결 리스트 비교 (0) | 2022.01.10 |
| Big-O 표기법 (0) | 2022.01.10 |
| [C#] Dictionary (0) | 2022.01.08 |
Comments