Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

iBookG4user

macrumors 604
Original poster
Jun 27, 2006
6,595
2
Seattle, WA
I'm back again, this class just goes a bit too fast, but I think I'm getting the hang of it slowly. Anyways, I'm trying to create a 5x5 magic square, I believe that my code is really close, and it compiles without error. But, in the terminal window it says "Program received signal: “EXC_BAD_ACCESS”." and I can't figure out what's wrong, I would greatly appreciate some help :) (I would go to the tutor, but the only times he is available are when I am in other classes :()

Code:
#include <iostream>

using namespace std;

void print(int array[][5]);

void zerofill(int array[][5]);

void magic(int array[][5]);

void colcheck(int array[][5],int row,int col);

void rowcheck(int arrray[][5],int row,int col);

void filledcheck(int array[][5],int row,int col);

int main()
{//main
	
	int array[5][5];
	
	zerofill(array);
	
	magic(array);
	
	print(array);
	
	return 0;
}//main

void zerofill(int array[][5])
{//int zerofill
	
	for(int i=0;i<5;i++)
	{//for
		
		for(int x=0;x<5;x++)
		{//for#2
		
			array[i][x]=0;
	
		}//for#2
	
	}//for
	
}//int

void print(int array[][5])
{//void print
	
	for(int i=0;i<5;i++)
	{//for
		
		for(int x=0;x<5;x++)
		{//for#2
			
			cout << array[i][x] << "\t";

		}//for#2
		
		cout << endl << endl;
		
	}//for
	
}//void

void magic(int array[][5])
{//void magic
	
	int row = 0;
	
	int col = 2;
	
	int i = 1;
	
	array[row][col] = i;
	
	for (i=2;i<26;i++)
	{//for
		
		row--;
		
		col++;
		
		colcheck(array,row,col);
		
		rowcheck(array,row,col);
		
		filledcheck(array,row,col);
		
		array[row][col] = i;
		
	}//for
	
}//void magic

void colcheck(int array[][5],int row,int col)
{//void colcheck
	
	if (col > 4)
	{//if
	
		col = 0;

	}//if

}//void

void rowcheck(int arrray[][5],int row,int col)
{//void rowcheck
	
	if (row < 0)
	{//if
	
		row = 4;
	
	}//if

}//void

void filledcheck(int array[][5],int row, int col)
{//void filledcheck
	
	if (array[row][col] > 0)
	{//if
		
		row++;
		
		if (row > 4)
		{//if#2
		
			row = 0;
	
		}//if#2
	
	}//if

}//void
 

lloyddean

macrumors 65816
May 10, 2009
1,047
19
Des Moines, WA
Haven't bothered look over the code. Seems to run to completion without error for me.

Running…
0 0 1 0 0

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0


Debugger stopped.
Program exited with status value:0.
 

iBookG4user

macrumors 604
Original poster
Jun 27, 2006
6,595
2
Seattle, WA
Haven't bothered look over the code. Seems to run to completion without error for me.

Running…
0 0 1 0 0

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0


Debugger stopped.
Program exited with status value:0.

Alright, so that gave me the hint I needed. It had to do with the check functions! I took the check functions and put the code into the magic function and what do you know, it worked!

19 25 1 7 13

24 5 6 12 18

4 10 11 17 23

9 15 16 22 3

14 20 21 2 8

:D
 

Catfish_Man

macrumors 68030
Sep 13, 2001
2,579
2
Portland, OR
Glancing over the code for your check functions, I believe your issue is that you're not understanding the way arguments are passed to functions. Only the *value* of an int argument is passed, so if you change the argument, it is not changed in the calling function. For example:

Code:
void addSeven(int number) {
    number = number + 7;
}

int main() {
    int number = 5;
    int number2 = 5;
    addSeven(number);
    number2 = number2 + 7;
    printf("%d\n", number);
    printf("%d\n", number2);
    return 0;
}

Does that print:
12
12
or does it print this instead? :
5
12
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.