Answer to Question #167117 in C++ for Hanzala

Question #167117

Write a C++ function to show all the INNOVATORS using one dimensional an•ay. An item value which is store in array is Innovator if it is greater than all the items to its left side. And the left most item is always an Innovator. For example, in the array {11, 21, 19, 33, 79, 41}, Innovator are 79,

21 and 11.


1
Expert's answer
2021-02-26T16:14:48-0500
#include <iostream>
#include <limits.h>
using namespace std;


//Function to print all elements which are greater than all elements present to their right
void findInnovators(int arr[], int n)
{
    //Store the max value encountered so far when looping
    int maxSoFar = INT_MIN;


    //Traverse the array from left to right
    for(int i = 0; i <= n; i++)
    {
        // f the current element is greater than the max so far print it and update maxSoFar variable
        if (arr[i] > maxSoFar)
        {
            maxSoFar = arr[i];
            cout<<arr[i]<<" ";


        }
    }
}


int main(void)
{
    int arr[] = { 11, 21, 19, 33, 79, 41 };
    int n = sizeof(arr)/sizeof(arr[0]);


    findInnovators(arr, n);


    return 0;

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment