동적할당 어디가 잘못된걸까요.
츠키
질문 제목 : 포인트터 변수와 동적할당텍스트 파일을 읽어서 동적할당한다음
결과값을 텍스트로 출력하는겁니다. 질문 내용 :
#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 닫음
}
붉은색으로 써져있는곳에서부터 작동중지 되버리더군요..
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2693647 | 12.620000 을요 12.620 으로 어떻게 표현해요? (2) | 파도 | 2025-05-04 |
2693619 | 타이틀 코드.. | 단순드립 | 2025-05-03 |
2693591 | 컴파일 에러에서 질문드립니다 (3) | 게자리 | 2025-05-03 |
2693463 | 동적할당 이용시 fwrite사용을 어떻게 해야하나요..? (10) | 일본어못해요 | 2025-05-02 |
2693387 | 배열문제입니다 수정오류캡쳐했습니다 (6) | 연하얀 | 2025-05-01 |
2693356 | text 입출력 내림차순 질문입니다 ㅠ | 빛글 | 2025-05-01 |
2693328 | C언어를이용해서 .txt파일 외에 다른 확장자 파일 삭제가 가능한지.. (2) | 대나무 | 2025-05-01 |
2693299 | 파일입출력 바이너리파일 | 독특한 | 2025-04-30 |
2693273 | 오류 (1) | 귀1여운렩 | 2025-04-30 |
2693080 | visual studio 2008 express edition 등록키 말인데요 | 얀별 | 2025-04-28 |
2693053 | 배열, 구조체 관련 프로그래밍 질문드립니다. | 싸리 | 2025-04-28 |
2693025 | 프로그램을 짜봤는데요 ㅠㅠ | 상처입은마음 | 2025-04-28 |
2693001 | 워닝문제, 세그멘트결함문제 (1) | 월식 | 2025-04-28 |
2692979 | 라인한줄 이랑.. 소스 설명좀 부탁드려요.. | 이루리 | 2025-04-27 |
2692947 | 이 문제좀 풀어 주세요..ㅜㅜ (1) | 소리 | 2025-04-27 |
2692889 | 함수의 구조체 인자로 받아서 그 인자로 데이터 넣기... | 한뎃집 | 2025-04-27 |
2692862 | 성적 출력 하는 프로그램인데요~!!!도움좀 주세욤.ㅠ | 두빛나래 | 2025-04-26 |
2692831 | if 문 간단해요 빨리좀 ㅠㅠ | 이플 | 2025-04-26 |
2692805 | 실행파일이 이상해요 | 푸헷 | 2025-04-26 |
2692750 | 퀵정렬 질문이요 . | 동생몬 | 2025-04-25 |