본문 바로가기

c언어

10. 배열과 포인터

 

1. 요소의 개수가 5인 int형 배열을 선언(초깃값은 임의로 설정)하고, 각 요소의 주소를 출력하는 프로그램을 작성하세요. 단, 각 요소는 배열 연산자로 접근해야 합니다.

[소스코드]

#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>

void main(void)
{
	int arr[5] = { 0 };
	for (int i = 0; i < sizeof(arr) / sizeof(int); ++i)
	{
		printf("%d. 주소: [%p]\n", i + 1, &arr[i]);
	}
	return;
}

[실행결과]

 

 

2. 1번에서 작성한 프로그램에서 배열 연산자를 제거하고 출력 결과가 같게 나오도록 프로그램을 수정하세요.

[소스코드]

#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>

void main(void)
{
	int arr[5] = { 0 };
	for (int i = 0; i < sizeof(arr) / sizeof(int); ++i)
	{
		printf("%d. 주소: [%p]\n", i + 1, arr + i);
	}
	return;
}

[실행결과]

 

 

 

3. 요소의 개수가 5인 char*형 배열을 임의의 문자열로 초기화한 후, 내림차순으로 정렬하여 출력하는 프로그램을 작성하세요. 단, 문자열을 비교하는 함수는 strcmp() 함수를 사용하고 정렬 알고리즘은 선택 정렬(Selection Sort) 방식을 사용합니다.

[소스코드]

#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>
#include <string.h>

void selection_sort(const char* szArr[5], int length)
{
	for (int i = 1; i < length; ++i)
	{
		const char* select = szArr[i-1];
		for (int j = i; j < length; ++j)
		{
			if (strcmp(select, szArr[j]) < 0)
			{
				const char* tmp = select;
				select = szArr[j];
				szArr[j] = tmp;
			}
		}
		szArr[i - 1] = select;
	}
	return;
}

void main(void)
{
	const char* szArr[5] = { "apple", "banana", "cherry", "watermelon", "strawberry" };
	int length = sizeof(szArr) / sizeof(const char*);
	selection_sort(szArr, length);
	for (int i = 0; i < length; ++i)
	{
		printf("%d. %s \n", i + 1, szArr[i]);
	}
	return;
}

[실행결과]

 

 

4. int형 2차원 배열(5행 5열)을 선언하여 다음과 같이 값을 채우는 프로그램을 작성하세요.

[소스코드]

#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>

void main(void)
{
	int nArr[5][5] = { 0 };
	int x = 0, y = 0;
	int n = 1, nStep = 4;
	int xAxis[] = { 1, 0, -1, 0 };
	int yAxis[] = { 0, 1, 0, -1 };

	while (n <= sizeof(nArr) / sizeof(int))
	{
		int cnt = nStep <= 1 ? 1 : 4;
		for (int i = 0; i < cnt; ++i)
		{
			for (int j = 0; j < nStep; ++j)
			{
				nArr[y][x] = n++;
				x += xAxis[i];
				y += yAxis[i];
			}
		}
		nStep /= 2;
		++x; ++y;
	}

	for (int i = 0; i < sizeof(nArr) / sizeof(nArr[0]); ++i)
	{
		for (int j = 0; j < sizeof(nArr[0]) / sizeof(int); ++j)
		{
			printf("%-2d ", nArr[i][j]);
		}
		putc('\n', stdout);
	}

	
	return;
}

[실행결과]

 

 

 

5. 4번에서 작성한 예제를 수정하여 다음과 같이 배열을 채우는 프로그램을 작성하세요.

[소스코드]

#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>

void main(void)
{
	int nArr[5][5] = { 0 };
	int x = 4, y = 0;
	int n = 1, nStep = 4;
	int xAxis[] = { -1, 0, 1, 0 };
	int yAxis[] = { 0, 1, 0, -1 };

	while (n <= sizeof(nArr) / sizeof(int))
	{
		int cnt = nStep <= 1 ? 1 : 4;
		for (int i = 0; i < cnt; ++i)
		{
			for (int j = 0; j < nStep; ++j)
			{
				nArr[y][x] = n++;
				x += xAxis[i];
				y += yAxis[i];
			}
		}
		nStep /= 2;
		--x; ++y;
	}

	for (int i = 0; i < sizeof(nArr) / sizeof(nArr[0]); ++i)
	{
		for (int j = 0; j < sizeof(nArr[0]) / sizeof(int); ++j)
		{
			printf("%-2d ", nArr[i][j]);
		}
		putc('\n', stdout);
	}

	
	return;
}

[실행결과]

 

 

 

6. 다음과 같이 배열을 채우는 프로그램을 작성한 후, 맨 오른쪽 열과 맨 아래 행에 각각 행과 열의 합을 저장하는 프로그램을 작성하세요.

[소스코드]

#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>

void main(void)
{
	int nArr[4][5] = { 0 };
	int nRow = sizeof(nArr) / sizeof(nArr[0]);
	int nColumn = sizeof(nArr[0]) / sizeof(int);
	int n = 1;

	for (int i = 0; i < nRow; ++i)
	{
		for (int j = 0; j < nColumn; ++j)
		{
			if (i == nRow - 1)
			{
				for (int k = 0; k < nRow - 1; ++k)
				{
					nArr[i][j] += nArr[k][j];
				}
			}
			else if (j == nColumn - 1)
			{
				for (int k = 0; k < nColumn - 1; ++k)
				{
					nArr[i][j] += nArr[i][k];
				}
			}
			else
			{
				nArr[i][j] = n++;
			}
		}
	}

	// print
	for (int i = 0; i < nRow; ++i)
	{
		for (int j = 0; j < nColumn; ++j)
		{
			printf("%-2d ", nArr[i][j]);
		}
		putc('\n', stdout);
	}
	
	return;
}

[실행결과]

 

'c언어' 카테고리의 다른 글

12. 문자, 문자열 처리 함수  (0) 2023.06.21
11. 함수  (0) 2023.06.17
9. 포인터와 메모리 관리  (0) 2023.06.09
8. 반복문  (0) 2023.06.07
7. 기초 제어문  (0) 2023.06.05