9일차 실습문제
//문제7
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void) {
char ch1, ch2;
printf("ch1, ch2 입력(ch1<ch2): ");
scanf(" %c %c", &ch1, &ch2); //%c 앞에 띄어쓰기
char a = ch1;
printf("while\n");
while (a >= ch1 && a <= ch2) {
printf("%c ", a);
a++;
}
printf("\nfor\n");
for (a = ch1; a <= ch2; a++) {
printf("%c ", a);
}
return 0;
}
//문제8
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void) {
char ch1, ch2;
printf("ch1, ch2 입력(ch1<ch2): ");
scanf(" %c %c", &ch1, &ch2);
char a = ch1;
printf("while\n");
while (a <= ch2) {
if (a % 2 == 0) //코드값이 짝수인 문자
printf("%c ", a);
a++;
}
printf("\nfor\n");
for (a = ch1; a <= ch2; a++) {
if (a % 2 != 0) //코드값이 홀수인 문자
printf("%c ", a);
}
return 0;
}
//문제9
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <ctype.h> //toupper 함수 헤더파일
int main(void) {
char ch1, ch2;
printf("ch1, ch2 입력(ch1<ch2): ");
scanf(" %c %c", &ch1, &ch2);
char a = ch1;
printf("while\n");
while (a <= ch2) {
if (a % 2 == 0)
printf("%c ", a);
a++;
}
printf("\nfor\n");
for (a = ch1; a <= ch2; a++) {
if (a % 2 != 0)
printf("%c ", toupper(a)); //소문자를 대문자로 변환하여 출력
}
return 0;
}
10일차 실습문제
//문제1
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void) {
int n;
int a; //열의 개수
printf("정수 n 입력: ");
scanf("%d", &n);
printf("열의 개수 입력: ");
scanf("%d", &a);
int b = n / a; //행의 개수
int i = 0;
int j = 0;
int output = 1;
printf("while\n");
while (i < b) { //행
for (j = 0; j < a; j++) { //열
printf("%.3d ", output);
output++;
}
i++; //i값 증가시키기!
printf("\n");
}
output = 1; //초기화
printf("\nfor\n");
for (i = 0; i < b; i++) {
for (j = 0; j < a; j++) {
printf("%.3d ", output);
output++;
}
printf("\n");
}
return 0;
}
//문제4
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int toss;
printf("toss 횟수 입력: ");
scanf("%d", &toss);
srand((unsigned)time(NULL));
int i = 0;
int front = 0;
int back = 0;
printf("while\n");
while (i < toss) {
if (rand() % 2 != 0) //발생시킨 난수가 홀수면, 앞면
front++;
else //난수가 짝수면, 뒷면
back++;
i++;
}
printf("앞면(1)의 확률 = %f\n", (double)front / toss);
printf("뒷면(0)의 확률 = %f\n", (double)back / toss);
i = 0;
front = 0;
back = 0;
printf("for\n");
for (i = 0; i < toss; i++) {
if (rand() % 2 != 0)
front++;
else
back++;
}
printf("앞면(1)의 확률 = %f\n", (double)front / toss);
printf("뒷면(0)의 확률 = %f\n", (double)back / toss);
return 0;
}
//문제5
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define SIZE 6
int main(void) {
int toss;
printf("toss 횟수 입력: ");
scanf("%d", &toss);
srand((unsigned)time(NULL));
int i = 0; //toss번
int j = 0; //출력
int tossing1[SIZE] = { 0 }; //배열 초기화
int tossing2[SIZE] = { 0 };
printf("while\n");
while (i < toss) {
tossing1[rand() % 6]++; //배열의 인덱스: 0~5
i++;
}
while (j < SIZE) {
printf("눈금%d의 확률: %f\n", j+1, (double)tossing1[j] /toss);
j++;
}
i = 0;
j = 0;
printf("\nfor\n");
for (i = 0; i < toss; i++) {
tossing2[rand() % 6]++;
}
for (j = 0; j < SIZE; j++) {
printf("눈금%d의 확률: %f\n", j + 1, (double)tossing2[j] / toss);
}
return 0;
}
//문제9
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char ch1, ch2;
int n;
printf("ch1, ch2 입력(ch1<ch2): ");
scanf(" %c %c", &ch1, &ch2);
printf("n 입력: ");
scanf("%d", &n);
srand((unsigned)time(NULL));
int i = 1;
char answer;
printf("while\n");
while (i <= n) {
answer = rand() % (ch2 - ch1 + 1) + ch1; //ch1과 ch2 사이의 랜덤 문자
printf("%.2d번째 [%c, %c] 사이의 랜덤문자: %c\n", i, ch1, ch2, answer);
i++;
}
i = 1;
printf("\nfor\n");
for (i = 1; i <= n; i++) {
answer = rand() % (ch2 - ch1 + 1) + ch1;
printf("%.2d번째 [%c, %c] 사이의 랜덤문자: %c\n", i, ch1, ch2, answer);
}
return 0;
}
11일차 실습문제
//문제3
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define PI 3.14
int main(void) {
double r; //초기 도우 반지름
printf("초기 도우 반지름 : ");
scanf("%lf", &r);
double firstSize = r * r * PI;
printf("초기 도우 면적 = %f\n", firstSize);
int i = 0;
double size = firstSize;
printf("while\n"); //while문
while (size < 2 * firstSize) {
i++;
size = size * 1.1 * 1.1; //size 증가
printf("%d번 던진 후 도우 면적 : %f\n", i, size);
}
printf("===================================\n");
printf("2배 이상 되기까지 던진 횟수: %d번\n", i);
i = 0;
size = firstSize; //초기화
printf("\nfor\n"); //for문
for (i = 0; size < 2 * firstSize; i++) {
size = size * 1.1 * 1.1;
printf("%d번 던진 후 도우 면적: %f\n", i+1, size);
}
printf("====================================\n");
printf("2배 이상 되기까지 던진 횟수: %d번\n", i);
return 0;
}
12일차 실습문제
//문제4
#include <stdio.h>
#define SIZE 26
int main(void) {
int i = 0;
char arr[SIZE] = { 'a' };
for (i = 0; i < SIZE; i++) {
arr[i] = 'a' + i; //'a' + i
printf("arr[%.2d] = %c ", i, arr[i]);
if ((i + 1) % 6 == 0)
printf("\n");
}
return 0;
}
//문제7
#include <stdio.h>
#include <ctype.h> //toupper 함수를 위한 헤더파일
#define SIZE 26
int main(void) {
int i = 0;
char arr[SIZE];
for (i = 0; i < SIZE; i++) {
if (i % 2 == 0)
arr[i] = toupper('a' + i);
else
arr[i] = 'a' + i;
printf("arr[%.2d] = %c ", i, arr[i]);
if ((i + 1) % 6 == 0)
printf("\n");
}
return 0;
}
//문제9
#include <stdio.h>
#include <stdlib.h>
#define SIZE 50
int main(void) {
int i = 0;
int sum = 0;
int arr[SIZE] = { 0 }; //초기화
srand((unsigned)time(NULL));
for (i = 0; i < 1000; i++) {
arr[rand() % 50]++; //+1 하지 않음!
sum++;
}
for (i = 0; i < 50; i++) {
printf("arr[%.2d] = %d ", i + 1, arr[i]);
if ((i + 1) % 10 == 0)
printf("\n");
}
printf("sum = %d\n", sum);
return 0;
}
//문제11
#include <stdio.h>
#include <stdlib.h>
#define SIZE 26
int main(void) {
int i = 0;
char alphabet = 'a';
int sum = 0;
char arr[SIZE] = { 'a' };
for (i = 0; i < 1000; i++) {
arr[rand() % 26]++; //+'a' 없음!
sum++;
}
for (i = 0; i < SIZE; i++) {
printf("arr[%c] = %d ", alphabet, arr[i]);
alphabet++;
if ((i + 1) % 10 == 0)
printf("\n");
}
printf("\nsum = %d\n", sum);
return 0;
}
'Programming > 컴퓨터프로그래밍및실습' 카테고리의 다른 글
[컴프실] 제11장: 포인터 (0) | 2023.08.11 |
---|---|
[컴프실] 제10장-2: 정렬 (0) | 2023.07.23 |
[컴프실] 제10장: 배열 (0) | 2023.07.19 |
[컴프실] 제9장: 함수와 변수 (0) | 2023.07.17 |
[컴프실] 제8장: 함수 (0) | 2023.07.16 |