동적할당 어디가 잘못된걸까요.
츠키
질문 제목 : 포인트터 변수와 동적할당텍스트 파일을 읽어서 동적할당한다음
결과값을 텍스트로 출력하는겁니다. 질문 내용 :
#includestdio.h
#includestdlib.h
int c;
void input_score_file(file* infile,float score[],int n)//파일을 읽어 score[c]에 저장
{
for(c=0; cn; c++)
fscanf(infile,%f,score[c]);
}
void print_score(float score[],int n)//점수 출력
{
for(c=0;cn;c++)
printf(점수:%f\n,score[c]);
}
float max_score(float score[],int n)//최대점수
{
float max;
if(n==1){
return score[0];
}
max=max_score(score,n-1);
if(maxscore[n-1])
{
return score[n-1];
}
return max;
}
float min_score(float score[],int n)//최소점수
{
float min;
if(n==1){
return score[0];
}
min=min_score(score,n-1);
if(minscore[n-1])
{
return score[n-1];
}
return min;
}
float sum_score(float score[],int n)//총합점수
{
float sum=0;
if(n0){
sum=score[n-1]+sum_score(score,n-1);
}
return sum;
}
void print_result(float max,float min,float sum,float mean)//결과값 출력
{
printf(최댓값 : %f \n최소값 : %f\n합친값 : %f\n평균값 : %f,max,min,sum,mean);
}
void save_result_file(file* outfile,float max,float min,float sum,float mean)//reusult.txt에 저장값
{
fprintf(outfile, 최고점수: %f, max);
fprintf(outfile, 최소점수: %f, min);
fprintf(outfile, 총합점수: %f, sum);
fprintf(outfile, 평균점수: %f, mean);
}
int main(void){
int n=0;
float* score;
float scoren;
float max=0,min=0,sum=0,mean=0;
file *infile,*outfile;
infile=(file*)fopen(score.txt,r);//socre.txt 오픈
if(infile==null)
{
printf(에러: 입력 파일을 열 수 없읍!!\n\n);
return;
}
printf(학생수를 입력하세요:);
scanf(%d,&n);
score = (float *)malloc(n*sizeof(float));
if (score == null)
{
printf(에러: 동적 메모리 할당 실패!!!\n\n);
return;
}
input_score_file(infile,score,n);//파일을 읽어 score[c]에 저장
print_score(score,n);//점수 출력
fclose(infile);//score.txt 닫음
max=max_score(score,n);//최고점수
min=min_score(score,n);//최저점수
sum=sum_score(score,n);//총합계
mean=sum/n;//평균
free(score);
print_result(max,min,sum,mean);//결과값 출력
outfile=(file*)fopen(result.txt,w);//result.txt 생성
if(outfile==null)
{
printf(에러: 입력 파일을 열 수 없읍!!\n\n);
return;
}
save_result_file(outfile, max, min, sum, mean);//result.txt에 저장될 값
fclose(outfile);//result.txt 닫음
}
붉은색으로 써져있는곳에서부터 작동중지 되버리더군요..
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2698719 | 조건부컴파일 질문입니다.~ (2) | 큐트 | 2025-06-19 |
2698693 | 재귀 함수 에러 | 바닐라 | 2025-06-19 |
2698673 | 고민이있는데 들어좀주세요!! (1) | 초코맛캔디 | 2025-06-19 |
2698644 | 1부터 n까지의 합을 구하는데 엄청긴숫자의 합을 구할때는 어떻게 해야하나요? (4) | 슬우 | 2025-06-18 |
2698616 | 다른 함수로 안넘어갑니다..;;; | 도1도캣 | 2025-06-18 |
2698587 | 배열하다 막혀서... (3) | WhiteCat | 2025-06-18 |
2698559 | 문자열을 비우는방법 (2) | 하늘 | 2025-06-18 |
2698528 | 착하고 친절한 선생씌구해염~ㅋㅋ (4) | 옆집언니야 | 2025-06-17 |
2698502 | 자료구조 큐 | 캔서 | 2025-06-17 |
2698477 | 실행화면 배경문의요 | 선아 | 2025-06-17 |
2698430 | 변수의 값이 저장이 않되네요;; (4) | 피네 | 2025-06-16 |
2698404 | C#을 배울려고 하는데 C나 C++을 알아야 하나요 ?? (1) | 신당 | 2025-06-16 |
2698342 | 프로그램 질문점녀 (4) | 데빌의눈물 | 2025-06-16 |
2698318 | 파일 입출력 질문입니다~ (2) | 꽃 | 2025-06-15 |
2698291 | 문자 출력 함수 : putchar, fputc에 관하여. | 으뜸 | 2025-06-15 |
2698261 | 씨언어 (1) | 마리 | 2025-06-15 |
2698212 | 구조체, 포인터가 같이 들어간 프로그램 소스코드 있으신분? (4) | 그림자 | 2025-06-14 |
2698184 | 간단한 C언어 인데 .. | 붕붕 | 2025-06-14 |
2698120 | -연산자 가 먼지 좀 알려주세요 (1) | 낮선검객 | 2025-06-14 |
2698091 | 길찾기문제 질문이요! | 노을빛 | 2025-06-13 |