Thread: Struct array and find max.

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    2

    Struct array and find max.

    Hello everyone. I am new to this forum and begginer in C programming.

    So I have to make a struct (named country) and I need to scanf the name of a country, population and the capital of a country. The problem is that I need to find the max population and printf the country and the capital of this.

    So I scanf USA, 100000, Washington and Greece, 1000, Athens etc.

    Then I have to printf USA, 100000, Washington.

    That's all I wrote.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define N 2
    
    struct country {
        char c_name[30];
        int population;
        char capital[30];
    };
    
    /*
     * 
     */
    int main(int argc, char** argv) {
        
        struct country xwra[N];
        
        int max = 0;
        int i;
        
        for (i=0; i<N; i++){
            printf("\nGive country name.\t");
            scanf("%s", xwra[i].c_name);
            printf("\nGive the population.\t");
            scanf("%d", &xwra[i].population);
            printf("\nGive the capital.\t");
            scanf("%s", xwra[i].capital);
            if (xwra[i].population > max)
                max = xwra[i].population;
        }
        
        
    
        return (EXIT_SUCCESS);
    }
    So how to printf the max population with name of the country and the capital???
    Last edited by pelopidass; 12-07-2013 at 09:04 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Have two variables.

    int maxPop = 0;
    int maxPos = 0;

    MaxPop tracks the max population, pretty much like you're doing at the moment.
    MaxPos tracks the value of i, so you can get back to it later on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    2
    Quote Originally Posted by salem View Post
    have two variables.

    Int maxpop = 0;
    int maxpos = 0;

    maxpop tracks the max population, pretty much like you're doing at the moment.
    Maxpos tracks the value of i, so you can get back to it later on.
    solved!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't find struct sigevent_t
    By wirmius in forum C Programming
    Replies: 4
    Last Post: 01-22-2013, 08:51 AM
  2. Replies: 3
    Last Post: 12-09-2012, 01:51 PM
  3. Struct Program to find Point in Rectangle
    By DMJKobam in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2009, 08:56 PM
  4. How find a substring in struct
    By nick048 in forum C Programming
    Replies: 5
    Last Post: 04-07-2007, 10:03 AM
  5. How to find a string in a struct..
    By Gugge in forum C Programming
    Replies: 3
    Last Post: 03-09-2002, 05:41 PM