-
C] 정수배열의 원소 중에서, 짝수의 합과 홀수의 합을 구하는 함수Study/C 2008. 5. 9. 20:52This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
/* 4. 정수배열의 원소 중에서, 짝수의 합과 홀수의 합을 구하는 함수를 작성하라. 함수정의 시 아래의 코드가 포함되도록 하라. void sum(int a[], int n, // n is the size of a[] int *even_element_sum_ptr, int *odd_element_sum_ptr) { ... } */ #include <stdio.h> void sum(int a[], int n, int *even_element_sum_ptr, int *odd_element_sum_ptr) { int i; for(i=0;i<n;i++) { if(a[i]%2!=0) *odd_element_sum_ptr += a[i]; else *even_element_sum_ptr += a[i]; } } void main() { int a[]={2,5,3,4,2}; int n=5; int odd_element_sum=0; int even_element_sum=0; sum(a, n, &even_element_sum, &odd_element_sum); for(n=0; n<5; n++) printf("%d, ", a[n]); printf("\n"); printf("even : %d, odd : %d\n", even_element_sum, odd_element_sum); } 반응형'Study > C' 카테고리의 다른 글
C] 2차원 배열의 최대원소와 최소원소를 찾기 (0) 2008.05.25 C] 배열의 최대값, 최소값, 버블정렬 이용한 중간값, 삽입, 선택 정렬 (0) 2008.05.20 C] 세 개의 문자형 변수에 저장되어 있는 값을 알파벳 순서로 정렬하는 함수 (0) 2008.04.10 C] 다섯 개의 문자형 변수에 저장되어 있는 값을 원형(circular)으로 이동(shift) (0) 2008.04.10 C] 0 입력할 때까지 입력받은 숫자들의 최소, 최대, 평균 (0) 2008.03.24