동적할당 어디가 잘못된걸까요.
츠키
질문 제목 : 포인트터 변수와 동적할당텍스트 파일을 읽어서 동적할당한다음
결과값을 텍스트로 출력하는겁니다. 질문 내용 :
#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 닫음
}
붉은색으로 써져있는곳에서부터 작동중지 되버리더군요..
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2691678 | C언어 함수 질문이요~!!! | 연보라 | 2025-04-15 |
2691650 | 반복문 | 돋가이 | 2025-04-15 |
2691618 | 링크드리스트 개념 질문이예요 (3) | 맨마루 | 2025-04-15 |
2691592 | 동적할당 이용 배열선언 질문입니다.ㅠㅠ (3) | 허리달 | 2025-04-15 |
2691542 | /=의 용도를 알려주세요 ㅠㅠ! (2) | 아라 | 2025-04-14 |
2691510 | sizeof 연산자 질문입니다 (2) | 종달 | 2025-04-14 |
2691483 | 파일 오픈시 에러 질문드립니다. (2) | 호습다 | 2025-04-14 |
2691450 | [visual c++ 툴]기초 질문 (3) | 해긴 | 2025-04-13 |
2691393 | UNIX 시스템을 사용하려면 어떤 프로그램이 좋을까요? (5) | 든솔 | 2025-04-13 |
2691334 | ㅠㅠ에러 (1) | Loseless | 2025-04-12 |
2691304 | 포인터배열에 대해 질문요 | 달님 | 2025-04-12 |
2691279 | float-정수변환-2진수변환 | 핫블루 | 2025-04-12 |
2691224 | 인자관련 질문입니다. (2) | 아잉형님 | 2025-04-11 |
2691196 | 원형 큐와 스택에서의 문제해결 (2) | 반혈 | 2025-04-11 |
2691169 | 동적할당 어디가 잘못된걸까요. | 츠키 | 2025-04-11 |
2691082 | puts vs printf | 리네 | 2025-04-10 |
2691058 | (C언어) 구구단을 작성해 봤는데 문제가 발생했습니다. (6) | 제나 | 2025-04-10 |
2690947 | 윈도우 메모장에서 버츄얼박스 우분투 붙여넣기 질문 (1) | 유우 | 2025-04-09 |
2690920 | 무슨 의미 인가요? | 딸기맛캔디 | 2025-04-09 |
2690891 | c++ 은행계좌 프로그램 | 알찬마루 | 2025-04-08 |