Thread: Compilation error: how pass constants into function

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    12

    Compilation error: how pass constants into function

    Errors:
    Compilation error: how pass constants into function-untitled-png
    How can I pass constants into a function. I tried using ' ' but that also gave me error, so I removed them here.
    Why does it say cell undeclared?

    Code:
     #include<stdio.h>
    
    #define MAX 80
    
    
    void generate(int cell[], MAX);
    void printArray(int cell[], MAX);
    
    
    /*generate: current condition-->next generation
    printArray: print 1 as *, 0 as <spacei>*/
    
    
    int main(void)
    {
        int cell[MAX]={0}, n, gen, i;
    
    
        printf("Enter starting location and number of generations: ");
        scanf("%d %d", &n, &gen);
    
    
        cell[n]=1;
        printArray(cell, MAX);
        for(i=2;i<=gen; i++)
        {
            generate(cell, MAX);
            printArray(cell, MAX);
        }
        return 0;
    }
    void generate(int cell[], MAX){
        int n=0;
        for (n=0; n<MAX; n++)
        if(!((cell[n+1]) && (cell[n-1])) && ((cell[n+1]) || (cell[n-1])))
            cell[n]=1;
            return;
    }
        
    void printArray(int cell[], MAX){
        int n;
        for (n=0; n<MAX; n++)
            if (cell[n]=1) printf ("*");
            if (cell[n]=0) printf (" ");
            else printf("!");
            printf("\n");
            return;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void generate(int cell[], MAX);
    void printArray(int cell[], MAX);
    Function arguments need types. You should be making something like:
    Code:
    void generate( int cell[], int size )
    {
    }
    ...
    generate( cell, MAX );
    Macros are just text replacement, so all your functions really just looked like:
    Code:
    void generate( int cell[], 80 )
    {
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    line 6 ... void generate(int cell[], int cells);
    line 32...
    Code:
    void generate(int cell[], int cells){
        int n=0;
        for (n=0; n< cells; n++)
        if(!((cell[n+1]) && (cell[n-1])) && ((cell[n+1]) || (cell[n-1])))
            cell[n]=1;
            return;
    }
    call as ... generate(cell,MAX); ...

    Or since preprocessor values are global just use...
    Code:
    void generate(int cell[]){
        int n=0;
        for (n=0; n < MAX; n++)
        if(!((cell[n+1]) && (cell[n-1])) && ((cell[n+1]) || (cell[n-1])))
            cell[n]=1;
            return;
    }

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    Thanks, the question prescribed the format, I don't know why

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nanobot View Post
    Thanks, the question prescribed the format, I don't know why
    Possibly to teach you this small lesson?

    (It's just as important to know what does not work as what does.)

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    12

    why constant change breaks down code

    The code produces conway's pattern as required. My problem is, when I define MAX to 80, it no longer works. Why?

    Code:
    #include<stdio.h>
    
    #define MAX 20
    
    
    void generate(int cell[]);
    void printArray(int cell[]);
    
    
    /*generate: current condition-->next generation
    printArray: print 1 as *, 0 as <spacei>*/
    
    
    int main(void)
    {
        int cell[MAX]={0}, n, gen, i;
    
    
        printf("Enter starting location and number of generations: ");
        scanf("%d %d", &n, &gen);
    
    
        cell[n]=1;
        printArray(cell);
        for(i=2;i<=gen; i++)
        {
            generate(cell);
            printArray(cell);
        }
        return 0;
    }
    void generate(int cell[]){
        int n=0;
        int copycell[MAX];
        for (n=0; n<MAX; n++) copycell[n]=cell[n];
        
        for (n=0; n<MAX; n++){
        if(!((copycell[n+1]) && (copycell[n-1])) && ((copycell[n+1]) || (copycell[n-1]))){
            cell[n]=1; }
            else cell[n]=0;
        }
            return;
    }
        
    void printArray(int cell[]){
        int n;
        for (n=0; n<MAX; n++)
            if (cell[n]==1) printf ("*");
            else if(cell[n]==0) printf (" ");
            else printf("!");
            printf("\n");
            return;
    }

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    bump

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Just so you know, saying "bump" is a really good way to turn people off helping you.

    > if(!((copycell[n+1]) && (copycell[n-1])) && ((copycell[n+1]) || (copycell[n-1])))
    When you're at the ends of the array, the +/-1 values are OUT OF BOUNDS.

    > My problem is, when I define MAX to 80, it no longer works. Why?
    It didn't work when it was 20 either.
    It might have been producing the expected result, but it wasn't bug-free.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compilation error in simple function
    By livin in forum C Programming
    Replies: 5
    Last Post: 04-17-2011, 09:07 PM
  2. compilation error on function overloading..
    By sanddune008 in forum C++ Programming
    Replies: 2
    Last Post: 07-27-2010, 10:09 AM
  3. error: was not declared in this scope compilation error
    By i_r_tomash in forum C Programming
    Replies: 2
    Last Post: 05-27-2010, 07:44 AM
  4. Replies: 3
    Last Post: 11-22-2007, 12:58 AM
  5. Help with Constants and enum constants. Newbie learning C++.
    By UnregJDiPerla in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2003, 08:29 PM

Tags for this Thread