예제#2. int형 동적메모리 할당 받고 정수 입력한 후 출력하기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int* p = (int*)malloc(sizeof(int) * 5);
printf("저장할 정수를 5개 입력하시오: ");
int i;
for (i = 0; i < 5; i++) {
scanf("%d", &p[i]);
}
for (i = 0; i < 5; i++) {
printf("%d\n", p[i]);
}
free(p);
return 0;
}
|
cs |
1.
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
|
#include <stdio.h>
#include <stdlib.h>
typedef struct rec
{
int i;
float PI;
char A;
} my_record;
int main(void) {
my_record* p;
p = (my_record*)malloc(sizeof(my_record));
p[0].i = 10;
p[0].PI = 3.14;
p[0].A = 'a';
printf("%d\n", p[0].i);
printf("%f\n", p[0].PI);
printf("%c\n", p[0].A);
free(p);
return 0;
}
|
cs |
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
26
27
|
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int count, i;
double sum = 0;
printf("요소의 갯수: ");
scanf("%d", &count);
double* p;
p = (double*)malloc(sizeof(double) * count);
for (i = 0; i < count; i++) {
printf("배열의 요소를 입력하시오: ");
scanf("%lf", &p[i]);
sum += p[i];
}
printf("합: %lf", sum);
free(p);
return 0;
}
|
cs |
3.
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
31
32
|
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int count, i, j;
double max;
printf("요소의 갯수: ");
scanf("%d", &count);
double* p;
p = (double*)malloc(sizeof(double) * count);
for (i = 0; i < count; i++) {
printf("요소 %d: ", i + 1);
scanf("%lf", &p[i]);
}
max = p[0];
for (j = 1; j < count; j++) {
if (p[j] > max)
max = p[j];
}
printf("최대값: %lf", max);
free(p);
return 0;
}
|
cs |
4.
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
|
#include <stdio.h>
#include <stdlib.h>
#define SIZE 100
int main(void) {
int i, sum = 0;
int average;
int* p;
p = (int*)malloc(sizeof(int) * SIZE);
for (i = 0; i < SIZE; i++) {
p[i] = rand() % 100;
sum += p[i];
}
average = sum / SIZE;
printf("평균 = %d", average);
free(p);
return 0;
}
|
cs |
제가만든 게임입니다! 도움이 되셨다면 평가 한번 부탁드립니다~
https://play.google.com/store/apps/details?id=com.attitudeproblem&hl=en_CA&gl=
'프로그래밍 > c언어 콘서트' 카테고리의 다른 글
C언어 콘서트 14장 프로그래밍(Programming) 풀이 (0) | 2019.06.17 |
---|---|
C언어 콘서트 12장 프로그래밍(Programming) 풀이 (0) | 2019.06.16 |
C언어 콘서트 11장 프로그래밍(Programming) 풀이 (0) | 2019.06.11 |
C언어 콘서트 10장 프로그래밍(Programming) 풀이 (0) | 2019.06.09 |
C언어콘서트 9장 프로그래밍(Programming) 풀이 (0) | 2019.06.05 |