Thread: Please help me i dont know why this doesnt work

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    7

    Please help me i dont know why this doesnt work

    Code:
    #include <stdio.h>
    #include<time.h>
    void array();
    
    int main()
    {
        printf("CAN SOME ONE TELL ME WHY THIS DOESNT WORK\n");
        printf("AND IF YOU COULD SHOW ME THE CORRECTION");
        array();
    }
    
    
    
    void array()
    {
    int check(int num, int array[4][4]) {
    int i, j;
    
        for (i = 0; i < 4; i++) {
            for (j = 0; j < 4; j++) {
                if (array[i][j] == num) {
                    return 1;
                }
            }
        }
        
        return 2;
    }
    
    
    int main() {
        int x,y,arr[4][4];
        int w;
        int ok=0;
        srand(time(NULL));
    
        for (x=0;x<4;x++)
        {
            for(y=0;y<4;y++)
            {
                if (x ==3 && y ==3) {
                
                    printf("| \t");
                }
                else {
                    ok =1;
                    while (ok!=2) {
                        w = rand()%15+1;
                        ok = check(w, arr);
                    }
                    arr[x][y] = w;
                    printf("|   %i\t",arr[x][y]);
                }
        
            }
            printf("|\n");
        }
    
    
    
    
    
    
    return 0;
        }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What are you trying to do with this code?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you have two main() functions.

    And you're trying to declare nested functions.
    Code:
    void array()
    {
    int check(int num, int array[4][4]) {
    A development process
    Read this, and learn to press "compile" every 5 to 10 lines of new code.
    Not bash the keyboard for hours to craft something with many dozens of lines, only to find out that it's swamped with errors you can't cope with.
    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.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    7
    soo should i define them seperatly
    if sooo could please show me how to
    iam kindda new to programming.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your program, like all C programs, should have one and only one main() function:
    Code:
    #includes up here
    
    //other functions? prototypes go here
    
    
    int main() {
    
       // your code in here
      
    
       return 0;
    } //end of main

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    7
    Okay when I changed

    int check(int num, int array[4][4]) {

    to :

    int check(int num)
    int check (int array[4][4])

    it didn't work

    cause when I use void array() alone it builds succesfully

    Please help me out here

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Go back and read your C book again.

    Because you seem to be randomly typing and compiling at the moment, just to see what happens.

    For example, you were supposed to fix the nested function, not mess about with the declarations
    Code:
    void array ( ) {
    } // this is the end of a function
    
    int check(int num, int array[4][4]) {
        int i, j;
    
        for (i = 0; i < 4; i++) {
            for (j = 0; j < 4; j++) {
                if (array[i][j] == num) {
                    return 1;
                }
            }
        }
        
        return 2;
    } // this is the end of a function
    
    int main ( ) {  // just ONE of these
        int arr[4][4], result;
        array();   // call it
        result = check(1,arr);  // call it
        return 0;
    }
    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. why doesnt this work?
    By davo666 in forum C Programming
    Replies: 2
    Last Post: 01-04-2009, 06:59 AM
  2. Why doesnt this work
    By Hugo716 in forum C++ Programming
    Replies: 13
    Last Post: 05-18-2006, 11:57 AM
  3. Why doesnt this work
    By digdug4life in forum C++ Programming
    Replies: 13
    Last Post: 06-19-2005, 03:22 PM
  4. Why doesnt this work
    By Jotun in forum C++ Programming
    Replies: 3
    Last Post: 04-18-2004, 04:55 PM
  5. why doesnt this work?
    By brad123 in forum C Programming
    Replies: 3
    Last Post: 04-23-2002, 05:26 PM

Tags for this Thread