-
Notifications
You must be signed in to change notification settings - Fork 3
/
sum of even numbers in an array.c
146 lines (129 loc) · 3.85 KB
/
sum of even numbers in an array.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#define _CRT_SECURE_NO_WARNINGS
/*
author: Dennis Folz
github profile : https://github.com/SchattenMonarch
date: 19.08.22
program: WAP to sum of even numbers in an array
Example 1
Input
4 9 5 80 10 2
Output
96
*/
#include <stdio.h>
#include <stdlib.h>
#define TRUE !0
#define FALSE 0
#define ARRAY_DEFAULT_SIZE ((int)20)
#define RANDOM_ARRAY ((unsigned char)'a')
#define ENTER_ELEMENTS ((unsigned char)'b')
typedef struct {
int* values;
int size;
int sum_of_even_numbers;
}array;
//clearStdinFromGarbage
//clears stdin from anything. Wrong user inputs will be erased.
void clearStdinFromGarbage(void) {
while (getchar() != '\n');
}
//createArray
//allocates memory for the array
array* createArray(int size) {
if (size <= 0)
return NULL;
array* arr = (array*)malloc(sizeof(array));
if (arr != NULL) {
arr->values = (int*)malloc(size * sizeof(int));
arr->size = size;
arr->sum_of_even_numbers = 0;
if (arr->values != NULL) {
for (int i = 0; i < size; i++) {
arr->values[i] = 0;
}
return arr;
}
}
return NULL;
}
//insertElement
//inserts an element to the given position into the array
int insertElement(array* arr, int pos, int value) {
if (arr == NULL) return FALSE;
if (pos < 0 || pos > arr->size) return FALSE;
arr->values[pos] = value;
return arr->values[pos];
}
//displayArray
//prints the elements of an array seperated by spaces
void displayArray(array* arr) {
if (arr == NULL) return;
for (int i = 0; i < arr->size; i++) {
printf("%d ", arr->values[i]);
}
return;
}
int is_even(int num) {
return num % 2 == 0 ? num : 0;
}
int main()
{
array* a;
unsigned char options = FALSE;
int input = FALSE, size = FALSE, randomVal = FALSE, val = FALSE;
time_t t;
/* Intializes random number generator */
srand((unsigned)time(&t));
puts("In this program we'll calculate the sum of the even integers in an array. "
"Choose an option a/b:\n a) Random array \n b) Enter elements");
while (scanf("%c", &options) != 1) {
clearStdinFromGarbage();
puts("Uuups ... somethin went wrong. Try again: ");
}
switch (options)
{
case RANDOM_ARRAY:
a = createArray(ARRAY_DEFAULT_SIZE);
for (int i = 0; i < a->size; i++) {
randomVal = (int)(rand() % 1000);
if (insertElement(a, i, randomVal) != randomVal) {
puts("Error: insertElement failed. Terminating program...");
return 0;
}
a->sum_of_even_numbers += is_even(randomVal);
}
puts("Printing random generated array...");
displayArray(a);
break;
case ENTER_ELEMENTS:
puts("How many elements do you want to enter?");
while (scanf("%d", &size) != 1) {
clearStdinFromGarbage();
puts("Uuups ... somethin went wrong. Try again: ");
}
a = createArray(size);
if (a != NULL) {
for (int i = 0; i < a->size; i++) {
printf("Enter element #%d: ", i + 1);
while (scanf("%d", &val) != 1) {
clearStdinFromGarbage();
puts("Uuups ... somethin went wrong. Try again: ");
}
if (insertElement(a, i, val) != val) {
puts("Error: insertElement failed. Terminating program...");
return 0;
}
a->sum_of_even_numbers += is_even(val);
}
puts("You entered the following numbers: ");
displayArray(a);
}
break;
default:
a = NULL;
puts("Invalid input. Terminating the program...");
break;
}
printf("The sum of the even elements in the array is: %d\n", a->sum_of_even_numbers);
return 0;
}