예제#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 |
대학 생활 인싸되는 질문카드 + 밸런스 게임 - 상황에 맞게 꺼내 쓰는 질문카드로 꿀잼 분위기로 전환해보세요!
하트 오픈 : 질문 카드 + 밸런스게임 - Google Play 앱
온라인, 오프라인 어떤 상황이든, 어색한 친구, 연인, 처음 만난 사람, 찐친끼리, 재미있는 대화를 나눠보세요.
play.google.com
'프로그래밍 > c언어 콘서트' 카테고리의 다른 글
| C언어 콘서트 14장 프로그래밍(Programming) 풀이 (3) | 2019.06.17 |
|---|---|
| C언어 콘서트 12장 프로그래밍(Programming) 풀이 (1) | 2019.06.16 |
| C언어 콘서트 11장 프로그래밍(Programming) 풀이 (1) | 2019.06.11 |
| C언어 콘서트 10장 프로그래밍(Programming) 풀이 (4) | 2019.06.09 |
| C언어콘서트 9장 프로그래밍(Programming) 풀이 (1) | 2019.06.05 |