-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrayo.c
220 lines (187 loc) Β· 6.75 KB
/
arrayo.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
// Function declarations
int max(int len, int alpha[]);
int min(int len, int alpha[]);
double avg(int len, int *alpha);
int gap(int len, int *alpha);
int second_maximum(int len, int *alpha);
int *rev(int *alpha, int len);
int *every_second(int len, int *alpha);
int *even_numbers(int len, int *alpha);
int *new_array(int len, int *alpha);
int main(void)
{
// Prompt the user to enter the number of elements, ensuring it's between 1 and 100 π
int n;
do
{
printf("Please give me a number between 1 and 100 π: ");
scanf("%i", &n);
}
while (n <= 0 || n > 100);
// Dynamically allocate memory to store the elements in an array π¦
int *alpha = malloc(n * sizeof(int));
if (alpha == NULL) {
fprintf(stderr, "Memory allocation failed π₯\n");
return 1;
}
// Collect the elements from the user and store them in the array π’
printf("Enter %d elements π’: ", n);
for (int i = 0; i < n; i++)
{
scanf("%i", &alpha[i]);
}
// Display the elements stored in the array π₯οΈ
printf("Array π₯οΈ: ");
for (int i = 0; i < n; i++)
{
printf("%i, ", alpha[i]);
}
printf("\n");
// Perform calculations and display the results π
printf("The maximum is: %i π\n", max(n, alpha));
printf("The minimum is: %i π\n", min(n, alpha));
printf("The average is: %.2lf π\n", avg(n, alpha));
printf("The gap between maximum and minimum is: %i π\n", gap(n, alpha));
printf("The second maximum is: %i π₯\n", second_maximum(n, alpha));
// Call functions that manipulate the array and display their results π
rev(alpha, n);
every_second(n, alpha);
even_numbers(n, alpha);
new_array(n, alpha);
// Free the allocated memory to prevent memory leaks π§Ή
free(alpha);
}
// Function to find the maximum value in an array π
int max(int len, int alpha[])
{
int maximum = INT_MIN; // Initialize to the smallest possible value to handle all possible int values
for (int i = 0; i < len; i++)
{
if (alpha[i] > maximum)
{
maximum = alpha[i]; // Update maximum if current element is larger
}
}
return maximum; // Return the maximum value found
}
// Function to find the minimum value in an array π
int min(int len, int alpha[])
{
if (len == 0) return INT_MAX; // Return the largest possible int value if array is empty
int minimum = alpha[0]; // Initialize minimum with the first element of the array
for (int i = 1; i < len; i++)
{
if (alpha[i] < minimum)
{
minimum = alpha[i]; // Update minimum if current element is smaller
}
}
return minimum; // Return the minimum value found
}
// Function to calculate the average value of an array's elements π
double avg(int len, int *alpha)
{
double sum = 0;
for (int i = 0; i < len; i++)
{
sum += alpha[i]; // Sum all elements
}
return len > 0 ? sum / len : 0; // Return the average or 0 for empty array
}
// Function to calculate the gap (difference between max and min) in an array π
int gap(int len, int *alpha)
{
return max(len, alpha) - min(len, alpha); // Calculate and return the gap
}
// Function to find the second maximum value in an array π₯
int second_maximum(int len, int *alpha) {
int maximum = max(len, alpha); // First find the maximum value
int maximum2 = INT_MIN; // Initialize to the smallest possible value
for (int i = 0; i < len; i++) {
// Check if the current element is greater than maximum2 and not equal to the maximum value
if (alpha[i] > maximum2 && alpha[i] < maximum) {
maximum2 = alpha[i]; // Update second maximum
}
}
return maximum2; // Return the second maximum value found
}
// Function to reverse the array π
int *rev(int *alpha, int len) {
int *reversed = malloc(len * sizeof(int)); // Allocate memory for the reversed array
if (reversed == NULL) {
fprintf(stderr, "Memory allocation failed π₯\n");
return NULL; // Return NULL if memory allocation fails
}
for (int i = len - 1, j = 0; i >= 0; i--, j++) {
reversed[j] = alpha[i]; // Reverse the array elements
}
return reversed; // Return the reversed array
}
// Function to return an array with every second element of the input array βοΈ
int *every_second(int len, int *alpha) {
int *second_elements = malloc((len / 2 + len % 2) * sizeof(int)); // Allocate memory for the new array
if (second_elements == NULL) {
fprintf(stderr, "Memory allocation failed π₯\n");
return NULL; // Return NULL if memory allocation fails
}
for (int i = 1, j = 0; i < len; i += 2, j++) {
second_elements[j] = alpha[i]; // Copy every second element
}
return second_elements; // Return the new array
}
// Function to filter out even numbers from an array π
int *even_numbers(int len, int *alpha) {
int counter = 0; // Count how many even numbers are there
// Count even numbers
for (int i = 0; i < len; i++) {
if (alpha[i] % 2 == 0) {
counter++;
}
}
int *even = malloc(counter * sizeof(int)); // Allocate memory for even numbers
if (even == NULL) {
fprintf(stderr, "Memory allocation failed π₯\n");
return NULL; // Return NULL if memory allocation fails
}
for (int i = 0, j = 0; i < len; i++) {
if (alpha[i] % 2 == 0) {
even[j++] = alpha[i]; // Store even numbers
}
}
// Display the array with even numbers
printf("Array with even numbers is: ");
for (int i = 0; i < counter; i++) {
printf("%i ", even[i]);
}
printf("\n");
return even;
}
// Function to merge two halves of an array into a new array alternately π
int *new_array(int len, int *alpha) {
int new_len = ceil(len / 2.0); // Determine the new length based on half of the original length
int *new_alpha = malloc(len * sizeof(int)); // Allocate memory for the new array
if (new_alpha == NULL) {
fprintf(stderr, "Memory allocation failed π₯\n");
return NULL; // Return NULL if memory allocation fails
}
int counter = 0; // Counter for the new array index
// Merge two halves alternately
for (int i = 0, j = new_len; i < new_len; i++, counter++) {
new_alpha[counter] = alpha[i]; // First half element
if (j < len) {
new_alpha[++counter] = alpha[j++]; // Second half element
}
}
// Display the merged array
printf("Merged arrays: ");
for (int i = 0; i < len; i++) {
printf("%i ", new_alpha[i]);
}
printf("\n");
return new_alpha;
}