기록하고 까먹지 말기

과제 3 - Data types (1 ~ 7) 본문

전공/C

과제 3 - Data types (1 ~ 7)

yha97 2021. 6. 21. 14:48

이번 과제에서는 기본적인 데이터 타입별로 입력, 출력시 어떤 것을 사용해야 하는지를 주로 묻는 문제가 나왔다.

 

알고리즘 문제보다는 단순 개념을 묻는 문제였기 때문에 조금만 공부했다면 어려움은 없었을 것이다.

 

data type별 size를 출력하는 문제다.

 

sizeof(datatype)를 사용하여 출력하면 해결 가능하다.

 

#include <stdio.h>

int main(){
	// declare int, long, long long, double, long double
	int a;
	long b;
	long long c;
	double d;
	long double e;
	
	printf("Demonstrate the working of keyword long\n");
	printf("-----------------------------------------\n");
	
	// printing size of each eata types with sizeof
	printf("The size of int = %d bytes\n", sizeof(a));
	printf("The size of long = %d bytes\n", sizeof(b)); // 4 bytes in 32-bit OS
	printf("The size of long long = %d bytes\n", sizeof(c));
	printf("The size of double = %d bytes\n", sizeof(d));
	printf("The size of long double = %d bytes\n", sizeof(e));
	
	return 0;
}

 

 

문제에서 제시한 사과, 오렌지, 바나나의 합을 구하여 출력하는 문제이다.

 

+ 연산을 통해 출력하면 해결 가능하다.

 

#include <stdio.h>

int main(){
	// declare variables
	int apples = 1800;
	int oranges = 450;
	int bananas = 8500;
	
	// declare and calculate sum of fruits
	int total = apples + oranges + bananas;
	
	// printing result
	printf("total = %d won\n", total);
	return 0;
}

 

원화를 달러화로 환전하는 문제다.

 

위에 나온대로 Q2에 나온답에 0.00090을 곱하면 된다.

 

이를 활용하여 Q2에 나온 값에 적용하면 해결 가능하다.

 

#include <stdio.h>

int main(){
	// declare variables
	int apples = 1800;
	int oranges = 450;
	int bananas = 8500;
	
	// declare and calculate sum of fruits
	int total = apples + oranges + bananas;
	
	// translating from won to dollars
	float total_dollar = total * 0.00090;
	
	// printing result
	printf("total = %.2f dollars\n", total_dollar);
	return 0;
}

수숫점 연산을 할 경우 데이터 타입이 float 또는 double형이어야 한다는 점을 명심해야 한다.

 

아스키코드를 활용하여 스펠링을 입력하면 이에 맞는 아스키 코드를 출력하는 문제다.

 

char 형으로 입력하고 int형으로 출력하면 해결 가능하다.

 

#include <stdio.h>

int main(){
	// declare character variable
	char a=0;
	
	printf("input an alphabet:");
	scanf("%c", &a); // input the value of variable
	
	//printing result
	printf("%d", a);
	return 0;
}

입력받을 때는 무조건 %c형으로 입력받아야 한다. 선언을 char 형으로 했기 때문이다.

 

정수를 입력받고 그 수에 해당하는 절댓값을 출력하는 프로그램이다.

 

주어진 문제에 빈칸만 채워넣는 프로그램이다. 절댓값을 구하는 함수를 쓰는게 아닌 해당 수의 데이터 타입만 쓰면 되는 문제이기에 해결하는데 큰 어려움은 없었다.

 

#include <stdio.h>
#include <stdlib.h>

int main(){
	int num;
	printf("\n Input a positive of negative number :");
	scanf("%d", &num); // answer 1 (input integer number)
	printf(" The absolute value of the given number is : %d\n\n", abs(num)); // answer 2 (prints absolute number)
	return 0;
}

 

 

Q5에서와 같이 절댓값을 출력하는 프로그램이지만 문제는 데이터타입을 쓰라는 것이다.

 

정수형 데이터 타입을 쓰면 간단히 해결 가능하다.

 

#include <stdio.h>
#include <stdlib.h>

int main(){
	long int x, y;
		printf("\n Input 1st number (positive of negative) : ");
		scanf("%d", &x); // answer 1(input with integer)
		printf(" Input 2nd number (positive of negative) : ");
		scanf("%d", &y); // answer 2(input with integer)
	printf(" The absolute value of 1st number is : %d\n", labs(x)); // answer 3(printing with integer)
	printf(" The absolute value of 2nd number is : %d\n", labs(y)); // answer 4(printing with integer)
	
	return 0;
}

 

 

stdlib의 random 함수를 사용하는 프로그램이다.

 

강의 교안에도 없는 내용을 과제로 제시한 것을 보면 이건 아마 교수님이 학생들에게 구글링 하라는 의미를 간접적으로 보이신 것 같다.

 

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(){
	int random;
	srand(time(NULL)); // setting seed of random with time(NULL)
	random = (rand()%(50-10)+1)+10; // answer (from 10 to 50)
	
	printf("%d\n", random); // printing result
	
	return 0;
}

'전공 > C' 카테고리의 다른 글

과제 4 - Control Flow (6 ~ 9)  (0) 2021.06.21
과제 4 - Control Flow (1 ~ 5)  (0) 2021.06.21
과제 2 - Operator & Binary(1 ~ 5)  (0) 2021.06.21
과제 1 - variable (6 ~ 9) (범위포함 x)  (0) 2021.06.21
과제 1 - variable (1 ~ 5)  (0) 2021.06.21