Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*В одномерном массиве, состоящем из н вещ элементов, вычислить:
- 1)сумму отрицательных элементов
- 2)произведение элементов массива между максимальным и минимальным
- упорядочить элементы массива по возрастанию*/
- #include <iostream>
- #include <time.h>
- #include <conio.h>
- #include <cmath>
- #include <memory>
- #include<stdio.h>
- using namespace std;
- void input(float *arr, int N){
- for (int i = 0; i < N; i++)
- scanf_s("%f", &arr[i]);
- }
- void output(float *arr, int N){
- for (int i = 0; i < N; i++)
- printf("%6.2f ", arr[i]);
- printf("\n");
- }
- void sum(float *arr, int N) {
- float sum=0;
- for (int i = 0; i < N; ++i) {
- if (arr[i] < 0)
- sum += arr[i];
- }
- if (sum==0)
- cout << "\n" << "1) array hasn't negative items " << "\n";
- else
- cout << "\n" << "1) sum of negative numbers: " << sum << "\n";
- }
- void product(float *arr, int N){
- int indmin = 0, indmax = 0;
- float product = 1, min, max;
- min = arr[0];
- for (int i = 1; i < N; ++i) {
- if (min > arr[i]) {
- min = arr[i];
- indmin = i;
- }
- }
- max = arr[0];
- for (int i = 1; i < N; ++i) {
- if (max < arr[i]) {
- max = arr[i];
- indmax = i;
- }
- }
- if (abs(indmax - indmin) == 1) {
- cout << "2) max and min are near" << "\n\n";
- }
- else if (indmax > indmin){
- for (int i = indmin + 1; i < indmax; ++i)
- product *= arr[i];
- cout << "2) product between min and max:" << product << "\n\n";
- }
- else {
- for (int i = indmax + 1; i < indmin; ++i)
- product *= arr[i];
- cout << "2) product between max and min:" << product << "\n\n";
- }
- }
- void bubble_sort(float *arr, int N){
- for (int i = 0; i < N - 1; ++i) {
- for (int j = 0; j < N - 1; ++j) {
- if (arr[j] > arr[j + 1])
- swap(arr[j], arr[j + 1]);
- }
- }
- }
- void Shell_sort(float *a, int N){
- int k, l;
- float item;
- for (int step = N / 2; step>0; step /= 2)
- for (l = step; l < N; ++l) {
- item = a[l];
- for (k = l; k >= step; k -= step) {
- if (item > a[k - step])
- a[k] = a[k - step];
- else
- break;
- }
- a[k] = item;
- }
- }
- int main() {
- int N;
- cout << "size array: ";
- cin >> N;
- float *arr = new float[N];
- cout << "original array: " << "\n";
- input(arr, N);
- output(arr, N);
- sum(arr, N);
- product(arr, N);
- bubble_sort(arr, N);
- cout << "new array after bubble sort" << "\n";
- output(arr, N);
- Shell_sort(arr, N);
- cout << "\n\n" << "new array after Shell sort" << "\n";
- output(arr, N);
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement