"R을 이용한 데이터 처리 & 분석 실무 - 서민구 지음" 정리 자료 #4
- https://thebook.io/006723/
- 첫번째 : goo.gl/FJjOlq
- 두번째 : goo.gl/Wdb90g
- 세번째 : goo.gl/80VGcn
- 네번째 : goo.gl/lblUsR
"R을 이용한 데이터 처리 & 분석 실무 - 서민구 지음" 정리 자료 #1
- https://thebook.io/006723/
- 첫번째 : goo.gl/FJjOlq
- 두번째 : goo.gl/Wdb90g
- 세번째 : goo.gl/80VGcn
- 네번째 : goo.gl/lblUsR
This document discusses structures in C programming. It explains that structures can contain elements of different data types, accessed by name, unlike arrays where all elements must be of the same type and accessed by index. It provides examples of declaring a structure type with members, defining structure variables, accessing members using the dot operator, passing structures to functions, and initializing an array of structures.
"R을 이용한 데이터 처리 & 분석 실무 - 서민구 지음" 정리 자료 #4
- https://thebook.io/006723/
- 첫번째 : goo.gl/FJjOlq
- 두번째 : goo.gl/Wdb90g
- 세번째 : goo.gl/80VGcn
- 네번째 : goo.gl/lblUsR
"R을 이용한 데이터 처리 & 분석 실무 - 서민구 지음" 정리 자료 #1
- https://thebook.io/006723/
- 첫번째 : goo.gl/FJjOlq
- 두번째 : goo.gl/Wdb90g
- 세번째 : goo.gl/80VGcn
- 네번째 : goo.gl/lblUsR
This document discusses structures in C programming. It explains that structures can contain elements of different data types, accessed by name, unlike arrays where all elements must be of the same type and accessed by index. It provides examples of declaring a structure type with members, defining structure variables, accessing members using the dot operator, passing structures to functions, and initializing an array of structures.
The document discusses call-by-value in function invocation in C. When a function is called, only the values of the arguments are passed to the function, not the variables themselves. So any changes made to the parameters inside the function are not reflected in the calling function. This causes an issue when trying to swap variables by passing them to a Swap function.
2. 2
One-Dimensional Arrays
§ Array
– 같은 이름의 변수를 사용하여 여러 개의 type이 같은 값을 표현할
수 있게 하는 data type.
– num이라는 이름으로 10개의 int형 변수를 연속적으로 5개 할당
int num[5] ;
num
1000번지 1004 1008 1012 1016
3. 3
One-Dimensional Arrays
§ Array
– 각각의 변수에는 index를 사용하여 접근 한다. index = 0가 첫 번째
원소를 의미 한다.
int num[5] ;
num[0] = 10 ;
num[1] = 13 ;
num[2] = 14 ;
num[3] = 17 ;
num[4] = 20 ;
10 13 14 17 20num
1000번지 1004 1008 1012 1016
num[0] num[1] num[2] num[3] num[4]
4. 4
One-Dimensional Arrays
§ Array – Syntax
– 배열의 크기는 반드시 양수로 써야 한다.
– 배열 원소의 첨자는 항상 0 부터 시작한다.
• 위의 예제의 경우는 grade[0], grade[1],~ , grade[49]가 생성.
element-type array_name[size];
[Ex] int grade[50];
data type variable Name
size of Array
5. 5
One-Dimensional Arrays
#include <stdio.h>
int main() {
int a[100], k ;
for( k = 0 ; k < 100 ; k++ )
scanf( “%d”, &a[k] ) ;
for( k = 99 ; k >= 0 ; k-- )
printf( “%d ”, a[k] ) ;
printf( “n” ) ;
return 0;
}
§ 배열사용 예제
#include <stdio.h>
int main() {
int a[100], k, sum = 0 ;
for( k = 0 ; k < 100 ; k++ )
scanf( “%d”, &a[k] ) ;
for( k = 0 ; k < 100 ; k++ )
sum += a[k] ;
printf( “%dn”, sum ) ;
return 0;
}
6. 6
Initialization
§ 초기화
– 지정된 array에 초기값을 할당하는 것.
§ 초기값이 배열 원소의 값보다 적을 때
float x[7] = { -1.1, 0.2, 33.0, 4.4, 5.05, 0.0, 7.7 };
x[0] = -1.1, x[1] = 0.2,…, x[6] = 7.7 로 초기화된다.
int a[100] = { -1 };
a[0] = -1, a[1] = 0, … a[99] = 0처럼
남은 원소들은 모두 0으로 초기화 된다.
7. 7
cnt_abc Program
#include <stdio.h>
#include <ctype.h>
int main(void) {
int letter[26] = {0}, c, i ;
while ( (c = getchar( )) != EOF) {
c=toupper(c);
if( isalph(c) ) ++letter[c – ‘A’];
}
for ( i = 0; i < 26; ++i) {
if ( i % 6 == 0 ) printf(“n”);
printf(“%4c:%3d”, ‘A’ + i, letter[i]);
} /* end of for */
return 0;
}
문자가 끝날때까지 getchar()로
입력받는다. 소문자는 대문자로
바꾼다.
배열에 저장된 문자들의
개수를 하나씩 출력한다.
§ 입력 받은 문자 각각의 개수를 헤아리자
8. 8
The Relationship between Arrays and
Pointers
§ Example :
– 각 원소의 주소는 ?
int num[5] ;
num
1000번지 1004 1008 1012 1016
num[0] num[1] num[2] num[3] num[4]
&num[0] == 1000
&num[1] == 1004
&num[2] == 1008
&num[3] == 1012
&num[4] == 1016
9. 9
The Relationship between Arrays and
Pointers
§ Example : num은 무엇일까?
– num은 포인터 상수로 “배열 시작 주소” 이다.
int num[5] ; num
1000번지 1004 1008 1012 1016
num[0] num[1] num[2] num[3] num[4]
num == &num[0] == 1000
10. 10
The Relationship between Arrays and
Pointers
§ Example : 포인터의 특별한 연산
– “포인터 + 1” 은 “1 큰 주소값”을 의미하는 것이 아니라, 그 “다음
원소의 주소” 이다
– “포인터 - 1” 은 “바로 이전 원소의 주소” 이다
int num[5] ;
1000번지 1004 1008 1012 1016
num[0] num[1] num[2] num[3] num[4]
num == &num[0] == 1000
(num+0) == ??
(num+1) == ??
(num+2) == ??
(num+3) == ??
(num+4) == ??
&num[0]
&num[1]
&num[2]
&num[3]
&num[4]
12. 12
Pointer Arithmetic and Element Size
§ 포인터 연산
– 변수 p가 포인터라면 p + 1은 그 type의 다음 변수를 저장하
거나 access할 수 있도록 주소를 생성한다.
– 포인터의 정수 덧셈 연산이 가능하다.
– 포인터의 정수 뺄셈 연산이 가능하다.
– 두 개의 포인터의 뺄셈 연산이 가능하다.
13. 13
Pointer Arithmetic and Element Size
§ Adding an Integer to a Pointer
[Ex]
p = &a[2];
q = p + 3;
p += 6;
0 1 2 3 4 5 6 7 8 9
p
a
0 1 2 3 4 5 6 7 8 9
p
a
0 1 2 3 4 5 6 7 8 9
p
a
q
q
14. 14
Pointer Arithmetic and Element Size
§ Subtracting an Integer from a Pointer
[Ex]
p = &a[8];
q = p - 3;
p -= 6;
0 1 2 3 4 5 6 7 8 9
p
a
0 1 2 3 4 5 6 7 8 9
p
a
0 1 2 3 4 5 6 7 8 9
p
a
q
q
15. 15
Pointer Arithmetic and Element Size
§ Subtracting Pointers
[Ex]
p = &a[5];
q = &a[1];
i = p – q; /* i == 4 */
i = q – p; /* i == -4 */
0 1 2 3 4 5 6 7 8 9
q
a
p
16. 16
Pointer Arithmetic and Element Size
§ Comparing Pointers
– 관계연산자 (relational operators) <, <=, >,>= 사용 가능.
– 동등연산자(equality operators) ==, != 사용 가능.
[Ex]
p = &a[5];
q = &a[1];
p <= q; /* result is 0 */
p >= q; /* result is 1 */
17. 17
Pointer Arithmetic and Element Size
§ 포인터 연산의 예제
int a[ ] = { 5,15,25,43,12,1,7,89,32,11}
int *p = &a[1], *q = &a[5] ;
1. *(p + 3) ?
2. *(q - 2) ?
3. q - p ?
4. if ( p > q ) ?
5. if ( *p > *q )?
18. 18
Pointer Arithmetic and Element Size
§ 포인터 연산의 예제
#include <stdio.h>
int main(void)
{
double a[2], *p, *q;
p = &a[0]; /* points at base of array */
q = p + 1; /* equivalent to q = &a[1]; */
printf(“%dn”, q – p );
printf(“%dn”, (int) q – (int) p );
printf(“%dn”, sizeof(double) );
return 0;
}
19. Combining the * and ++ Operators
§ Combining the * and ++ Operators
– p를 1 증가 혹는 감소 시킨 후 *p
– *p 후 p를 1 증가 혹은 감소
– p가 가르키는 변수를 1 증가 혹은 감소
19
*++p º *(++p), *--p º *(--p)
*p++ º *(p++), *p-- º *(p--)
(*p)++, (*p)--
20. Combining the * and ++ Operators
§ Combining the * and ++ Operators
20
int main()
{
int k, a[10], *p = a ;
while( p < &a[10] )
*p++ = 0 ;
return 0;
}
int main()
{
int k, a[10], *p = a ;
while( p < &a[10] ) {
*p = 0 ;
p = p + 1 ;
}
return 0;
}
21. Combining the * and ++ Operators
§ Combining the * and ++ Operators
21
int main()
{
int k, a[10], *p = &a[10] ;
while( p >= &a[0] )
*--p = 0 ;
return 0;
}
int main()
{
int k, a[10], *p = a ;
while( p >= &a[0] ) {
p = p -1 ;
*p = 0 ;
}
return 0;
}
22. Example
§ 입력 받은 수 더하기
22
#include <stdio.h>
int sum( int num[], int size ) {
int k, sum = 0 ;
for( k = 0 ; k < size ; k++ )
sum += num[k] ;
return sum ;
}
int main() {
int a[100], k ;
for( k = 0 ; k < 100 ; k++ )
scanf( “%d”, &a[k] ) ;
printf( “%dn”, sum(a, 100) ) ;
return 0;
}
#include <stdio.h>
int sum( int num[], int size ) {
int k, sum = 0 ;
for( k = 0 ; k < size ; k++ )
sum += *num++ ;
return sum ;
}
int main() {
int a[100], k ;
for( k = 0 ; k < 100 ; k++ )
scanf( “%d”, &a[k] ) ;
printf( “%dn”, sum(a, 100) ) ;
return 0;
}
int num[]는
int *num과 같다.