1 / 3

최대치는 max 에 최대치의 배열 첨차는 index 에 저장이 되게 하려면 ?

최대치는 max 에 최대치의 배열 첨차는 index 에 저장이 되게 하려면 ?. void main() { int score[N] ={90,99,51,60,70}; int i , result, index; result = maximum (______, &index ); printf (" 최대치 : score[%d]=%d ", index , result); }. #include < stdio.h > #define N 5 void main() {

gefjun
Download Presentation

최대치는 max 에 최대치의 배열 첨차는 index 에 저장이 되게 하려면 ?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 최대치는 max에 최대치의 배열 첨차는 index에 저장이 되게 하려면? void main() { int score[N] ={90,99,51,60,70}; inti, result, index; result =maximum(______, &index); printf("최대치: score[%d]=%d ", index, result); } #include <stdio.h> #define N 5 void main() { int score[N]={90,99,51,60,70}; inti, max, index; max= score[0]; index=0; for ( i=1 ; i< N ; i++) { if (max < score[i]) { max = score[i]; index = i; } } printf("최대치: score[%d]=%d ", index, max); } // 전달된 배열의 최대치는리턴하고 최대치의 // 배열 첨자는 main의 index에저장되도록 // index를 가리키는 포인터 which를 선언 intmaximum( 배열선언 , int *which ) { int max; *which = 0 ; //첨자를 0으로 초기화 : }

  2. // maximum() 함수원형 선언 할 곳 void main() { int score[N] ={90,99,51,60,70}; inti, result, index; result =maximum( score, &index); printf("최대치: score[%d]=%d “, index, result); } score int maximum( intary[N] , int *which ) { int result, i; max = ary[0]; // max를 첫 원소로 초기화 *which = 0 ; // 첨자를 0으로 초기화 for ( i=1 ; i< N ; i++) { if (max < ary[i]) { max = ary[i]; *which = i; //변경된 최대치의 첨자를 보관 } } return max; } 함수는 한 개의 값만 리턴할수있으므로 최대치는 리턴하되, main()의 지역변수 index의 값을 함수 maximum에서 변경시킬 수 있도록 call-by-address를 이용해야 한다.

  3. int maximum( ); // 함수원형 선언 int score[N] ={90,99,51,60,70}; // 전역변수선언 intindex; 전역변수를 활용하여 call-by-address 이용하지 않고 결과 얻기 void main( ) { inti, result; result=maximum( ); printf("최대치: score[%d]=%d “, index, result); } score와 index를 전역변수로 선언하면 함수 호출시 인수로 전달할 필요가 없어진다. int maximum( ) { int max, i; // 전역배열 score와 전역변수 index를 직접 사용 가능 max = score[0];index = 0; for ( i=1 ; i< N ; i++) { if (max < score[i]) { max = score[i]; index = i; } } return max; }

More Related