1. ## Segmentation fault

Can anyone check ths code and tell me whats wrong? Im getting a Segmentation fault [COREDUMP] when im runnig my program. I do not get any error when compiling.

Code:
```#include <stdio.h>
int size(void)
{
int n; //size of matrix
printf ("What is the size of the matrix?"); //user input
scanf ("%d",&n);
return n;
}
int recursion (int row, int column, int n) //Recursive function
{
int result;

result = ((row + 1) + column) % n;

// If the result is 0, n will be returned. n is the last digit in the
series
if (result == 0)
{
return n;
}
else
{
return result;
}
}
int main ()
{

int row; //row of the matrix
int column; // column of the matrix
int n; // n is the size of the matrix as mentioned in the question
int square[n][n];

n = size();

for (row = 0; row < n; row++)
{
printf("\n");

for (column = 0; column < n; column++)
{
square[row][column] = recursion(row, column, n);
printf("%d", square);   // Prints the Result
}
}
return 0;
}```

2. ## Also,

In the program, I have to build a Latin square for n elements.
Eg of latin square for n=3;

1 2 3
2 3 1
3 1 2

I also get a warning, no new line at end of file.
What does that mean?

3. Code:
```#include <stdio.h>
int size(void)
{
int n; //size of matrix
printf ("What is the size of the matrix?"); //user input
scanf ("%d",&n);
return n;
}
int recursion (int row, int column, int n) //Recursive function
{
int result;

result = ((row + 1) + column) % n;

// If the result is 0, n will be returned. n is the last digit in the
/*What a heck is series?*/
//   series
if (result == 0)
{
return n;
}
else
{
return result;
}
}
int main ()
{

int row; //row of the matrix
int column; // column of the matrix
int n; // n is the size of the matrix as mentioned in the question
int square[n][n];//this is invalid!

//n = size();this makes no sence
//you need something like
/*
* int *foo = calloc(n,sizeof(int*));
* foo[i] = calloc(n,sizeof(int));
*/
for (row = 0; row < n; row++)
{
printf("\n");

for (column = 0; column < n; column++)
{
square[row][column] = recursion(row, column, n);
//This is wrong you can't print the whole matrix at once
printf("%d",square[?][?]);   // Prints the Result
}
}
return 0;
}```

5. This is the change I made. I still get the Segmentation Fault error as well as no new line at end of file warning. Please check.

Code:
```#include <stdio.h>
{
int n; //size of matrix
printf ("What is the size of the matrix?"); //user input
scanf ("%d",&n);
return n;
}
int recursion (int row, int column, int n) //Recursive function
{
int result;

result = ((row + 1) + column) % n;

// If the result is 0, n will be returned. n is the last digit in the
series// Series means from 1 to 10, 10 is the last digit. So if a user inputs the size. the square generates from 1 to n, where n is the last digit.
if (result == 0)
{
return n;
}
else
{
return result;
}
}
int main ()
{

int row; //row of the matrix
int column; // column of the matrix
int n; // n is the size of the matrix as mentioned in the question
int square[n][n]; //What is wrong with this declaration, as
the  user will be giving the size of the array

for (row = 0; row < n; row++)
{
printf("\n");

for (column = 0; column < n; column++)
{
square[row][column] = recursion(row, column, n);
printf("%d", square[row][matrix]);   // Prints the Result
}
}
putchar('\n');
return 0;
}```

6. Im a beginner in C. So that seems like a recursive function to me. Aint it?

7. recursive functions are of this format:

Code:
```returntype function (some arguments)
{
/*some code*/
function (arguments);
}```
naturally your function isn't doing that: recursion - a function
calling itself.

no newline at end of file means to go to the very last line of code
(i.e. the last brace in the file) and press enter - save - recompile.

this code should not compile - for one I quote my compiler:
>>1>c:\documents and
settings\rich\desktop\boredom\boredom\boredom.cpp( 44) : error
C2065: 'matrix' : undeclared identifier

also,

>>int square[n][n]; //What is wrong with this declaration, as
//the user will be giving the size of the array

quzah pointed out that it is legal in C99 to some extent, but
you won't find many compilers that comply to that code yet - use
malloc, or make the array a constant size

8. > int n; // n is the size of the matrix
Setting aside the legality (or otherwise) of the way you declare your array, you need to read the size in BEFORE you create the array.
At the moment, you've probably got some huge (or negative) random value (because n isn't initialised) and you try and create some totally bogus array with that size.

Go with something like
Code:
`#define MAXSIZE 10`
And
Code:
```int square[MAXSIZE ][MAXSIZE ];
if ( n > MAXSIZE ) {
printf( "That's too big for me\n" );
}```

9. Hi Ron

The problem is with your array. square[n][n] is not accepted in C.

I have modified your main function which now uses dynamic allocation of two dimentional array.

Compile this code and it should work for you.

------------------------------------------------

Code:
```#include <stdio.h>

int size(void)
{
int n; /* size of matrix */
printf ("What is the size of the matrix: "); /* user input */
scanf ("%d",&n);
return n;
}

int recursion( int row,  int column,  int n) /* Recursive function */
{
int result;

result = ((row + 1) + column) % n;

/* If the result is 0, n will be returned. n is the last digit in the  series */
if (result == 0)
{
return n;
}
else
{
return result;
}
}

int main( )
{

int row; /* row of the matrix */
int column; /*  column of the matrix */
int n; /*  n is the size of the matrix as mentioned in the question  */
int **square;

n = size();

/* Memory allocation for the matrix.... */
square = (int **) malloc( n * sizeof(int));

for (row = 0; row < n; row++)
{
for (column = 0; column < n; column++)
{
square[row] = (int *) malloc( n * sizeof(int));
}
}

for (row = 0; row < n; row++)
{
printf("\n");

for (column = 0; column < n; column++)
{
square[row][column] = recursion(row, column, n);
printf("%d ", square[row][column]);   /* Prints the Result */
}
}
printf("\n\n");

free(square);

return 0;
}```
Regards
Ravi O. Rattihalli
ravirattihalli@yahoo.com

10. ## small change !!!

Hi Ron

Further to my previous message.....

There is no need to have two for loops while allocationg memory!!
I saw that I have used two loops just now...

so it should be like this:

Code:
```    /* Memory allocation .... */
square = (int **) malloc( n * sizeof(int));

for (row = 0; row < n; row++)
{
square[row] = (int *) malloc( n * sizeof(int));
}```
then the other for loop follows....

Cheers

Ravi O. Rattihalli

11. > square = (int **) malloc( n * sizeof(int));
Try it without the cast, and see what error message you get.

http://faq.cprogramming.com/cgi-bin/...&id=1043284351
and never cast malloc again.

12. ## thats fine but........

Yup.... I am aware of the warning :

Operation between types "int**" and "int" is not allowed..

But then how will you avoid that warning?? without casting it?

In such cases the documentation helps the developer whenever the declaration type is changed... so that he will change the cast accordingly... ( at the time of maintenance )

But the idea of using "sizeof *p" in malloc is very good.

If you know any best way of using malloc without cast and still avoid warnings, please let me know..

BTW, I had given the solution to Ron's problme so that he can atleast get rid of the segmentation fault and see the latin square :-)

Regards
Ravi

13. But then how will you avoid that warning?? without casting it?
By compiling your C programs as C instead of C++. As long as you're including the right header file (stdlib.h) then C won't complain.

14. ## got it right this time !!!

Hi Salem....

I guess you are right.. Need not cast the malloc if I use stdlib.h

and "itsme86" , thanX for the suggestion.. but saw ur mail after I corrected my code ;-) but I must thank you too.....

so Ron !!

here is your changed main again

Code:
```int main(void)
{

int row; /* row of the matrix */
int column; /*  column of the matrix */
int n; /*  n is the size of the matrix as mentioned in the   question  */

n = size();
int **square;

/* Memory allocation for the matrix.... */
square = malloc( n * sizeof(**square));
for (row = 0; row < n; row++)
{
square[row] = malloc( n * sizeof(**square));
}

for (row = 0; row < n; row++)
{
printf("\n");

for (column = 0; column < n; column++)
{
square[row][column] = recursion(row, column, n);
printf("%d ", square[row][column]);   /* Prints the Result */
}
}
printf("\n\n");

free(square);

return 0;
}```
Regards
Ravi

15. Nope, still wrong
Code:
```    square = malloc( n * sizeof(*square));  /* only one * here */
for (row = 0; row < n; row++)
{
square[row] = malloc( n * sizeof(*square[row]));  /* ** is OK, but not so intuitive */
}```
Always p = malloc ( n * sizeof *p );

> free(square);
You need to free each square[row] first