将最大元素减半并创建一个新的c ++的动态数组

问题描述 投票:-1回答:1

所以任务是这样的:用户必须键入数组中元素的数量,它们必须为> 3并且<1024,数组中的元素必须为%2 == 0,因此用户键入元素,然后用户必须键入另一个数字,例如,如果数组是2 4 6 8 2且用户types 1,则程序必须在数组中找到最大的数字,一半是数字,程序应使用新的数组[ 2 4 6 4 4 2]但是如果用户键入3或另一个数字,程序应该找到最大的num,一半,然后再次找到最大的num,一半,以及类似的数字(它确实是用户输入了numer的数字。输入3->会找到最大的数字,一半,找到新的最大数字,一半,找到新的最大,一半。另外,如果数组中的数字仅为2 2 2 2,则程序应退出“重试”

到目前为止,我已经做到了,请帮助我,我是新手,需要帮助。

#include <iostream>

using namespace std;

int main ()

{

  int i,n, numa, half;

  int * p;

  cout << "How many numbers would you like to type? ";

  cin >> i;

  p= new (nothrow) int[i];

  if(i<4 || i>1024)
{

      cout<<"Invalid input!";

      return 1;

  }

  if (p == nullptr)

    cout << "Error: memory could not be allocated";

  else

  {

    for (n=0; n<i; n++)

    {

      cout << "Enter number: ";

      cin >> p[n];

    }

    cout<<"A<num>, enter a number: "; //thats how many times u have to find the biggest num and half it

    cin>>numa;

    for(i = 1; i < n; ++i)

    {

       if(p[0] < p[i])

           p[0]=p[i];
    }

    cout<<p[0]<<endl;

    half=p[0]/2;

    cout<<half<<endl;
  }


  delete[] p;

  return 0;

}
c++ arrays input dynamic output
1个回答
0
投票

在C ++中,动态“数组”将由std::vector处理。您可以在此处阅读更多信息。

您的问题/任务/要求不太清楚,很难理解。无论如何,据我了解,我已经将整个任务分解为较小的任务,并为您创建了一些演示。

如果不是您所期望的,请给我更好的描述,我将对其进行修改。

请参阅许多可能的解决方案之一:

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

constexpr size_t MinNumberElementsInVector = 4U;
constexpr size_t MaxNumberElementsInVector = 1024U;

size_t getNumberOfVectorElements() {
    // inform user, what to do
    size_t result{ 0U };

    do {
        std::cout << "\nPlease enter, how many numbers the test array should have.\n"
            "The given number must be >=" << MinNumberElementsInVector << "and <="
            << MaxNumberElementsInVector << ".\nPlease enter: ";

        // Get value from user and check range
        if (!((std::cin >> result) && (result >= MinNumberElementsInVector) && (result <= MaxNumberElementsInVector))) {
            // Error in input. Reset result to 0 and show error message
            result = 0U;
            std::cout << "\n\nError during input. Try Again.\n\n";
        }
    } while (0 == result);
    return result;
}
size_t getNumberOfActions() {
    // inform user, what to do
    size_t result{ 0U };

    do {
        std::cout << "\n\nPlease enter, how many times the half operation should be done.\n"
            "The given number must be > 0.\nPlease enter: ";

        // Get value from user and check range
        if (!((std::cin >> result) && (result > 0))) {
            // Error in input. Reset result to 0 and show error message
            result = 0U;
            std::cout << "\n\nError during input. Try Again.\n\n";
        }
    } while (0 == result);
    return result;
}

std::vector<int> getVectorWithEvenNumbers(size_t numberOfElements) {

    // Limit the number of elements to the correct range
    numberOfElements = std::clamp(numberOfElements, MinNumberElementsInVector, MaxNumberElementsInVector);

    // Resulting vector. Reserve space for "numberOfElements" numbers
    std::vector<int> result(numberOfElements);

    // Get numbers from user
    for (bool allEven = false; !allEven; ) {

        // Give info for user
        std::cout << "\n\nPlease enter " << numberOfElements << " even numbers:\n";

        // Read all values from user
        std::copy_n(std::istream_iterator<int>(std::cin), numberOfElements, result.begin());

        // Check, if all values are even

        allEven = std::all_of(result.begin(), result.end(), [](const int i) {return (i % 2) == 0; });

        // Error message, in case of odd values
        if (!allEven) std::cout << "\n\nError during input. Try Again.\n\n";
    }
    return result;
}

void halfMaxValues(std::vector<int>& values) {
    // Search max value
    std::vector<int>::iterator max = std::max_element(values.begin(), values.end());
    if ((max != values.end()) && ((*max > 2) || (*max < -2))) {
        int m = *max;
        // Half all found max values, if even
        std::for_each(values.begin(), values.end(), [&m](int& i) {if ((i % 2 == 0) && (i == m)) {i /= 2;}});
    }
}

// Show contents of vector on screen
inline void displayVector(std::vector<int> values) {
    std::cout << "\nResult:\n";
    std::copy(values.begin(), values.end(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << "\n";
}

int main() {

    // Get number of elements for our vector
    size_t numberOfVectorElements{ getNumberOfVectorElements() };

    // Get the elements and put them into the vector
    std::vector<int> values{ getVectorWithEvenNumbers(numberOfVectorElements) };

    // How many times should we perfom the action
    size_t numberOfActions{ getNumberOfActions() };

    while (numberOfActions-- && !std::all_of(values.begin(), values.end(), [](int i) {return (i == 2) || (i == -2) || (i % 2 == 1); })) {
        halfMaxValues(values);
        displayVector(values);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.