ใ ๋ฐฐ์ด ใ
- ๋์ผํ ํ์ ์ ๋ฐ์ดํฐ๋ฅผ ํ๋ฒ์ ์ฌ๋ฌ๊ฐ ๋ง๋ค ๋ ์ฌ์ฉ
[1์ฐจ์ ๋ฐฐ์ด ์์ ]
// C code
#include <stdio.h>
int main(void) {
int arr[5];
int byte_size, size;
byte_size = sizeof(arr); // int ์๋ฃํ์ ํฌ๊ธฐ 4byte * 5 = 20
printf("๋ฐฐ์ด์ ๋ฐ์ดํธ ํฌ๊ธฐ : %d\n", byte_size);
size = sizeof(arr) / sizeof(arr[0]); // (4*5)20 / 4 = 5
printf("๋ฐฐ์ด์ ํฌ๊ธฐ : %d\n", size);
for (int i = 0; i < size; i++) {
arr[i] = 0;
printf("%d ", arr[i]);
}
return 0;
}
[2์ฐจ์ ๋ฐฐ์ด ์์ ]
// C code
#include <stdio.h>
#define ROW 3
#define COL 5
int main(void) {
int arr[ROW][COL];
int byte_size, size;
byte_size = sizeof(arr);
size = sizeof(arr) / sizeof(arr[0]);
printf("๋ฐฐ์ด์ ๋ฐ์ดํธ ํฌ๊ธฐ : %d\n", byte_size);
printf("๋ฐฐ์ด์ ํฌ๊ธฐ : %dํ\n", size);
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
arr[i][j] = 0;
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
ใ ๊ตฌ์กฐ์ฒด ใ
- ํ์ ์ด ๋ค๋ฅธ ๋ฐ์ดํฐ๋ฅผ ํ๋์ ์๋ฃํ์ผ๋ก ์ ์ธํ ๋ ์ฌ์ฉ
[๊ตฌ์กฐ์ฒด ์์ ]
// C code
#include <stdio.h>
typedef struct studentTag {
char name[10];
int age;
double gpa;
} STUDENT;
int main(void) {
printf("studentTag ๊ตฌ์กฐ์ฒด์ ํฌ๊ธฐ = %d\n", sizeof(struct studentTag));
STUDENT a = { "kim", 20, 4.3 };
printf("a ๊ตฌ์กฐ์ฒด์ ํฌ๊ธฐ : %d\n", sizeof(a));
printf("ํ์ a์ ์ด๋ฆ : %s\n", a.name);
printf("ํ์ a์ ๋์ด : %d\n", a.age);
printf("ํ์ a์ ํ์ : %.2f\n", a.gpa);
return 0;
}
ใ ๋ฐฐ์ด/๊ตฌ์กฐ์ฒด์ ์์ฉ ใ
[ ๋คํญ์ ๋ง์ ํ๋ก๊ทธ๋จ ]
- ๋คํญ์์ ๋ชจ๋ ํญ์ ๋ฐฐ์ด์ ์ ์ฅ
- ๋คํญ์์ 0์ด ์๋ ํญ๋ง์ ๋ฐฐ์ด์ ์ ์ฅํ๋ ๋ฐฉ๋ฒ
// C code
#include <stdio.h>
#define MAX(a, b) a>b?a:b
#define MAX_DEGREE 101
typedef struct {
int degree; // ๋คํญ์์ ์ฐจ์
float coef[MAX_DEGREE]; // ๋คํญ์์ ๊ณ์
} polynomial;
polynomial poly_add1(polynomial A, polynomial B) {
polynomial C; // ๋คํญ์ A + B ์ ๊ฒฐ๊ณผ ๋คํญ์์ธ C
int Apos = 0, Bpos = 0, Cpos = 0; // ๋ฐฐ์ด ์ธ๋ฑ์ค ๋ณ์
int degree_a = A.degree;
int degree_b = B.degree;
C.degree = MAX(A.degree, B.degree); // ๊ฒฐ๊ณผ ๋คํญ์ ์ฐจ์
while (Apos <= A.degree && Bpos <= B.degree) {
// Aํญ์ ์ฐจ์ > Bํญ์ ์ฐจ์
if (degree_a > degree_b) {
C.coef[Cpos++] = A.coef[Apos++];
degree_a--;
}
// Aํญ์ ์ฐจ์ == Bํญ์ ์ฐจ์
else if (degree_a == degree_b) {
C.coef[Cpos++] = A.coef[Apos++] + B.coef[Bpos++];
degree_a--;
degree_b--;
}
// Aํญ์ ์ฐจ์ < Bํญ์ ์ฐจ์
else {
C.coef[Cpos++] = B.coef[Bpos++];
degree_b--;
}
}
return C;
}
void print_poly(polynomial p) {
for (int i = p.degree; i > 0; i--) {
printf("%3.1fx^%d + ", p.coef[p.degree - i], i);
}
printf("%3.1f \n", p.coef[p.degree]);
}
int main(void) {
polynomial a = { 5, {3, 6, 0, 0, 0, 10} };
polynomial b = { 4, {7, 0, 5, 0, 1} };
polynomial c;
print_poly(a);
print_poly(b);
c = poly_add1(a, b);
printf("-------------------------------------------\n");
print_poly(c);
return 0;
}
[ ํฌ์ํ๋ ฌ ]
- ํ๋ ฌ์ ๊ฐ์ด ๋๋ถ๋ถ 0์ธ ํ๋ ฌ
โ 2์ฐจ์ ๋ฐฐ์ด๋ก ์ ์ฒด ์์๋ฅผ ์ ์ฅํ์ฌ ํํ ํ๋ ๋ฐฉ๋ฒ
- ์ ์ฒด ์์๋ฅผ 2์ฐจ์ ๋ฐฐ์ด๋ก ์ ์ฅ
- ํ๋ ฌ์ ์ฐ์ฐ์ ๊ฐ๋จํ ๊ตฌํ ๊ฐ๋ฅ
- ๋ฐฐ์ด์ ํ๋ ฌ ์ธ๋ฑ์ค ๊ฐ์ด 0์ธ ๊ฒฝ์ฐ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ ๋ญ๋น
โก 0์ด ์๋ ์์๋ค๋ง ์ ์ฅํ์ฌ ํํ ํ๋ ๋ฐฉ๋ฒ
- 2์ฐจ์ ๋ฐฐ์ด ๋ด 0์ด ์๋ ์์๋ค๋ง ์ ์ฅ
- ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ ์ ์ฝ
- ํ๋ ฌ ์ฐ์ฐ๋ค์ ๊ตฌํ์ด ๋ณต์กํด์ง
[ ํฌ์ํ๋ ฌ์ ์ ์น ]
โ 2์ฐจ์ ๋ฐฐ์ด๋ก ์ ์ฒด ์์๋ฅผ ์ ์ฅํ๋ ํ๋ ฌ์ ์ ์นํ๋ ๋ฐฉ๋ฒ
- ๋ฐฐ์ด์ ์์(i, j) → (j, i)๋ก ๊ตํํ๋ฉด ๊ฐ๋จํ๊ฒ ๊ตฌํ ๊ฐ๋ฅ
// C code
// ๊ธฐ์กด ํ๋ ฌ A → ์ ์น → ํ๋ ฌ B
void matrix_transpose(int A[row][col], int b[row][col]){
for(int i=0; i<row; i++)
for(int j=0; j<col; j++)
b[j][i] = a[i][j];
}
โก 0์ด ์๋ ์์๋ง (row, col, value)๋ก ํํํ๋ ๋ฐฉ๋ฒ
- ์ ์น ์ฐ์ฐ์ ์ํด ๊ตฌ์กฐ์ฒด ์ฌ์ฉ
// C code
#include <stdio.h>
#include <stdlib.h>
#define MAX_TERMS 100
typedef struct {
int row;
int col;
int value;
} ELEMENT;
typedef struct SparseMatrix {
ELEMENT data[MAX_TERMS];
int rows; // ํ์ ๊ฐ์
int cols; // ์ด์ ๊ฐ์
int terms; // ํญ์ ๊ฐ์
} SparseMatrix;
SparseMatrix matrix_transpose2(SparseMatrix a) {
SparseMatrix b;
int c, i, bindex; // ํ๋ ฌ b์์ ํ์ฌ ์ ์ฅ ์์น
b.rows = a.rows;
b.cols = a.cols;
b.terms = a.terms;
if (a.terms > 0) {
bindex = 0;
for (c = 0; c < a.cols; c++) {
for (i = 0; i < a.terms; i++) {
if (a.data[i].col == c) {
b.data[bindex].row = a.data[i].col;
b.data[bindex].col = a.data[i].row;
b.data[bindex].value = a.data[i].value;
bindex++;
}
}
}
}
return b;
}
void matrix_print(SparseMatrix a) {
int i;
printf("=========================\n");
for (i = 0; i < a.terms; i++)
printf("(%d, %d, %d) \n", a.data[i].row, a.data[i].col, a.data[i].value);
printf("=========================\n");
}
int main(void) {
SparseMatrix m = {
{{0, 3, 7},
{1, 0, 9},
{1, 5, 8},
{3, 0, 6},
{3, 1, 5},
{4, 5, 1},
{5, 2, 2}},
6,
6,
7
};
SparseMatrix result;
matrix_print(m);
printf("\n------- transpose -------\n\n");
result = matrix_transpose2(m);
matrix_print(result);
return 0;
}
ใ ํฌ์ธํฐ ใ
- ๋ค๋ฅธ ๋ณ์์ ์ฃผ์๋ฅผ ๊ฐ์ง๊ณ ์๋ ๋ณ์
- &์ฐ์ฐ์ : ๋ณ์์ ์ฃผ์๋ฅผ ๋ฐํ
- *์ฐ์ฐ์ : ํฌ์ธํฐ๊ฐ ๊ฐ๋ฆฌํค๋ ๊ณณ์ ๊ฐ์ ๋ฐํ
[ ํฌ์ธํฐ๋ฅผ ํจ์์ ๋งค๊ฐ๋ณ์๋ก ์ฌ์ฉํ๋ ์์ (ํฌ์ธํฐ์ ์ํ ํธ์ถ) ]
// C code
#include <stdio.h>
void swap(int* a, int* b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main(void) {
int a = 1, b = 2;
printf("swap์ ํธ์ถํ๊ธฐ ์ a=%d, b=%d\n", a, b);
swap(&a, &b);
printf("swap์ ํธ์ถํ๊ธฐ ํ a=%d, b=%d\n", a, b);
return 0;
}
[ ๋ฐฐ์ด์ ํจ์์ ๋งค๊ฐ๋ณ์๋ก ์ฌ์ฉํ๋ ์์ ]
// C code
#include <stdio.h>
#define SIZE 6
#define _CRT_SECURE_NO_WARNINGS
void get_int(int arr[]) {
printf("6๊ฐ์ ์ ์๋ฅผ ์
๋ ฅํ์์ค : ");
for (int i = 0; i < SIZE; i++)
scanf("%d", &arr[i]);
}
int arr_sum(int arr[]) {
int sum=0;
for (int i = 0; i < SIZE; i++)
// sum += *(arr + i)
sum += arr[i];
return sum;
}
int main(void) {
int arr[SIZE];
get_int(arr);
printf("arr์ ํฉ : %d\n", arr_sum(arr));
return 0;
}
[ ์ขํ ๋๊ฐ๋ฅผ ์ ๋ฌ๋ฐ์ ์ขํ ์ฌ์ด์ ๊ฑฐ๋ฆฌ ๊ตฌํ๊ธฐ ]
// C code
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct {
int x_coord;
int y_coord;
}Coord;
double get_distance(Coord p1, Coord p2) {
double dx = p1.x_coord - p2.x_coord;
double dy = p1.y_coord - p2.y_coord;
double distance = sqrt((dx * dx) + (dy * dy));
return distance;
};
int main(void) {
Coord p1 = { 1, 2 };
Coord p2 = { 9, 8 };
double distance = get_distance(p1, p2);
printf("p1์ p2 ์ ์ฌ์ด์ ๊ฑฐ๋ฆฌ : %.2f\n", distance);
return 0;
}