본문 바로가기

+ 컴퓨터/C

[C언어] 기초 예제 3 - sin값 구현하기, 난수 생성 예제

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>//기본 입출력 헤더
#include <math.h>//수학 관련 헤더
void main()
{
  float pi = 3.141592;
  float a = 0;  //소수일 때 float
  int d; //정수일 때 int
  for(d = 0 ; d <= 360 ; d += 15)  //+15, d+=15 d는 15도씩 증가 , for문 뒤에는 ;찍으면 안 됨
  {
   a = sin(pi / 180 * d);
   printf("sin(%d) = %.2f\n", d, a);
  }
}

math.h 헤더를 이용한 sin값 출력입니다.
sin 0 = 0.00 이런 식으로 각에 따른 사인값이 출력되어 나옵니다.




 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main()
{
 int a,i;
 srand(time(NULL));
 for(i = 1 ; i <= 10 ; i++)
 {
  a = rand() % 1000;
  printf("(%d)random number is %d\n",i,a);
  
 }
}
난수 기본 예제입니다.
srand(time(NULL)); 은 시드값을 초기화시켜주는 역할을 하며,
rand() % 1000; 부분은 0부터 999까지의 1000개의 정수 하나를 생성한다는 뜻입니다.
(for문에 의하여 정수는 10개가 출력되겠죠?)