Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <time.h>
- using namespace std;
- int main()
- {
- int arraySize, randRangeFrom, randRangeTo;
- cout << "Enter element count: ";
- cin >> arraySize;
- if (arraySize < 1) {
- cout << "\nCount must be posivive integer.\n\n";
- return 0;
- }
- float* arr = new float[arraySize];
- char v(0);
- cout << "\n\nHow would You like to fill in array, (M)anually or (A)utomatically? \n:";
- cin >> v;
- if (v == 'M') {
- for (int i = 0; i < arraySize; i++)
- {
- cout << "Enter element " << i + 1 << ": ";
- cin >> arr[i];
- }
- }
- else if (v == 'A') {
- cout << "Enter initial range value: ";
- cin >> randRangeFrom;
- cout << "Enter end range value: ";
- cin >> randRangeTo;
- cout << endl;
- srand(time(NULL));
- for (int i = 0; i < arraySize; i++)
- {
- arr[i] = (rand() % (randRangeTo * 10 - randRangeFrom * 10 + 1) + randRangeFrom * 10) / 10.0;
- }
- }
- else {
- cout << "\nI expected \"M\" or \"A\".\n";
- return 0;
- }
- cout << "Your array is ready:\n";
- for (int i = 0; i < arraySize; i++)
- {
- cout << "(" << i << ") " << arr[i] << " ";
- }
- cout << "\n\nNow starting our Variant 6 tasks:\n\n\n1. Find minimum array element by the module.\n";
- float min = fabs(arr[0]);
- for (int i = 1; i < arraySize; i++)
- {
- if (min > fabs(arr[i])) min = fabs(arr[i]);
- }
- cout << "Minimum array element by the module is: " << min;
- cout << "\n\n\n2. Calculate the sum of the elements between the first and the last positive elements of the array.\n";
- float sumBetween = 0;
- int firstPos = 0, lastPos = 0;
- bool isFirstPositiveFound = false;
- for (int i = 0; i < arraySize; i++) {
- if (arr[i] >= 0) {
- if (isFirstPositiveFound) {
- lastPos = i;
- }
- else {
- firstPos = i;
- isFirstPositiveFound = true;
- }
- }
- }
- for (int i = firstPos + 1; i < lastPos; i++) {
- sumBetween += arr[i];
- }
- cout << "The sum of the elements between the first (" << firstPos << ") and the last (" << lastPos << ") positives is: " << sumBetween;
- cout << "\n\n\n3. Reorder the array so that zero value elements are placed before all others.\n";
- for (int i = 0; i < arraySize; i++)
- {
- if (arr[i] == 0)
- {
- for (int j = i - 1; j >= 0; j--)
- if (arr[j] != 0)
- {
- float tmp = arr[j];
- arr[j] = arr[j + 1];
- arr[j + 1] = tmp;
- }
- }
- }
- for (int i = 0; i < arraySize; i++)
- {
- cout << arr[i] << " ";
- }
- cout << "\n\nIf there were zeroes, they are at the beginning of the array." << endl;
- delete[] arr;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement