Please help me i dont know why this doesnt work

This is a discussion on Please help me i dont know why this doesnt work within the C Programming forums, part of the General Programming Boards category; 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 ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 05: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21