자기주도C언어프로그래밍 Chapter12. 함수2 - 연습문제&자가진단
교재 P.240 연습문제1
#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;
}
교재 P.242 자가진단1
#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 num;
scanf("%d", &num);
int arr[num];
input(arr, num);
sort(arr, num);
output(arr, num);
return 0;
}
교재 P.243 연습문제2
#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;
}
교재 P.244 자가진단2
#include <stdio.h>
bool date(int month, int day){
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if(day >= 1 && day <= 31){
return true;
}
else{
return false;
}
break;
case 2:
if(day >= 1 && day <= 28){
return true;
}
else{
return false;
}
break;
case 4:
case 6:
case 9:
case 11:
if(day >= 1 && day <= 30){
return true;
}
else{
return false;
}
break;
default:
return false;
}
}
int main(){
int month, day;
scanf("%d %d", &month, &day);
if(date(month, day)){
printf("OK!");
}
else{
printf("BAD!");
}
return 0;
}
※ bool date를 이용하여 해당 월에 해당하는 날짜가 있으면 true를, 없으면 false를 반환하였습니다. 또한 1월~13월에 해당하지 않는 경우엔 false만 반환하였습니다.
교재 P.245 연습문제3
#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 - d));
return 0;
}
※ abs( ) : ( )안의 절댓값을 구하는 함수
※ fabs( ) : ( )안 실수의 절댓값을 구하는 함수
교재 P.246 자가진단3
#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);
if(abs(a) > abs(b)){
printf("%d", a);
}
else{
printf("%d", b);
}
printf("\n");
if(abs(c) < abs(d)){
printf("%.2f", c);
}
else{
printf("%.2f", d);
}
return 0;
}
※ 출력 예가 소수점 둘째자리까지 밖에 없어서 %.2f로 출력하였습니다.
교재 P.247 연습문제4
#include <stdio.h>
#include <math.h>
int main(){
double area;
double base;
double exp;
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;
}
※ sqrt(a) : a의 제곱근을 구하는 함수 (리턴값은 double형)
※ pow(a, b) : a^b를 계산하는 함수 (리턴값은 double형)
교재 P.248 자가진단4
#include <stdio.h>
#include <math.h>
int main(){
double radius, area;
scanf("%lf", &area);
area = area / 3.14;
radius = sqrt(area);
printf("%.2f", radius);
return 0;
}
※ 출력 예시에 소수점 둘째 자리까지 출력되어 있어서 %.2f로 작성하였습니다.
교재 P.249 연습문제5
#include <stdio.h>
#include <math.h>
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;
}
※ floor(a) : "버림"을 해주는 함수 (소수점 이하의 값을 버린 결과를 리턴), (음수일 경우 -2.3이면 -2가 아니라 -3이 된다)
※ round(a) : "반올림"을 해주는 함수
※ ceil(a) : "올림"을 해주는 함수 (음수일 경우 -2.3이면 -3이 아니라 -2가 된다)
교재 P.250 자가진단5
#include <stdio.h>
#include <math.h>
int main(){
double a, b, c;
scanf("%lf %lf %lf", &a, &b, &c);
if(a > b && a > c){ // a가 제일 크면
printf("%.0f ", ceil(a)); // a는 올림
if(b > c){ // b가 2번째로 크면
printf("%.0f ", floor(c)); // c는 내림
printf("%.0f ", round(b)); // b는 반올림
}
else{ // c가 2번째로 크면
printf("%.0f ", floor(b)); // b는 내림
printf("%.0f ", round(c)); // c는 반올림
}
}
if(b > a && b > c){ // b가 제일 크면
printf("%.0f ", ceil(b)); // b는 올림
if(a > c){ // a가 2번재로 크면
printf("%.0f ", floor(c)); // c는 내림
printf("%.0f ", round(a)); // a는 반올림
}
else{ // c가 2번째로 크면
printf("%.0f ", floor(a)); // a는 내림
printf("%.0f ", round(c)); // c는 반올림
}
}
if(c > a && c > b){ // c가 제일 크면
printf("%.0f ", ceil(c)); // c는 올림
if(a > b){ // a가 2번째로 크면
printf("%.0f ", floor(b)); // b는 내림
printf("%.0f ", round(a)); // a는 반올림
}
else{ // c가 2번재로 크면
printf("%.0f ", floor(a)); // a는 내림
printf("%.0f ", round(b)); // b는 반올림
}
}
return 0;
}
교재 P.251 연습문제6 - 소스 1
#include <stdio.h>
int main(){
double r, ci;
const double PI = 3.14;
scanf("%lf", &r);
ci = r * 2 * PI;
printf("%.2f\n", ci);
return 0;
}
교재 P.252 연습문제6 - 소스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;
}
교재 P.253 자가진단6
#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;
}
// 교재에 Hint라고 주어진 내용은 #define one 1 ....이 있었습니다. #define three 3을 하지 않고 one+2로 해도 같은 결과나 나올겁니다.
교재 P.254 연습문제7
#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;
}
교재 P.257 자가진단7
#include <stdio.h>
#define N 10
#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 output(int a[]){
int i;
for(i=0;i<N;i++){
printf("%d ", a[i]);
}
}
int main(){
int i, j;
int arr[N];
input(arr);
for(i=1;i<N;i++){
for(j=0;j<N-i;j++){
if(arr[j] < arr[j+1]){
SWAP(arr[j], arr[j+1]);
}
}
output(arr);
printf("\n");
}
return 0;
}
교재 P.258 연습문제8
#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", a, b, c);
return 0;
}
교재 P.259 자가진단8 - 1
#include <stdio.h>
int SQUARE(int x, int y);
int main() {
int a, b, c;
scanf("%d %d", &a, &b);
c = SQUARE(a-b,2);
printf("(%d - %d) ^ 2 = %d\n", a, b, c);
c = SQUARE(a+b,3);
printf("(%d + %d) ^ 3 = %d", a, b, c);
return 0;
}
int SQUARE(int x, int y) {
int i, result=1;
for(i=1;i<=y;i++){
result = result * x;
}
return result;
}
교재 P.259
#include <stdio.h>
#include <math.h>
#define SQUARE(x, y) (int)pow(x, y)
int main() {
int a, b, c;
scanf("%d %d", &a, &b);
c = SQUARE(a - b, 2);
printf("(%d - %d) ^ 2 = %d\n", a, b, c);
c = SQUARE(a + b, 3);
printf("(%d + %d) ^ 3 = %d", a, b, c);
return 0;
}
※ #include <math.h>를 통해 pow함수를 사용하여 작성하였습니다.
중간고사 이후 (노느라)정신이 없어서 드디어 다음 내용을 작성하네요....ㅋㅋ
4/29~30 중간 시험 끝났다고 놀았습니다..ㅋㅋ
5/6~7 정처기 시험 공부
5/14 정처기 시험 - 가채점 결과는 합격 점수 받았습니다 (합격 커트라인이 60점인데 61점으로 턱걸이...)
5/20~21 정처기 시험 끝났다고 놀았습니다...ㅋㅋ
그러다 보니 이제야 작성을 하게 되는데 또 다음주정도 부턴 학기말 고사 기간이라 또 공부를 하러 가지 않으면....
종강을 해야 좀 시간적 여유가 많아질 듯하네요..(놀지만 않았으면 충분히 많았겠지만 ㅎㅎ;;;;)
번외로 요즘 파이썬 공부를 하고 있습니다.
주변에서 파이썬이 쉽다 쉽다 해서 정말 압도적으로 쉬울줄 알았는데
익숙하지 않아서 그런지 학교에서 배워서 과제 등을 하면서 좀 써먹던 C에 비해선 좀 어렵네요...ㅋㅋ