Thread: Recursion problem part 2...help

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    53

    Recursion problem part 2...help

    Hi;

    I have a linked list working but now the problem is to calculate the total of products i have using recursion.

    So my problem is here.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    typedef struct loja{
        char marca[100];
        char car[10];
        int stock;
        float preco;
    }INFO;
    
    
    typedef struct elem
    {
        INFO nodo;
        struct elem *Prox;
    }ELEM;
    
    
    int insElectrodomestico(INFO info, ELEM **initlista)
    {
        ELEM *novo, *aux;
        novo=(ELEM*) malloc(sizeof(ELEM));
        if(novo==NULL)
        {
            printf("Erro na lista");
            return -1;
        }
        novo->nodo=info;//O que se criar novo pertence a estrutura info
        novo->Prox=NULL;//O proximo elemente fica NULL
        if(*initlista==NULL)
        {
            *initlista=novo;//Se for NULL inicia a lista
            return 0;
        }
        aux=*initlista; //Inicia a lista atraves do aux
        while(aux->Prox!=NULL) //Enquanto que aux->prox seja diferente de NULL
        {
            aux=aux->Prox; //aux vai receber aux->prox
        }
        aux->Prox=novo; //Deixa preparado novo, para receber valor de aux->novo
    return 0;
    }
    
    //My main problem is here
    //I read the list, then if the stock is <1, quit
    //else, i read the stock, and use recursion
    int totEletrodomesticos(INFO info,ELEM *initlista)
    {
        ELEM *aux;
        aux->nodo=info;
        aux->Prox=NULL;
        if(aux==NULL)
        {
            printf("Lista vazia...");
            exit(1);
        }
        if(info.stock<1)
        {
            return -1;
        }
        else
        {
            return(info.stock+totEletrodomesticos(info.stock-1));
        }
    }
    
    
    
    
    int main()
    {
        ELEM *initlista=NULL;  // ---Declarar um aponta para o primeiro elemento da lista
        INFO info; //Chama a lista
        int op;
        do
        {
            printf("Quer registar: 1- SIM, 0-NAO \n");
            scanf("%d", &op);
            getchar();
            printf("Brand: \n");
            gets(info.marca); 
            printf("Class: \n");
            gets(info.car);
            printf("Stock: \n");
            scanf("%d", &info.stock);
            getchar();
            printf("Price: \n");
            scanf("%f", &info.preco);
            getchar();
            insElectrodomestico(info,&initlista);
            totEletrodomesticos(info, &initlista);
        }
        while(op!=0);
        return 0;
    }
    I have all this errors caused by recursion function



    error: incompatible type for argument 1 of 'totEletrodomesticos'|
    note: expected 'INFO' but argument is of type 'int'|
    error: too few arguments to function 'totEletrodomesticos'|
    D:\2012\c_exe\Pergunta 3 b\main.c|42|note: declared here|
    In function 'main':|
    warning: passing argument 2 of 'totEletrodomesticos' from incompatible pointer type [enabled by default]|
    D:\2012\c_exe\Pergunta 3 b\main.c|42|note: expected 'struct ELEM *' but argument is of type 'struct ELEM **'|
    In function 'totEletrodomesticos':|


    ||=== Build finished: 5 errors, 2 warnings ===|

    Honestly...i'm lost

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Go through the error messages one by one to solve these problems. For instance:

    This function is expecting a pointer of type ELEM:

    Code:
    int totEletrodomesticos(INFO info,ELEM *initlista)
    In "main()", you declare a pointer of type ELEM:

    Code:
    ELEM *initlista=NULL;
    And then you try to pass the address of (&) the pointer to the function:

    Code:
    totEletrodomesticos(info, &initlista);
    You only need to pass the pointer (remove the &). If "initlista" were a non-pointer variable, then you would need to send the address to that function.
    Last edited by Matticus; 07-11-2012 at 08:20 AM. Reason: clarity

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    53
    Hi Matticus

    Thanks, i saw that and o took out
    totEletrodomesticos(info, &initlista);

    to

    totEletrodomesticos(info, initlista);

    but still have 4 errors

    D:\2012\c_exe\Pergunta 3 b\main.c||In function 'totEletrodomesticos':|
    D:\2012\c_exe\Pergunta 3 b\main.c|58|error: incompatible type for argument 1 of 'totEletrodomesticos'|
    D:\2012\c_exe\Pergunta 3 b\main.c|42|note: expected 'INFO' but argument is of type 'int'|
    D:\2012\c_exe\Pergunta 3 b\main.c|58|error: too few arguments to function 'totEletrodomesticos'|
    D:\2012\c_exe\Pergunta 3 b\main.c|42|note: declared here|
    ||=== Build finished: 4 errors, 0 warnings ===|



    Well, one by one

    58|error: incompatible type for argument 1 of 'totEletrodomesticos'|
    Is this line
    return(info.stock+totEletrodomesticos(info.stock-1));

    note: expected 'INFO' but argument is of type 'int
    int totEletrodomesticos(INFO info,ELEM *initlista)
    All the error are in thos lines, for instance, it says too few arguments but in the main function i just have to pass, info and initlista, so why there is a error there??

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    53
    Fixed

    Code:
    int totEletrodomesticos(ELEM *initlista)
    {
        if(initlista==NULL) return 0;
        return (initlista->nodo.stock+totEletrodomesticos(initlista->Prox));
    }

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Excellent - it's working now?

  6. #6
    Registered User
    Join Date
    Jul 2012
    Posts
    53
    Yes, i wasn't thinking well about what data i should looking for, he can't be just livros->stock since i'm working with a variable aux.

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in the while part
    By abhishekabby in forum C Programming
    Replies: 6
    Last Post: 09-17-2010, 11:56 AM
  2. A problem in part of array.
    By dirtydrummer in forum C Programming
    Replies: 3
    Last Post: 04-01-2008, 02:04 AM
  3. Problem with part of program
    By ammochck21 in forum C++ Programming
    Replies: 8
    Last Post: 11-09-2006, 06:45 AM
  4. Part of ATM problem
    By 1rwhites in forum C Programming
    Replies: 9
    Last Post: 10-28-2005, 11:29 AM
  5. STACK problem part II
    By sballew in forum C Programming
    Replies: 7
    Last Post: 12-01-2001, 09:31 PM