[완료] rand 함수가 이상해요...
글쓴이: nathaniel7687 / 작성시간: 월, 2012/05/21 - 2:00오후
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define CLASSES 3
#define STUDENTS 5
#define ROWS 3
#define COLS 3
int dice_func(void);
int counter(void);
int ave(void);
int main(void)
{
srand(time(NULL));
int A[ROWS][COLS]={
{2,3,0},
{3,4,5},
{3,4,2}
};
int B[ROWS][COLS]={
{5,3,0},
{3,6,5},
{3,3,2}
};
int C[ROWS][COLS];
int row,col;
for(row=0;row<ROWS;row++)
for(col=0;col<COLS;col++)
C[row][col] = A[row][col] + B[row][col];
for(row=0;row<ROWS;row++)
{
for(col=0;col<COLS;col++)
printf("%d",C[row][col]);
printf("\n");
}
dice_func();
printf("\n");
counter();
printf("\n");
ave();
return 0;
}
int dice_func(void)
{
int count[6] = {0};
int i, temp = 0;
for(i=0;i<100;i++)
{
temp = rand() & 5;
count[temp]++;
}
printf("주사위면\t\t\t빈도\n");
for(i=0;i<6;i++)
{
printf("%d\t\t\t%6d회 출현 \n",i+1,count[i]);
}
return 0;
}
int counter(void)
{
int count[10] = {0,};
int i, temp;
int max;
for(i=0;i<100;i++)
{
temp = rand() & 9;
count[temp]++;
}
max=0;
for(i=1;i<10;i++)
{
if(count[i] > count[max])
max = i;
}
for(i=0;i<10;i++)
{
printf("%d=>%d회 출현 \n",i,count[i]);
}
printf("제일많이나온값%d, %d출현\n",max,count[max]);
return 0;
}
int ave()
{
int s[CLASSES][STUDENTS]={
{0,1,2,3,4},
{10,11,12,13,14},
{20,21,22,23,24},
};
int clas, student,total,subtotal;
total = 0;
for(clas=0;clas<CLASSES;clas++)
{
subtotal = 0;
for(student = 0;student<STUDENTS;student++)
{
subtotal+=s[clas][student];
}printf("학급%d의평균은%d입니다.\n",clas,subtotal/STUDENTS);
}
printf("전체 학생 평균 성적은%d\n",total/(CLASSES*STUDENTS));
return 0;
}여기서 rand 함수를 사용했는데요 문제점은.... 출력 결과가 아래와 같다는 겁니다 ...
도대체 왜 주사위 면과 그 아래 0~9 부분에서 왜 3,4 가 0회 출현이 되고 2~7부분에 0회 출현이 되는지 모르겠습니다.
760 61010 674 주사위면 빈도 1 24회 출현 2 33회 출현 3 0회 출현 4 0회 출현 5 17회 출현 6 26회 출현 0=>20회 출현 1=>23회 출현 2=>0회 출현 3=>0회 출현 4=>0회 출현 5=>0회 출현 6=>0회 출현 7=>0회 출현 8=>32회 출현 9=>25회 출현 제일많이나온값8, 32출현 학급0의평균은2입니다. 학급1의평균은12입니다. 학급2의평균은22입니다. 전체 학생 평균 성적은0 계속하려면 아무 키나 누르십시오 . . .
Forums:


_
temp = rand() & 5;
temp = rand() & 9;
비트 레벨 and가 들어가 있네요.
아하 ... 그랬군요...... 감사합니다.
아하 ... 그랬군요...... 감사합니다.
댓글 달기