Basics/자기주도 C언어 프로그래밍

[Chapter] 12 함수 II

whereareyoung 2023. 7. 14. 15:16
//1 6개의 정수를 입력 받아 오름차순으로 정렬하여 출력하는 프로그램을 작성하시오
입력 예 9 2 6 8 3 4
출력 예 2 3 4 6 8 9
 
 
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
 
void input(int a[], int cnt)
{
int i;
for (i = 0; i < cnt; i++)
{
scanf("%d", &a[i]);
}
}
 
void output(int a[], int cnt)
{
int i;
for (i = 0; i < cnt; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
 
void swap(int& x, int& y)
{
int tmp = x;
x = y;
y = tmp;
}
 
void sort(int a[], int cnt)
{
int i, j;
for (i = 0; i < cnt - 1; i++)
{
for (j = i + 1; j < cnt; j++)
{
if (a[i] > a[j])
{
swap(a[i], a[j]);
}
}
}
}
 
int main()
{
int arr[6];
 
input(arr, 6);
sort(arr, 6);
output(arr, 6);
 
return 0;
}
 
문자열
 
 
자가진단 1 10 이하의 자연수 n을 입력받고 n개의 정수를 입력받아 내림차순으로 정렬하여 출력하는 프로그램을 작성하시오. (배열을 전달하는 함수를 이용한다.)
입력 예
4
10 9 2 15
출력 예 
15 10 9 2
 
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
 
void input(int a[], int n)
{
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
}
 
void output(int a[], int n)
{
for (int i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
 
void sort(int a[], int n)
{
int i, j;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - 1 - i; j++)
{
if (a[j] < a[j + 1])
{
int tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
}
}
}
}
 
int main()
{
int arr[10];
int n;
 
scanf("%d", &n);
 
input(arr, n);
sort(arr, n);
output(arr, n);
 
return 0;
}
 
 
 
 
 
 
//2 3과목의 점수를 입력 받아서 평균이 60 미만이거나 1과목이라도 40점 미만이 있으면 "불합격", 아니면 "합격" 이라는 메세지를 아래와 같이 출력하는 프로그램을 작성하시오.
입력 예 3과목의 점수를 입력하세요. 95 77 80
출력 예 축하합니다. 합격입니다.
 
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void input(int a[], int cnt)
{
int i;
 
printf("%d과목의 점수를 입력하세요. ", cnt);
 
for (i = 0; i < cnt; i++)
{
scanf("%d", &a[i]);
}
}
 
bool pass(int a[], int cnt)
{
int i, sum = 0, avg;
 
for (i = 0; i < cnt; i++)
{
if (a[i] < 40) return false;
sum += a[i];
}
 
avg = sum / cnt;
 
if (avg < 60) return false;
return true;
}
 
int main()
{
int score[3];
 
input(score, 3);
 
if (pass(score, 3))
{
printf("축하합니다. 합격입니다.");
}
else
{
printf("죄송합니다. 불합격입니다.");
}
 
return 0;
}
 
 
 
자가진단 2 2016년의 날짜를 월 일로 입력받아서 입력된 날짜가 존재하면 "OK!", 그렇지 않으면 "BAD!"라고 출력하는 프로그램을 작성하시오.
입력 예 2 30
출력 예 BAD!
 
(풀이 1)
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
 
bool output(int month, int day)
{
int calander[12] = { 31, 28, 31,30 ,31 ,30 ,31, 31, 30, 31, 30, 31 };
if (month > 0 && month <= 12)
{
if (day <= calander[month - 1] && day > 0) return true;
} return false;
}
 
int main()
{
int month, day;
scanf("%d %d", &month, &day);
 
if (output(month, day))
{
printf("Ok!");
}
else
{
printf("BAD!");
}
 
return 0;
}
 
(풀이2)
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void input(int& month, int& day)
{
scanf("%d %d", &month, &day);
}
 
bool output(int month, int day, int calander[], int cnt)
{
if (day <= calander[month]) return true;
else return false;
}
 
int main()
{
int calander[13] = { 0, 31, 28, 31,30 ,31 ,30 ,31, 31, 30, 31, 30, 31 };
int month, day;
 
input(month, day);
 
if (output(month, day, calander, 13)) printf("OK!\n");
else printf("BAD!");
 
return 0;
}
 
 
 
 
//3 두 정수를 입력받아 차를 구하고, 두 실수를 입력받아 차를 구하는 프로그램을 작성하시오.
입력 예
58 62
1.25 52.23
출력 예 
두 정수의 차 : 4
두 실수의 차 : 50.980000
 
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
 
int main()
{
int a, b;
double c, d;
 
scanf("%d %d", &a, &b);
scanf("%lf %lf", &c, &d);
 
printf("두 정수의 차 : %d \n", abs(a - b));
printf("두 실수의 차 : %f \n", fabs(c - a));
 
return 0;
}
 
 
자가진단 3 두 개의 정수를 입력받아 절대값이 더 큰 수를 출력하고, 두 개의 실수를 입력받아 절대값이 작은 수를 출력하는 프로그램을 작성하시오.
입력 예
-50 40
-12.32 5.67
 
출력 예 
-50
5.67
 
(풀이1) 조건연산자 활용
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
 
int max(int a, int b)
{
int max;
 
max = abs(a) > abs(b) ? a : b;
 
return max;
}
 
double min(double a, double b)
{
double max;
 
max = fabs(a) < fabs(b) ? a : b;
 
return max;
}
 
int main()
{
int a, b;
double c, d;
 
scanf("%d %d", &a, &b);
scanf("%lf %lf", &c, &d);
 
printf("%d \n", max(a, b));
printf("%.2f \n", min(c, d));
 
return 0;
}
 
(풀이2) if문 활용
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
 
int max(int a, int b)
{
int max;
if (abs(a) > abs(b))
{
max = a;
}
else
{
max = b;
}
return max;
}
 
double min(double a, double b)
{
double min;
if (fabs(a) < fabs(b))
{
min = a;
}
else
{
min = b;
}
return min;
}
 
int main()
{
int a, b;
double c, d;
 
scanf("%d %d", &a, &b);
scanf("%lf %lf", &c, &d);
 
printf("%d \n", max(a, b));
printf("%.2f \n", min(c, d));
 
return 0;
}
 
 
 
 
 
//4 정사각형의 넓이를 입력받아서 한 변의 길이를 구하고, 밑과 지수를 입력받아 거듭제곱의 결과를 출력하는 프로그램을 작성하시오.
입력과 출력의 예
정사각형의 넓이 : 36
정사각형 한 변의 길이 : 6.000000
밑과 지수 : 4 2
4.000000의 2.000000승은 16.000000입니다.
 
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
 
int main()
{
double area; //넓이
double base; //밑
double exp; // 지수(exponent)를 줄여서 나타냄
 
printf("정사각형의 넓이 : ");
scanf("%lf", &area);
printf("정사각형의 한 변의 길이 : %f \n", sqrt(area));
 
printf("밑과 지수 : ");
scanf("%lf %lf", &base, &exp);
printf("%f의 %f승은 %f입니다. \n", base, exp, pow(base, exp));
 
return 0;
}
 
 
 
 
자가진단 4 원의 넓이를 입력받아 반지름의 길이를 소수 둘째자리까지 출력하는 프로그램을 작성하시오. (원의 넓이 = 반지름 * 반지름 * 3.14 식을 이용하시오.)
입력 예 314
출력 예 10.00
 
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
 
int main()
{
int area; //원의 넓이
 
scanf("%d", &area);
 
printf("%.2f \n", sqrt(area/3.14));
 
return 0;
}
 
 
 
 
//5 반지름의 길이를 입력받아서 원의 넓이를 구하되, 소수 이하를 버림한 경우, 반올림한 경우, 올림한 경우를 각각 출력하는 프로그램을 작성하시오. (원주율은 3.14로 한다.)
입력 예 원의 반지름 : 6
출력 예 
원의 넓이
버림 : 113
반올림 : 113
올림 : 114
 
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
 
double round(double val)
{
return floor(val + 0.5);
}
 
int main()
{
double radius, area, pi = 3.14;
printf("원의 반지름 : ");
scanf("%lf", &radius);
 
area = pow(radius, 2) * pi;
 
printf("원의 넓이\n");
printf("버림 : %.0f\n", floor(area));
printf("반올림 : %.0f\n", round(area));
printf("올림 : %.0f\n", ceil(area));
 
return 0;
}
 
 
 
 
자가진단 5 세 개의 실수를 입력받아 가장 큰 수를 올림한 정수를 출력하고 가장 작은 수를 버림한 정수를 출력한 후 남은 수를 반올림한 정수를 출력하는 프로그램을 작성하시오.
입력되는 실수는 -1000 이상 1000 이하이다.
입력 예 3.45 51.48 -100.1
출력 예 52 -101 3
 
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
 
double round(double val)
{
return floor(val + 0.5);
}
 
int main()
{
double a, b, c;
double max = -1000, min = 1000;
double rest;
 
scanf("%lf %lf %lf", &a, &b, &c);
 
if (a > b && a > c) max = a;
else if (b > a && b > c) max = b;
else max = c;
 
if (a < b && a < c) min = a;
else if (b < a && b < c) min = b;
else min = c;
 
if (a != max && a != min) rest = a;
else if (b != max && b != min) rest = b;
else rest = c;
 
printf("%.0f %.0f %.0f\n",ceil(max), floor(min), round(rest));
 
return 0;
}
 
 
 
소수함수 외워라
 
//6 반지름의 길이를 입력받아서 원의 둘레의 길이를 구하여 출력하는 프로그램을 작성하시오. (원주율은 3.14로 하고 반올림하여 소수 둘째자리까지 출력한다.)
입력 예 5.5
출력 예 34.54
 
 
(소스 1)
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
 
int main()
{
double r, ci; // 반지름(radius), 원주(circumference)
const double PI = 3.14;
 
scanf("%lf", &r);
ci = r * 2 * PI;
printf("%.2f\n", ci);
 
return 0;
}
 
(소스2)
#include <stdio.h>
#define PI 3.14
#define SIK r * 2 * PI
 
int main()
{
double r, ci; // 반지름, 원주
scanf("%lf", &r);
ci = SIK;
printf("%.2f\n", ci);
 
return 0;
}
 
 
자가진단 6 main 함수 내에는 숫자를 사용하지 말고 1, 2, 3 세 개의 숫자를 조합하여 가능한 한 모든 합을 출력하는 프로그램을 작성하시오.
출력 예 
1 + 1 = 2
1 + 2 = 3
1 + 3 = 4
2 + 1 = 3
2 + 2 = 4
2 + 3 = 5
3 + 1 = 4
3 + 2 = 5
3 + 3 = 6
hint : define one 1....
 
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define one 1
#define three 3
 
int main()
{
int i, j;
for (i = one; i <= three; i++)
{
for (j = one; j <= three; j++)
{
printf("%d + %d = %d\n", i, j, i + j);
}
}
return 0;
}
 
 
 
 
 
//7 5개의 정수를 입력받아 오름차순으로 정렬하여 출력하는 프로그램을 작성하시오.
입력 예 9 2 6 8 3
출력 예 2 3 6 8 9
 
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define N 5
#define SWAP(x, y) {int z=x; x=y; y=z;}
 
void input(int a[])
{
int i;
for (i = 0; i < N; i++)
{
scanf("%d", &a[i]);
}
}
 
void sort(int a[])
{
int i, j;
for (i = 1; i < N; i++)
{
for (j = 0; j < N - i; j++)
{
if (a[j] > a[j + 1])
{
SWAP(a[j], a[j + 1])
}
}
}
}
 
void output(int a[])
{
int i;
for (i = 0; i < N; i++)
{
printf("%d ", a[i]);
}
}
 
int main()
{
int arr[N];
 
input(arr);
sort(arr);
output(arr);
 
return 0;
}
 
 
 
 
자가진단 7 10개의 정수를 입력받아 버블정렬로 내림차순 정렬을 하면서 하나의 단계가 끝날 때마다 그 정렬 결과를 출력하는 프로그램을 작성하시오.
입력 예 15 93 26 8 43 10 25 88 75 19
출력 예 
93 26 15 43 10 25 88 75 19 8
93 26 43 15 25 88 75 19 10 8
93 43 26 25 88 75 19 15 10 8
93 43 26 88 75 25 19 15 10 8
93 43 88 75 26 25 19 15 10 8
93 88 75 43 26 25 19 15 10 8
93 88 75 43 26 25 19 15 10 8
93 88 75 43 26 25 19 15 10 8
93 88 75 43 26 25 19 15 10 8
 
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define SWAP(x, y) {int z=x; x=y; y=z;}
 
void input(int a[])
{
int i;
for (i = 0; i < 10; i++)
{
scanf("%d", &a[i]);
}
}
 
void sort(int a[])
{
int i, j, k;
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10 - i; j++)
{
if (a[j] < a[j + 1])
{
SWAP(a[j], a[j + 1])
}
}
for (k = 0; k < 10; k++)
{
printf("%d ", a[k]);
}
printf("\n");
}
}
 
int main()
{
int arr[10];
 
input(arr);
sort(arr);
 
return 0;
}
 
 
k부분 output으로 빼기
j < 10 – i 주의
 
 
 
//8 두개의 정수 a, b를 입력받아 a보다 10 큰 수와 b보다 5 작은 수의 곱을 구하여 출력하는 프로그램을 작성하시오.
입력 예 10 20
출력 예 (10 + 10) * (20 - 5) = 300
 
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define MULTI(x, y) (x) * (y)
 
int main()
{
int a, b, c;
scanf("%d %d", &a, &b);
c = MULTI(a + 10, b - 5);
 
printf("(%d + 10) * (%d - 5) = %d\n", a, b, c);
 
return 0;
}
 
 
 
 
 
자가진단 8 정수 두 개를 입력받아서 매크로 함수를 작성하여 두 수의 차를 제곱한 값과 합을 세제곱한 값을 각각 출력하는 프로그램을 작성하시오. (출력시 거듭제곱은 '^'로 표시하기로 한다.)
입력 예 5 10
출력 예 
(5 - 10) ^ 2 = 25
(5 + 10) ^ 3 = 3375
 
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define minus(x, y) (x - y)
#define plus(x, y) (x + y)
 
int main()
{
int a, b;
int M2, M3;
 
scanf("%d %d", &a, &b);
 
M2 = minus(a, b);
M2 *= M2;
 
M3 = plus(a, b);
M3 *= M3 * M3;
 
printf("(%d - %d) ^ 2 = %d\n", a, b, M2);
printf("(%d + %d) ^ 3 = %d\n", a, b, M3);
 
return 0;
}
 
 

'Basics > 자기주도 C언어 프로그래밍' 카테고리의 다른 글

[Chapter] 14 문자열 I  (0) 2023.07.14
[Chapter] 13 함수 III  (0) 2023.07.14
[Chapter] 11 함수 I  (0) 2023.07.14
[Chapter] 10 배열 II  (0) 2023.07.14
[Chapter] 09 배열 I  (0) 2023.07.14