입력받은 값들 중 best - worst 값 출력하기
글쓴이: minjae0116 / 작성시간: 일, 2019/04/21 - 5:38오후
#include
#include
int main(){
int student;
int *scor;
int temp=0,i=0,best=0,worst=0;
scanf("%d",&student);
scor = (int*)malloc(sizeof(int)*sizeof(student));
for(i=0;i
scanf("%d",&temp);
scor[i] = temp;
}
worst = scor[0];
best = scor[0];
for(i=0;i<=sizeof(scor);i++){
if(scor[i]<=worst){
worst = scor[i];
}
if(scor[i]>=best){
best = scor[i];
}
}
temp = best - worst;
printf("%d",temp);
return 0;
}
위와 같은 코드를 작성해서 출력값을 확인한 결과 알맞은 수가 나왔습니다.
ex) student = 5; 입력 값 : 27 35 92 75 42
worst = 27 , best = 92 출력값 temp = 65;
그런데 code UP 컴파일러에서는 전혀다른수 132993 가 출력됩니다. 왜 이런현상이 발생하는지 알 수 있을까요...?
Forums:
scor = (int*)malloc(sizeof
잘 보세요. 왜 그럴까요?
sizeof 연산자를 오/남용하고 계시는군요. 이 연산자가 어떤 값을 주는지 다시 한 번 확인해보세요.
https://en.cppreference.com/w/c/language/sizeof
그리고 사실 잘 생각해보면 배열을 잡을 필요가
그리고 사실 잘 생각해보면 배열을 잡을 필요가 없습니다.
댓글 달기