We need to help figure out what's wrong with the code.



  • There is a mission: Write a program that embodies an empty set of whole numbers from the keyboard, and prints the number of local peaks (the element is local maximum if it has no neighbours larger than it itself).

    #include <iostream>
    using namespace std;
    

    int main() {
    cout << "Vedite pazmepHoct macuva: ";
    int n = 0;
    cin >> n;
    int arr[n];
    for (int i = 0; i < n; i++) {
    cout << "arr[" << i << "] = ";
    cin >> arr[i];
    }
    for (int i = 0; i < n; i++) {
    if (i == 0) {
    if (arr[i] > arr[i + 1]) {
    cout << arr[i] << '\n';
    }
    } else if (i == n - 1) {
    if (arr[i] > arr[i - 1]) {
    cout << arr[i] << '\n';
    }
    } else {
    if (arr[i] > arr[i + 1] && arr[i] > arr[i - 1])
    cout << arr[i] << " ";
    }
    }
    cin.get();
    cin.get();
    }



  • You should. prints the number of local maximumsso we just need to count this number and get it out of here, not the numbers themselves.

    #include <iostream>
    using namespace std;
    int main() {
        cout << "Vedite pazmepHoct macuva: ";
        int n = 0;
        cin >> n;
        int* arr = new int[n];
    
    for (int i = 0; i &lt; n; i++) {
        cout &lt;&lt; "arr[" &lt;&lt; i &lt;&lt; "] = ";
        cin &gt;&gt; arr[i];
        }
    
    int max_count = 0;
    
    if (arr[0] &gt;= arr[1]) max_count++;
    
    if (arr[n - 1] &gt;= arr[n - 2]) max_count++;
    
    for (int i = 1; i &lt; n - 1; i++)
        if (arr[i] &gt;= arr[i + 1] &amp;&amp; arr[i] &gt;= arr[i - 1]) max_count++;
    
    cout &lt;&lt; max_count;
    }
    


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2