Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
array and its sum
#include <stdio.h> int is_even(int number); int is_odd(int number); int count_even_in_array(int a[]); int count_odd_in_array(int a[]); void print_numbers(int a[]); int sum_of_positive_even_numbers(int a[]); int sum_of_positive_odd_numbers(int a[]); int is_positive(int number); int main(void) { int a[10] = {-1, -2, 3, 4, -5, 6, 7, -8, 9,10}; print_numbers(a); return 0; } int is_even( int number) { if(number % 2 == 0) { return 1; } return 0; } int is_odd(int number) { if(number % 2 != 0) { return 1; } return 0; } int count_even_in_array(int a[]) { int i = 0, count_even_numbers = 0; for(i = 0; i < 10 ; i++) { if(is_even(a[i])) { count_even_numbers++; } } return count_even_numbers; } int count_odd_in_array(int a[]) { int i = 0, count_odd_numbers = 0; for(i = 0; i < 10 ; i++) { if(is_odd(a[i])) { count_odd_numbers++; } } return count_odd_numbers ; } void print_numbers(int a[]) { int i; printf("The elements in the array are"); for(i = 0; i < 9; i++) { printf(" %3d", a[i]); } printf(" and %d", a[i]); printf("\nThe even numbers in the array "); for(i = 0; i < 10 ; i++) { printf(" %d ", a[i]); } printf("= %d",count_even_in_array(a)); printf("\nThe odd numbers in the array "); for(i = 0;i < 10 ; i++) { printf(" %d ", a[i]); } printf("= %d", count_odd_in_array(a)); printf("\nThe sum of positive even numbers in the array is %d",sum_of_positive_even_numbers(a)); printf("\nThe sum of positive odd numbers in the array is %d",sum_of_positive_odd_numbers(a)); } int sum_of_positive_even_numbers(int a[]) { int i, sum = 0; for(i = 0; i < 10; i++) { if(is_even(a[i]) && is_positive(a[i] )) { sum = sum + a[i]; } } return sum; } int sum_of_positive_odd_numbers(int a[]) { int i, sum = 0; for(i = 0; i < 10; i++) { if(is_odd(a[i]) && is_positive(a[i] )) { sum = sum + a[i]; } } return sum; } int is_positive(int number) { if(number >= 1) { return 1; } return 0; }
run
|
edit
|
history
|
help
1
120465-11.1-3E
Exercicio de C - Vetor Multidimensional
B_141212_IntercambiarPunteros
a1
Character testing
NETFLIX TERMINADO
etapa final de pregunta 2
Stub Program for Problem 3 HW 2
Assignment 5 part 2
Intro to Arrays with comments