Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

56 строки
1.3 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
float A, B;
int n;
// Input values A and B
printf("Enter value A: ");
scanf("%f", &A);
printf("Enter value B: ");
scanf("%f", &B);
// Input size of vector X
printf("Enter size of vector X: ");
scanf("%d", &n);
// Create vector X
float* X = (float*)malloc(n * sizeof(float));
if (X == NULL) {
printf("Memory allocation error.\n");
return 1; // Exit the program with an error
}
// Input elements of vector X
for (int i = 0; i < n; i++) {
printf("Enter element X[%d]: ", i);
scanf("%f", &X[i]);
}
// Initialize sum and counter
float total_sum = 0;
int count = 0;
// Iterate over elements of vector X and check condition
for (int i = 0; i < n; i++) {
if (fabs(X[i] - A) < B) { // Check condition
total_sum += X[i]; // Sum the suitable element
count++; // Increase the counter
}
}
// Output results
printf("Sum: %.2f\n", total_sum);
printf("Count of elements: %d\n", count);
// Free allocated memory
free(X);
printf("Press any key to exit...\n");
getchar(); // To consume the newline character left by previous input
getchar(); // Wait for user input
return 0; // Exit the program
}