C/C++
Написать код на С++ в одномерном массиве
В одномерном массиве, состоящем из N вещественных элементов, найдите произведение отрицательных элементов массива, расположенных до минимального элемента. Если минимальный элемент – первый, или отрицательных элементов нет, то произведение вычислить нельзя (выведите соответствующее сообщение).
// способ 1
#include <iostream>
#include <numeric>
#include <ctime>
using namespace std;
int main() {
const int SIZE = 10;
double ar[SIZE];
srand(time(0));
for (double& i : ar) cout << (i = (rand() % 4000 - 2000) / 100.0) << " ";
cout << "\n\n";
bool b = false;
auto min = min_element(ar, ar+SIZE);
auto prod = accumulate(ar, min, 1.0,
[&b](double r, double x) {return (x < 0) ? (b = true, r*x) : (r);});
if (b) cout << "Product of negative numbers: " << prod << "\n";
else cout << "There are no negative elements\n";
}
// способ 2
#include <iostream>
#include <ctime>
using namespace std;
int main() {
double ar[50];
cout << "Enter a size of the array: ";
int size;
cin >> size;
srand(time(0));
for (int i = 0; i < size; ++i) cout << (ar[i] = (rand() % 2000 - 1000) / 100.0) << " ";
cout << "\n\n";
int ind = 0;
for (int i = 0; i < size; ++i) if(ar[i] < ar[ind]) ind = i;
double prod = 1.0;
bool b = false;
for (int i = 0; i < ind; ++i) if (ar[i] < 0) (prod *= ar[i], b = true);
if (b) cout << "Product of negative numbers: " << prod << "\n";
else cout << "There are no negative elements\n";
}
#include <iostream>
#include <numeric>
#include <ctime>
using namespace std;
int main() {
const int SIZE = 10;
double ar[SIZE];
srand(time(0));
for (double& i : ar) cout << (i = (rand() % 4000 - 2000) / 100.0) << " ";
cout << "\n\n";
bool b = false;
auto min = min_element(ar, ar+SIZE);
auto prod = accumulate(ar, min, 1.0,
[&b](double r, double x) {return (x < 0) ? (b = true, r*x) : (r);});
if (b) cout << "Product of negative numbers: " << prod << "\n";
else cout << "There are no negative elements\n";
}
// способ 2
#include <iostream>
#include <ctime>
using namespace std;
int main() {
double ar[50];
cout << "Enter a size of the array: ";
int size;
cin >> size;
srand(time(0));
for (int i = 0; i < size; ++i) cout << (ar[i] = (rand() % 2000 - 1000) / 100.0) << " ";
cout << "\n\n";
int ind = 0;
for (int i = 0; i < size; ++i) if(ar[i] < ar[ind]) ind = i;
double prod = 1.0;
bool b = false;
for (int i = 0; i < ind; ++i) if (ar[i] < 0) (prod *= ar[i], b = true);
if (b) cout << "Product of negative numbers: " << prod << "\n";
else cout << "There are no negative elements\n";
}
Рустем Альясов
А что значит rand()%2000-1000..?
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<unistd.h>
int main()
{
int n,index=-1;
float min=10.0;
double mult=1;
printf("\nEnter the dimension of array n = ");
scanf("%d",&n);
if(n==0 || n==1)
{
n=2;
printf("\nSet n=2...\n");
}
float* A=(float*)malloc(n*sizeof(float));
if(A)
{//if A
srand(time(NULL));
printf("\nStart array is:\n");
for(int i=0;i*A) {min=*A;index=i;}
A++;
}//for
A-=n;
if(min!=10.0 && index!=0)
printf("\n\nMin=%.2f, index=%d",min,index);
else
printf("\n\nMinimal digit not found\nor minimal index is 0");
if(index!=0 && index!=-1)
{//if
for(int i=0;i<index;i++)
{//for
mult=mult*(*A);
A++;
}//for
printf("\nMultiplication=%.2lf",mult);
}//if
free(A);
}//if A
else printf("\n Array not created");
printf("\n\n");
sleep(10);
return 0;
}

#include<stdlib.h>
#include<time.h>
#include<unistd.h>
int main()
{
int n,index=-1;
float min=10.0;
double mult=1;
printf("\nEnter the dimension of array n = ");
scanf("%d",&n);
if(n==0 || n==1)
{
n=2;
printf("\nSet n=2...\n");
}
float* A=(float*)malloc(n*sizeof(float));
if(A)
{//if A
srand(time(NULL));
printf("\nStart array is:\n");
for(int i=0;i*A) {min=*A;index=i;}
A++;
}//for
A-=n;
if(min!=10.0 && index!=0)
printf("\n\nMin=%.2f, index=%d",min,index);
else
printf("\n\nMinimal digit not found\nor minimal index is 0");
if(index!=0 && index!=-1)
{//if
for(int i=0;i<index;i++)
{//for
mult=mult*(*A);
A++;
}//for
printf("\nMultiplication=%.2lf",mult);
}//if
free(A);
}//if A
else printf("\n Array not created");
printf("\n\n");
sleep(10);
return 0;
}

Рустем Альясов
Просто Человеческое спасибо! Вы отличный человек
Похожие вопросы
- Написать программу «справочник». Создать два одномерных массива. Один массив хранит номера мобильных телефонов, второй
- Нужна помощь в составлении одномерных массивов С++
- C++\Работа с одномерными массивами: удаление, добавление, перестановка, поиск
- Написать программу на С++ с применением одномерных массивов, используя указатели.
- Одномерные массивы. Сортировка
- В одномерном массиве, состоящем из n вещественных элементов, вычислить сумму элементов массива
- Одномерные массивы на языке си
- C++ Обработка одномерных массивов.
- Задача на одномерные массивы С++
- Составить программу. В одномерном массиве произвольных чисел вычислить произведения четных и нечетных элементов.