Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,026 questions

51,982 answers

573 users

How to find max in struct array using C

1 Answer

0 votes
#include <stdio.h> 
#include <limits.h> 
 
struct Test { 
    float book_price; 
    int book_tax; 
}; 

int max(int mx, int tmp) {
    if (mx > tmp)
        return mx;
    return tmp;
}

int find_max(struct Test arr[], int len) { 
    int mx = INT_MIN; 
     
    for (int i = 0; i < len; i++) { 
        int tmp = arr[i].book_price + arr[i].book_tax; 
        mx = max(mx, tmp); 
    } 
    return mx; 
} 
   
int main() 
{ 
    struct Test arr[] = { 
        { 4, 6 }, 
        { 1, 7 }, 
        { 11, 5 }, 
        { 8, 6 }, 
        { 2, 3 }, 
        { 7, 5 } 
    }; 
     
    int result = find_max(arr, sizeof arr / sizeof arr[0]); 
     
    printf("%i", result); 
     
    return 0; 
} 
 
 
 
/*
run:
 
16
 
*/

 



answered May 6, 2019 by avibootz

Related questions

1 answer 135 views
135 views asked May 6, 2019 by avibootz
1 answer 140 views
1 answer 127 views
127 views asked Mar 7, 2024 by avibootz
3 answers 1,571 views
1 answer 62 views
1 answer 69 views
...