이것이 C# 이다. 10장 연습문제

2020. 1. 20. 16:10C# 언어/이것이 C# 이다. 연습문제

반응형

 

1. 다음 배열 선언 문장 중 올바르지 않은 것을 고르세요. (답 : 1번)

  1. int [ ] array = new string [3] {"안녕", "Hello", "Halo"}; // int형 배열에 string값 배열을 참조하고 있다.  (오류)
  2. int [ ] array = new int [3]{1,2,3}; // 배열
  3. int[ ] array = new int []{1,2,3}; // 선언에 
  4. int[ ] array = {1,2,3}; // 다른 방법 3가지들

 

2. 두 행렬의 곱은 다음과 같이 계산합니다.

 

다음 두 행렬 A와 B의 곱을 2차원 배열을 이용하여 계산하는 프로그램을 작성하세요.

 

코드

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
using System;
using System.Collections;
using static System.Console;
namespace ex
{
    class MainApp
    {
        static void Main()
        {
            int[,] A = new int[22] { { 32 }, { 14 } }; // 2차원 배열 선언
            int[,] B = new int[22] { { 92 }, { 17 } }; // 2차원 배열 선언
            int[,] muxArray = new int[22]; // 두 배열의 곱을 저장할 배열
 
            muxArray[00= (A[00* B[00]) + (A[01* B[10]);
            muxArray[01= (A[00* B[01]) + (A[01* B[11]);
            muxArray[10= (A[10* B[00]) + (A[11* B[10]);
            muxArray[11= (A[10* B[01]) + (A[11* B[11]);
 
            Write($"muxArray[0,0] :{muxArray[0, 0]}"); //29
            WriteLine($" muxArray[0,1] :{muxArray[0, 1]}"); // 20
            Write($"muxArray[1,0] :{muxArray[1, 0]}"); // 13
            WriteLine($" muxArray[1,1] :{muxArray[1, 1]}"); // 30
        }
    }
}

 

 

3. 다음 코드의 출력 결과는 무엇일까요?

Stack stack = new Stack();
stack.Push(1);
stack.Push(2);
stack.Push(3);
stack.Push(4);
stack.Push(5);

while (stack.Count > 0) // Count는 현재 stack의 개수를 의미한다.
                                // 시작 Count는 0 부터 이므로 => 4부터 시작

                                // Pop을 하면 실제 stack의 개수가 빠져나감으로 1개씩 줄어듦
    Console.WriteLine(stack.Pop());

출력 부분

Stack은 LIFO(Last In First Out) 즉 입력(Push)을 주면 맨 앞에 쌓이고 출력(Pop)을 하면 맨 앞에 데이터가 출력된다.


 

4. 다음 코드의 출력 결과는 무엇일까요?

Queue que = new Queue();
            que.Enqueue(1);
            que.Enqueue(2);
            que.Enqueue(3);
            que.Enqueue(4);
            que.Enqueue(5);

            while (que.Count > 0)
                WriteLine(que.Dequeue());

출력부분

Stack과는 반대의 출력이 나옵니다. 이유는 FIFO(First In First Out) 선출 선입 구조이기 때문입니다.

 

5. 다음과 같은 결과를 출력하도록 아래의 코드를 완성하세요.

회사 : Microsoft
URL : www.microsoft.com
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Collections;
using static System.Console;
namespace ex
{
    class MainApp
    {
        static void Main()
        {
            Hashtable ht = new Hashtable();
 
            ht["회사"= "Microsoft";
            ht["URL"= "www.microsoft.com";
 
            WriteLine("회사 : {0}", ht["회사"]);
            WriteLine("URL : {0}", ht["URL"]);
        }
    }
}
반응형