c언어) 양수, 음수, 0 카운팅
언어/c언어2016. 6. 25. 20:38
배열에서 숫자를 입력하고 양수 , 음수 , 0, 의 갯수를 세어라
c언어
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include <stdio.h> #define _SIZE_ 10 int main (void) { int number[ _SIZE_ ] = {0, }; int i; int positive = 0; int negative = 0; int zeroCnt = 0; for ( i =0; i< _SIZE_; i++ ) { printf("%d data input : ", i); scanf("%d", &number[i]); } for ( i =0; i< _SIZE_; i++ ) { if (number[i] > 0) { positive ++; } else if (number[i] < 0) { negative ++; } else { zeroCnt ++; } } printf("positive number Cnt : %d \n", positive); printf("negative number Cnt : %d \n", negative); printf("zeroCnt number Cnt : %d \n", zeroCnt); return 0; } | cs |
파이썬) 코드는 특별히 예외처리 구문을 넣어봤다.
[ 코드 ]
ruby 구현
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | $prng = Random.new # global_variable class Stu attr_accessor :L def initialize @L = [] end def array_method for i in 1..10 @L << $prng.rand(-10..10) end end def print_method puts @L end end my_Object = Stu.new my_Object.array_method my_Object.print_method positive_count = 0 negative_count = 0 zero_count = 0 my_Object.L.each do |data| if data < 0 negative_count +=1 elsif data > 0 positive_count +=1 else zero_count +=1 end end puts " positive_count #{positive_count}" puts " negative_count #{negative_count}" puts " zero_count #{zero_count}" | cs |
'언어 > c언어' 카테고리의 다른 글
중복없이 랜덤 하게 숫자 뽑기 (0) | 2016.07.28 |
---|---|
네이버 지식인 답변준것 c언어 구구단 (0) | 2016.07.01 |
c언어 구현 linkedlist (0) | 2016.06.25 |
앞 문자열 대문자 로 ~~~ (0) | 2016.06.17 |
c언어 조건에 맞는 사람 찾기 (0) | 2016.06.12 |