Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Find max in struct array

  • Difficulty Level : Easy
  • Last Updated : 06 Jun, 2018

Given a struct array of type Height, find max

struct Height{
  int feet;
  int inches;
}

Question source : Microsoft Interview Experience Set 127 | (On-Campus for IDC)

The idea is simple, traverse the array, and keep track of max value
value of array element(in inches) = 12*feet + inches

// CPP program to return max
// in struct array
#include <iostream>
#include <climits>
using namespace std;

// struct Height
// 1 feet = 12 inches
struct Height {
    int feet;
    int inches;
};

// return max of the array
int findMax(Height arr[], int n)
{
    int mx = INT_MIN;
    for (int i = 0; i < n; i++) {
        int temp = 12 * (arr[i].feet) 
                     + arr[i].inches;
        mx = max(mx, temp);
    }
    return mx;
}

// driver program
int main()
{
    // initialize the array
    Height arr[] = {
        { 1, 3 },
        { 10, 5 },
        { 6, 8 },
        { 3, 7 },
        { 5, 9 }
    };
    int res = findMax(arr, 5);
    cout << "max :: " << res << endl;
    return 0;
}

Output:

max :: 125

This article is contributed by Mandeep Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!