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

lcriley

macrumors newbie
Original poster
Jan 13, 2010
2
0
I am rather new to programming, and I am trying to write a console program that will create 12 tone matrices.
The code compiles, but when run I get a "bus error".
The debugger says EXC_BAD_ACCESS.


Code:
int main ()
{
	int j,k,l;
	int twelve[13][13];
	void mat(int twelve[13][13]);
	

	printf("input original tone row \n");
	for(j=0;j<=11;j++)
	{
		scanf("%2i",&twelve[j][0]);
		
	}
	
	
	mat(twelve);
	
	for(k=0;k<=11;k++)
	{ for(l=0;l<=11;l++)
	{
		printf("%i ",twelve[l][k]);
	}
		printf("\n");
	}
	return 0;
}
		
void mat(twelve)
int twelve[13][13];

{
	int j,k,l;
	int temp;
	
	/*inversion*/
	for(j=1;j<=11;j++)
	{
		twelve[0][j] = 12 - twelve[j][0];
	}
	/*fill in columns*/
	/*this sections seems to be what's crashing it */
	for(k=1;k<=11;k++)
	{for(l=1;1<=11;l++)
	{
		temp = twelve[0][k] + twelve[l][0];
		if(temp >= 12)
		{
			twelve[k][l] = temp - 12;
		}
		else { twelve[k][l] = temp;}
	}}
}
 

chown33

Moderator
Staff member
Aug 9, 2009
10,767
8,468
A sea of green
Code:
void mat(twelve)
int twelve[13][13];
{
	int j,k,l;
	{for(l=1;1<=11;l++)
}

Look very carefully at this 'for' statement. You may even need to change the font.

The condition is "one less than eleven". Which is always true. Forever.

Never name a variable "l" (ell). Never. Ever.

EDIT: Anddon'tbeafraidtoaddspaces.Itmakesiteasiertospoterrors.
Code:
for ( l=1;  1 <= 11;  l++ )
 

ChrisA

macrumors G5
Jan 5, 2006
12,601
1,737
Redondo Beach, California
Never name a variable "l" (ell). Never. Ever.


I heard a story about a FORTRAN programmer at a place I worked at years ago who named variables with combinations of O and 0. So he had things like this

Code:
O00 = OO0+OOO

They fired him. Writing l<=1 is almost as bad although I'm sure it was unintended.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Code:
#include <stdio.h>

void mat(int twelve[13][13]);

int main (int argc, char *argv[]) {
	int dim1,dim2;
	int twelve[13][13];
	
	printf("input original tone row \n");
	for(dim1=0;dim1<=11;dim1++) {
		scanf("%2i",&twelve[dim1][0]);
	}
	
	mat(twelve);
	
	for(dim2=0;dim2<=11;dim2++) {
		for(dim1=0;dim1<=11;dim1++) {
			printf("%i ",twelve[dim1][dim2]);
		}
		printf("\n");
	}
	return 0;
}
		
void mat(int twelve[13][13]) {
	int temp,dim1,dim2;
	
	/*inversion*/
	for(dim1=1;dim1<=11;dim1++) {
		twelve[0][dim1] = 12 - twelve[dim1][0];
	}
	/*fill in columns*/
	/*this sections seems to be what's crashing it */
	for(dim1=1;dim1<=11;dim1++) {
		for(dim2=1;dim2<=11;dim2++) {
			temp = twelve[0][dim1] + twelve[dim2][0];
			if(temp >= 12) {
				twelve[dim1][dim2] = temp - 12;
			} else {
				twelve[dim1][dim2] = temp;
			}
		}
	}
}

This should be easier to read, and work. People have already told you and showed you the error... but what did you try to debug this? Did you add any print statements to see what was going on? Did you use the debugger? For more complex problems, just a quick look-see won't do it. You should start using tools to debug your code now.

-Lee
 

lcriley

macrumors newbie
Original poster
Jan 13, 2010
2
0
Thanks for your help. I can't believe I didn't spot such a simple mistake.
Is there a suggested font to use for programming?

I did use the debugger in xcode, but I'm not quite sure I understood everything it was saying.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Thanks for your help. I can't believe I didn't spot such a simple mistake.
Is there a suggested font to use for programming?

I did use the debugger in xcode, but I'm not quite sure I understood everything it was saying.

The default Terminal font in OS X is Menlo. It suits me fine, day-to-day.

The attached image is a screenshot of a vim session with some l's and 1's. They are pretty easy to tell apart in this font. XCode uses Monaco by default, and it's pretty easy to tell there, too. If you're using an earlier version it may be different, though.

-Lee
 

Attachments

  • terminal.jpg
    terminal.jpg
    24.6 KB · Views: 196

Detrius

macrumors 68000
Sep 10, 2008
1,623
19
Apex, NC
Thanks for your help. I can't believe I didn't spot such a simple mistake.
Is there a suggested font to use for programming?

I did use the debugger in xcode, but I'm not quite sure I understood everything it was saying.

First, if you need to change the font to prevent coding mistakes, you're doing it wrong. Use things like i,j,k, or x,y,z but not j,k,l.

Next, you'll easily spend as much time debugging as you do writing code. You don't actually have to use a debugger to debug code, but it does make it easier, since you don't have to write a new printf statement and recompile just to see the value of a different variable. If you can't debug your own code, let alone someone else's, you won't be a useful programmer. That being said, sometimes you do just need an extra set of eyes with a different perspective.
 

jared_kipe

macrumors 68030
Dec 8, 2003
2,967
1
Seattle
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.