
Задан одномерный массив А(N) (N<=60). Найти сумму положительных элементов и произведение
отрицательных массива. Если сумма больше произведения, найти максимальный элемент массива, а если нет – выдать об этом сообщение

Ответы на вопрос

Создаёшь массив, заполняешь его, считаешь сумму и произведение, и обрабатываешь условие:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#define RANDOM_MIN -9
#define RANDOM_MAX 9
int main(void) {
srand(time(NULL)); /* for "visible" random */
int N;
scanf("%d", &N);
int *A = malloc(sizeof(int) * N);
int sum = 0;
int prd = 1;
printf("elements: ");
int i;
for (i = 0; i < N; i++) {
A[i] = RANDOM_MIN + rand() % (RANDOM_MAX + abs(RANDOM_MIN) + 1);
printf("%d ", A[i]);
if (A[i] > 0) {
sum += A[i];
} else if (A[i] < 0) {
prd *= A[i];
}
}
printf("\n");
printf("summa: %d\n", sum);
printf("product: %d\n", prd);
if (sum > prd) {
int max = A[0];
for (i = (0 + 1); i < N; i++) {
if (max < A[i]) {
max = A[i];
}
}
printf("max element is: %d", max);
} else {
printf("product higher than summa");
}
getch();
free (A);
return 0;
}



Task: Finding the sum of positive elements and the product of negative elements in an array
To find the sum of positive elements and the product of negative elements in a one-dimensional array, you can follow these steps:
1. Initialize variables for the sum of positive elements and the product of negative elements. 2. Iterate through each element in the array. 3. If the element is positive, add it to the sum of positive elements. 4. If the element is negative, multiply it with the product of negative elements. 5. After iterating through all the elements, compare the sum of positive elements with the product of negative elements. 6. If the sum is greater than the product, find the maximum element in the array. 7. If the sum is not greater than the product, display a message indicating that there is no maximum element.
Here's an example implementation in C++:
```cpp
#include
int main() { const int N = 60; // maximum size of the array int A[N]; // one-dimensional array int sumPositive = 0; int productNegative = 1; int maxElement = INT_MIN; // initialize with the minimum possible value
// code to populate the array A with values
for (int i = 0; i < N; i++) { if (A[i] > 0) { sumPositive += A[i]; } else if (A[i] < 0) { productNegative *= A[i]; }
if (A[i] > maxElement) { maxElement = A[i]; } }
if (sumPositive > productNegative) { std::cout << "The sum of positive elements is greater than the product of negative elements." << std::endl; std::cout << "The maximum element in the array is: " << maxElement << std::endl; } else { std::cout << "The sum of positive elements is not greater than the product of negative elements." << std::endl; std::cout << "There is no maximum element in the array." << std::endl; }
return 0; } ```
Please note that the code provided is just an example and assumes that the array `A` has already been populated with values. You may need to modify the code to suit your specific requirements and programming language.
I hope this helps! Let me know if you have any further questions.


Топ вопросов за вчера в категории Информатика







Последние заданные вопросы в категории Информатика
-
Математика
-
Литература
-
Алгебра
-
Русский язык
-
Геометрия
-
Английский язык
-
Химия
-
Физика
-
Биология
-
Другие предметы
-
История
-
Обществознание
-
Окружающий мир
-
География
-
Українська мова
-
Информатика
-
Українська література
-
Қазақ тiлi
-
Экономика
-
Музыка
-
Право
-
Беларуская мова
-
Французский язык
-
Немецкий язык
-
МХК
-
ОБЖ
-
Психология
-
Физкультура и спорт
-
Астрономия
-
Кыргыз тили
-
Оʻzbek tili