I created a problem and solved it. Now i rewrote it and try to pass a variable named square by reference. Now im having trouble. What is the right way to pass a 2d array by reference?
Call by Value - Working
PHP Code:
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 9
void createprintdata( int [SIZE][SIZE] );
int hasduplicate( int [SIZE][SIZE], int );
void printmax( int [SIZE][SIZE] );
int square[SIZE][SIZE] = { 0 };
int main()
{
clrscr();
createprintdata( square );
getch();
return 0;
}
void createprintdata( int location[SIZE][SIZE] )
{
int genint = 0;
int col = 0, row = 0;
srand( time( 0 ) );
for( col = 0; col != SIZE; col++ ){
gotoxy( ( col + 2 ) * 5, 1 );
printf( "(%d)", col + 1 );
}
for( row = 0; row != SIZE; row++ ){
gotoxy( 2, ( row + 2 ) * 2 );
printf( "(%d)", row + 1 );
for( col = 0, genint = 0; col != SIZE; col++ ){
while( !genint || hasduplicate( location, genint ) ){
genint = ( rand() % 99 ) + 1;
}
location[row][col] = genint;
gotoxy( ( col + 2 ) * 5, ( row + 2 ) * 2 );
printf( "%d", genint );
}
}
gotoxy( 25, 22 );
printmax( location );
}
int hasduplicate( int location[SIZE][SIZE], int i )
{
int hasdup = 0;
int row, col;
for( col = 0; col != SIZE; col++ ){
for( row = 0; row != SIZE; row++ ){
if( location[row][col] == i ){
hasdup = 1;
break;
}
}
if( hasdup ) break;
}
return hasdup;
}
void printmax( int location[SIZE][SIZE] )
{
int row, col;
int max = location[0][0], min = max, curval = min;
for( row = 0; row != SIZE; row++ ){
for( col = 0; col != SIZE; col++ ){
curval = location[row][col];
max = ( curval > max ? curval : max );
min = ( curval < min ? curval : min );
}
}
printf( "\nThe highest value generated is %d", max );
printf( "\nThe lowest value generated is %d", min );
}
Call by Reference - Not Working!!
PHP Code:
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 9
void createprintdata( int ** );
int hasduplicate( int **, int );
void printmax( int ** );
int main()
{
int square[12][12] = { 0 };
clrscr();
createprintdata( square );
getch();
return 0;
}
void createprintdata( int **location )
{
int genint = 0;
int col = 0, row = 0;
srand( time( 0 ) );
for( col = 0; col != SIZE; col++ ){
gotoxy( ( col + 2 ) * 5, 1 );
printf( "(%d)", col + 1 );
}
for( row = 0; row != SIZE; row++ ){
gotoxy( 2, ( row + 2 ) * 2 );
printf( "(%d)", row + 1 );
for( col = 0, genint = 0; col != SIZE; col++ ){
while( !genint || hasduplicate( location, genint ) ){
genint = ( rand() % 99 ) + 1;
}
location[row][col] = genint;
gotoxy( ( col + 2 ) * 5, ( row + 2 ) * 2 );
printf( "%d", genint );
}
}
gotoxy( 25, 22 );
printmax( location );
}
int hasduplicate( int **location, int i )
{
int hasdup = 0;
int row, col;
for( col = 0; col != SIZE; col++ ){
for( row = 0; row != SIZE; row++ ){
if( location[row][col] == i ){
hasdup = 1;
break;
}
}
if( hasdup ) break;
}
return hasdup;
}
void printmax( int **location )
{
int row, col;
int max = location[0][0], min = max, curval = min;
for( row = 0; row != SIZE; row++ ){
for( col = 0; col != SIZE; col++ ){
curval = location[row][col];
max = ( curval > max ? curval : max );
min = ( curval < min ? curval : min );
}
}
printf( "\nThe highest value generated is %d", max );
printf( "\nThe lowest value generated is %d", min );
}