plz some one explain in details this code :
we have array of 9 cells and 0 present empty locations we want solution of this array (no number will be repeated)
also if there is any way to call this functions in main so as the program runs succesfully plz tell me
one more thing plz is it illegal that function return true or false (what i knew is no thing can be performed after return statement )
Here is the code :

#include <iostream>
using namespace std;
#define empty 0
#define size 9
int r;
bool isEmptyLocation(int grid[size] , int &r);
bool CheckRow(int grid[size], int r,  int n);
bool Solution(int grid[size])
{   
    if (isEmptyLocation(grid, r)==false)
        return true;
    for (int n = 1; n <= 9; n++)
    {
        if (CheckRow(grid, r, n)==false)
        {
            grid[r] = n;
            if (Solution(grid))
                return true;

        }
    }
        return false;
}
bool isEmptyLocation(int grid[size], int &r)
{
    for (r = 0; r < size; r++)
        if (grid[r] == empty)
        return true ;
        return false ;
}


bool CheckRow(int grid[size], int r, int n)
{
    for (int r = 0; r < size; r++)
        if (grid[r] == n)
            return true;
            return false;
}

void print(int grid[size])
{
    for (int r = 0; r < size; r++)
    {
        cout<<grid[r]<<"  ";
        cout<<endl;
    }
}

int main()
{
    int grid[size] = {0, 0, 0, 5, 0, 0, 0, 0, 0};

    if (Solution(grid) == true)
          print(grid);
    else
        cout<<"No solution exists"<<endl;
        system("pause");
        return 0;
}

one more thing plz is it illegal that function return true or false (what i knew is no thing can be performed after return statement )

You can return from anywhere. You're right in that when a function returns, nothing else from the function happens, but in your code that you have return statements are inside if statements. Those will only return if the condition is true; otherwise, it goes on to the rest of the function.

if there is any way to call this functions in main so as the program runs succesfully plz tell me

What do you mean by "runs successfully" here? Does it not run at all for you? Or are you just looking for a grid that meets the Solution criteria?

no the program run.....i just asked if there is any way to call each function in main so as the program run succefuly as well
also why when i put else after true in EmptyLocation function the program didn't run ???

any way to call each function in main so as the program run succefuly as well

main calls Solution, which calls both isEmptyLocation and CheckRow, so those are getting called; I'm not sure what you mean.

By "run successfully" do you mean that Solution(grid) returns true?

why when i put else after true in EmptyLocation function the program didn't run

What does "didn't run" mean here? Post the altered code, and we might be able to help.

ok leave the question i asked what is confusing me this function works when

bool isEmptyLocation(int arr[size], int &r)
{
    for (r = 0; r < size; r++)
        if (arr[r]==empty)
        return true ;
        return false ;
}

and doesn't work when i put else like this :

bool isEmptyLocation(int arr[size], int &r)
{
    for (r = 0; r < size; r++)
        if (arr[r]==empty)
        return true ;
        else return false ;
}

i want just to know the reason

It looks to me you're getting confused by using single statement blocks without brackets.

The first code basically translates like this:

bool isEmptyLocation(int arr[size], int &r)
{
    for (r = 0; r < size; r++)
    {
        if (arr[r]==empty)
        {
            return true ;
        }
    }
        return false ;
}

The second like this:

bool isEmptyLocation(int arr[size], int &r)
{
    for (r = 0; r < size; r++)
    {
        if (arr[r]==empty)
        {
            return true ;
        }
        else
        {
            return false ;
        }
    }
}

You can see more easily from this that in the first case the function will return true on the first empty location it finds and false if no locations are empty. In the second case the function will return the condition of the first location only, true if empty or false if not, and not finish the loop.

Yep, use brackets; they are your friends.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.