Thread: Birth 2 Parents 1 Son - LinkedList

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    61

    Birth 2 Parents 1 Son - LinkedList

    Better way to get two animals on a linkedlist

    I want to make a function that give “birth of a son”, frist i need to choose two parents.

    What is the best approach ?

    I already got one function working with just 1 parent.

    With two parents the only approach is with 2 pointers ?

    Code:
    struct SPECIES {
        char especie[MAX];
        char idalphanumeric[MAX]; // Alphanumeric ID
        long int nranimais; // Se for 0 não aparece nas espécies existentes.   
        pno listadeanimais;
        pspecies prox;
    };
    
    
    struct ANIMAL {
        char especie[MAX]; // ID Unico
        int id; // id único por animal        
        char nome[MAX]; // Nome do animal
        int peso;
        char nomelocal[MAX]; // Area onde o animal se encontra.
        pno prox; // Ponteiro para proximo no da lista  
    
    
        // Ramos familaires 1 pai,1 mae, nFilhos ?
    };

    Code:
    species * birth2animals(species * listoriginal, Area * listareas, int total) {
    
    
        species *list = listoriginal;
        species buffer;
    
    
        no *aux, *aux2, bufferanimal;
    
    
        char progenitor[MAX];
        char progenitor2[MAX];
    
    
        strncpy(progenitor, "ZFixe", MAX);
        strncpy(progenitor, "Simba", MAX);
    
    
        // 1 Progenitor
        while (list != NULL) {
            aux = list->listadeanimais;
            while (aux != NULL) {
                if (!strcmp(aux->nome, progenitor)) { // Already got the 1 parent
    
    // Do some verification and stuff
    
    
                }
                aux = aux->prox;
            }
            list = list->prox;
        }
        return listoriginal;
    }

  2. #2
    Registered User
    Join Date
    May 2017
    Posts
    61
    What i got so far.

    Code:
    species * birth2animals(species * listoriginal, Area * listareas, int total) {
    
    
        species *list = listoriginal;
        species *list2 = listoriginal;
        species buffer;
    
    
        no *aux, *aux2, bufferanimal;
    
    
        char progenitor[MAX];
        char progenitor2[MAX];
    
    
        strncpy(progenitor, "Simba", MAX);
        strncpy(progenitor2, "ZFixe", MAX);
    
    
        int parentfind = 0;
        int parentfind2 = 0;
    
    
        // 1 Progenitor
        while (list != NULL && parentfind == 0) {
            aux = list->listadeanimais;
            while (aux != NULL) {
                if (!strcmp(aux->nome, progenitor)) { // Already got the 1 parent                
                    parentfind = 1;
                    break;
                }
                aux = aux->prox;
            }
            list = list->prox;
        }
        
        // 2 Progenitor
        while (list2 != NULL && parentfind2 == 0) {
            aux2 = list2->listadeanimais;
            while (aux2 != NULL) {
                if (!strcmp(aux2->nome, progenitor2)) { // Already got the 2 parent
                    parentfind2 = 1;
                    printf("Name 2 parent %s\n", aux2->nome);
    
    
                    break;
                }
                aux2 = aux2->prox;
            }
            list2 = list2->prox;
        }
    
    
        printf("Name 1 parent %s\n", aux->nome);
        printf("Name 2 parent %s\n", aux2->nome);
    
    
        return listoriginal;
    }

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by thinkabout
    Better way to get two animals on a linkedlist

    I want to make a function that give “birth of a son”, frist i need to choose two parents.

    What is the best approach ?
    Unfortunately, I don't understand what you mean by this. It would be a good idea to explain the context, e.g., what are you trying to model with "animals" and a linked list? What does "a son" and "parents" mean in the context of "animals" and this linked list?

    It may also be a good idea to translate the names used in your code into English.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > // 1 Progenitor
    > while (list != NULL && parentfind == 0)
    ..
    > // 2 Progenitor
    > while (list2 != NULL && parentfind2 == 0)
    Surely you want both parents to be from the same species?
    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.

  5. #5
    Registered User
    Join Date
    May 2017
    Posts
    61
    I got with working.

    Sorry for my poor English.

    My goal it was to choose two animals , and need to be the same specie to make a baby.

    The baby will weight 20% of the total weight of both parents.

    Parents stay with 80%.

    Code:
    species * birth2animals(species * listoriginal, Area * listareas, int total) {
    
    
        species *list = listoriginal;
        species *list2 = listoriginal;
        species buffer;
    
    
        no *aux, *aux2, bufferanimal;
    
    
        int id;
        int id2;
        char son[MAX];
    
    
        puts("Insert the ID of the parent");
        scanf("%d", &id);
        getchar();
    
    
        puts("Insert the ID of the parent 2");
        scanf("%d", &id2);
        getchar();
    
    
        puts("What is the name newborn ?");
        scanf(" %99{^\n]", son);
        getchar();
    
    
    
    
        int parentfind = 0;
        int parentfind2 = 0;
    
    
        // 1 Progenitor
        while (list != NULL && parentfind == 0) {
            aux = list->listadeanimais;
            while (aux != NULL) {
                if (aux->id == id) { // Already got the 1 parent                
                    parentfind = 1;
                    break;
                }
                aux = aux->prox;
            }
            // Not to count more species after i found the animal
            if (!parentfind) {
                list = list->prox;
            }
        }
    
    
    
    
        // 2 Progenitor
        while (list2 != NULL && parentfind2 == 0) {
            aux2 = list2->listadeanimais;
            while (aux2 != NULL) {
                if (aux2->id == id2) { // Already got the 2 parent
                    parentfind2 = 1;
                    break;
                }
                aux2 = aux2->prox;
            }
            // Not to count more species after i found the animal
            if (!parentfind2) {
                list2 = list2->prox;
            }
        }
    
    
        // Confirmation that you have 2 parents 
        if (aux == NULL || aux2 == NULL) {
            puts("One or two of the IDs does not exist");
            return listoriginal;
        }
    
    
        // Now i got the two parents i need to made some verifications.
        if (!strcmp(list->especie, list2->especie)) {// Same specie i can continue.
            if (!strcmp(aux->nomelocal, aux2->nomelocal)) { // Same area
                bufferanimal.peso = ((aux->peso * 0.20) + (aux2->peso * 0.20));
                strncpy(buffer.especie, list->especie, MAX);
                strncpy(bufferanimal.nomelocal, aux->nomelocal, MAX);
                strncpy(bufferanimal.nome, son, MAX);
    
    
                if (insereanimal(listoriginal, buffer, bufferanimal, listareas, total)) {
                    aux->peso = aux->peso * 0.80;
                    aux2->peso = aux2->peso * 0.80;
                    return listoriginal;
                } else {
                    puts("birth not possible");
                    // TODO - Repor o peso em caso de falha
                    return listoriginal;
                }
            } else {
                puts("not in the same local");
                return listoriginal;
            }
        } else {
            puts("Not the same specie");
    
    
            return listoriginal;
        }
    
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calculation birth date as floating number
    By Ph0x in forum C Programming
    Replies: 6
    Last Post: 09-25-2014, 09:09 PM
  2. Parents control over kids?
    By Leeman_s in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 12-13-2003, 01:52 PM
  3. Parents Divorce
    By Xei in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 04-04-2003, 01:26 AM
  4. cards for parents!! a success
    By josh nojones in forum C++ Programming
    Replies: 1
    Last Post: 02-04-2003, 01:21 PM
  5. cards for parents
    By josh nojones in forum Tech Board
    Replies: 3
    Last Post: 01-30-2003, 07:24 AM

Tags for this Thread