Tuesday, September 28, 2010

Add 3 number


Question : how to add 3 number with C++

void main()
{
    int num1, num2, num3;
    printf("세 개의 정수값을 입력하세요.\n");
    printf("num1 -> ");
    scanf("%d", &num1);
    printf("num2 -> ");
    scanf("%d", &num2);
    printf("num3 -> ");
    scanf("%d",&num3);
    printf("덧셈의 결과는 다음과 같습니다.\n");
    printf("%d + %d + %d= %d", num1, num2,num3, (num1 + num2 + num3));
//세가지를 더한다.
    //%d로 모양이 변한다.
}


10진수를 16진수로
16진수를 10진수로

void main()
{
    char key;
    int data;
    printf("10진수->16진수 변환 프로그램입니다.\n\n");
    printf("10진수를 16진수로 바꾸려면 a키를 누르고,\n");
    printf("16진수를 10진수로 바꾸려면 b키를 누르세요.\n");
    printf("a나 b키를 누르세요. : ");
    scanf("%c", &key);
    //입력을 &key 에 입력을 한다.
    printf("변환할 숫자를 입력하세요. : ");

    if(key == 'a') {
        scanf("%d", &data);
        printf("10진수 값 : %d --> 16진수 값 : %x\n", data, data);
    }
    else if(key == 'b') {
        scanf("%x", &data);
        printf("16진수 값 : %x --> 10진수 값 : %d\n", data, data);
    }
    else
        printf("a와 b값만을 사용해야 합니다.");
   
}



srand


functions
<cstdlib>
void srand ( unsigned int seed );

Initialize random number generator

The pseudo-random number generator is initialized using the argument passed as seed.

For every different seed value used in a call to srand, the pseudo-random number generator can be expected to generate a different succession of results in the subsequent calls to rand.
Two different initializations with the same seed, instructs the pseudo-random generator to generate the same succession of results for the subsequent calls to rand in both cases.

If seed is set to 1, the generator is reinitialized to its initial value and produces the same values as before any call to rand or srand.

In order to generate random-like numbers, srand is usually initialized to some distinctive value, like those related with the execution time. For example, the value returned by the function time (declared in header <ctime>) is different each second, which is distinctive enough for most randoming needs.

Parameters

seed
An integer value to be used as seed by the pseudo-random number generator algorithm.


Return Value

(none)

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* srand example */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main ()
{
printf ("First number: %d\n", rand() % 100);
srand ( time(NULL) );
printf ("Random number: %d\n", rand() % 100);
srand ( 1 );
printf ("Again the first number: %d\n", rand() %100);

return 0;
}


Output:

First number: 41
Random number: 13
Again the first number: 41


No comments:

Post a Comment