A simple matrix in C

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 373 Views - Last Post: 03 October 2012 - 11:46 AM Rate Topic: -----

#1 tomnidi  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 36
  • Joined: 18-September 12

A simple matrix in C

Posted 03 October 2012 - 09:55 AM

Hello,

Today we've been given our first matrix tasks. One particular task was a bit hard for me, couldn't find a way to get through the problem:

The task is to create a program which generates the following matrix-

Posted Image


My last attempt:
void main(void) // TAR 1:3
{
	int mat[N][N], nIdx;
	for(nIdx=0;nIdx<(N*N);nIdx++)
	{
		if((COL>=LINE&&COL<N-LINE) || !(COL>=LINE||COL<N-LINE))
			mat[LINE][COL] = LINE<2?nIdx:nIdx+1-(LINE*2-1);
		else
			mat[LINE][COL]=0;

		printf("%3d",mat[LINE][COL]);
		(COL==N-1)?puts(""):0;
	}
}


This code doesn't work properly and have some errors in it even though it compiles properly. Would be glad if someone could point me somewhere, I've tried changing way too many things within my code but since I'm posting here, none of my changed were any good.

Is This A Good Question/Topic? 0
  • +

Replies To: A simple matrix in C

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: A simple matrix in C

Posted 03 October 2012 - 10:03 AM

Quote

This code doesn't work properly and have some errors in it even though it compiles properly.

Why doesn't your code work properly? What are the errors? What output is your program producing?

Please explain the following line:
        if((COL>=LINE&&COL<N-LINE) || !(COL>=LINE||COL<N-LINE))

What exactly are you trying to accomplish?

I also suggest that you start using if/else statements instead of:
LINE<2?nIdx:nIdx+1-(LINE*2-1)

At least until you have your program working properly. In my opinion you are obscuring your logic using this construct and the if/else would make seeing what is going wrong easier when using your debugger. You have tried to follow your logic by using your debugger, right?



Jim
Was This Post Helpful? 0
  • +
  • -

#3 tomnidi  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 36
  • Joined: 18-September 12

Re: A simple matrix in C

Posted 03 October 2012 - 10:16 AM

Indeed, I couldn't understand where it goes wrong though.
The number of zeros in each line is the line as an integer, that means in the second line (1, in the array), there would be 1 zero in each one of the sides within the line, in line three would be two zeros in each side and so on.

if((COL>=LINE&&COL<N-LINE) || !(COL>=LINE||COL<N-LINE))


This line is supposed to check whether or no I should put the next number in the field of the array or define it as a zero. It should somehow mark the border of the output.

The program somewhere goes wrong with the output at the second line, comes out with some numbers which shouldn't be there - theoretically.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: A simple matrix in C

Posted 03 October 2012 - 10:33 AM

I suggest you run your program through your debugger, set a breakpoint on the first line and single step through your program. Watch your variables as you single step. I think that if statement is one of the causes of your problems so carefully watch how that statement evaluates the variables. It will be much easier to watch your variables if you first get rid of the Ternary conditional operators.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 tomnidi  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 36
  • Joined: 18-September 12

Re: A simple matrix in C

Posted 03 October 2012 - 10:39 AM

I've already gone through this but as I've stated I couldn't follow the change in the var, something didn't look quite right there. I believe my entire point of view about the whole task is completely wrong though, I thought maybe I could get some advices over here on how to handle this.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3570
  • View blog
  • Posts: 11,095
  • Joined: 05-May 12

Re: A simple matrix in C

Posted 03 October 2012 - 10:44 AM

Also, I'm not seeing anything that declares LINE and COL or changes their values. Given the way the code is written, it will always be storing values into the same matrix element for all iterations of nIdx from 0..N2-1.
Was This Post Helpful? 0
  • +
  • -

#7 tomnidi  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 36
  • Joined: 18-September 12

Re: A simple matrix in C

Posted 03 October 2012 - 10:47 AM

This is my code after splitting it into normal if/else lines, hope it helps somehow:

void main(void) // TAR 1:3
{
	int mat[N][N], nIdx;
	int col,line, c;
	for(nIdx=0;nIdx<(N*N);nIdx++)
	{
		col = (nIdx%N);
		line = (nIdx/N);
		if((col>=line&&col<N-line))
		{
			if(line<1)
				mat[line][col] = nIdx+1;
			else
				mat[line][col] = nIdx+1-(line*2-1);
		}
		else
			mat[line][col]=0;

		printf("%3d",mat[line][col]);
		(col==N-1)?puts(""):0;
	}
}


Current output:

Quote

1 2 3 4 5 6 7
0 8 9 10 11 12 0
0 0 14 15 16 0 0
0 0 0 20 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
Press any key to continue . . .


As you can see, it skips on the 13 in the third line even though I can't see anything wrong within my algorithm.

This post has been edited by tomnidi: 03 October 2012 - 10:47 AM

Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5822
  • View blog
  • Posts: 12,675
  • Joined: 16-October 07

Re: A simple matrix in C

Posted 03 October 2012 - 10:54 AM

I wouldn't bother with any kind of array for this. I would use two loops. You're really working too hard without them.

A counter also makes life easier. The ONLY thing you need determine is if the position is filled with a zero or not.

e.g.
void printMat(int n) {
	int row, col, counter, m;
	
	m = n/2;
	counter = 1;
	for(row=0; row<n; row++) {
		int ofs = ?
		for(col=0; col<n; col++) {
			// some code and a single print
		}
		printf("\n");
	}
}


Was This Post Helpful? 2
  • +
  • -

#9 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3570
  • View blog
  • Posts: 11,095
  • Joined: 05-May 12

Re: A simple matrix in C

Posted 03 October 2012 - 10:54 AM

For row 3, column 3, you have:
nIdx == 16
line == 16 / 7 == 2
col == 16 % 7 == 2

mat[line][col] = nIdx+1-(line*2-1)
mat[2][2] == 16+1 - (2*2-1) == 17 - (4-1) == 14

Your formula for determining the value that is supposed to go into that slot is incorrect.
Was This Post Helpful? 0
  • +
  • -

#10 tomnidi  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 36
  • Joined: 18-September 12

Re: A simple matrix in C

Posted 03 October 2012 - 11:17 AM

View Postbaavgai, on 03 October 2012 - 10:54 AM, said:

I wouldn't bother with any kind of array for this. I would use two loops. You're really working too hard without them.

A counter also makes life easier. The ONLY thing you need determine is if the position is filled with a zero or not.

e.g.
void printMat(int n) {
	int row, col, counter, m;
	
	m = n/2;
	counter = 1;
	for(row=0; row<n; row++) {
		int ofs = ?
		for(col=0; col<n; col++) {
			// some code and a single print
		}
		printf("\n");
	}
}


I doubt it helps in any way practicing arrays and matrixes ;)
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3570
  • View blog
  • Posts: 11,095
  • Joined: 05-May 12

Re: A simple matrix in C

Posted 03 October 2012 - 11:21 AM

I agree with baavgai about using a counter. Also his line #4 should give you a big hint as well.

If the point is using 2 dimensional arrays, then you can still fill in the array, and then print later. That way you are using the matrix twice, and not have wasted the memory for something that could have been done without a matrix. If you want to use the array three times: do a first pass of filling it with zeros, then a second pass filling in the sequence, and then a third pass to print it out.

:)
Was This Post Helpful? 0
  • +
  • -

#12 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5822
  • View blog
  • Posts: 12,675
  • Joined: 16-October 07

Re: A simple matrix in C

Posted 03 October 2012 - 11:26 AM

View Posttomnidi, on 03 October 2012 - 02:17 PM, said:

I doubt it helps in any way practicing arrays and matrixes ;)


But that's not what your problem is, really. You problem is determining the position to set a value. If you could print it, you could load it.

If you prefer:
int mat[N][N], row, col, counter, m;

m = N/2;
counter = 1;
for(row=0; row<N; row++) {
	int ofs = ?; // hint, I used ABS
	for(col=0; col<n; col++) {
		int value = 0;
		// some code to change value as required
		mat[row][col] = value;
	}
}
// print mat here, use two loops as shown


Was This Post Helpful? 0
  • +
  • -

#13 tomnidi  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 36
  • Joined: 18-September 12

Re: A simple matrix in C

Posted 03 October 2012 - 11:30 AM

View PostSkydiver, on 03 October 2012 - 11:21 AM, said:

I agree with baavgai about using a counter. Also his line #4 should give you a big hint as well.

If the point is using 2 dimensional arrays, then you can still fill in the array, and then print later. That way you are using the matrix twice, and not have wasted the memory for something that could have been done without a matrix. If you want to use the array three times: do a first pass of filling it with zeros, then a second pass filling in the sequence, and then a third pass to print it out.

:)


In my course the main goal of my teacher is to teach us how to think properly. We're trying to solve the given tasks with the minimum variables, that's why I've given up on the counter in the first place. Logic is the key in our course, please excuse me if I deny your suggestions (even though you're right about the fact using a counter would be much easier and giving up on the array would be even a better thing to do, though, this is not what the teacher wants us to code this like).
Was This Post Helpful? 0
  • +
  • -

#14 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2983
  • Posts: 10,313
  • Joined: 08-August 08

Re: A simple matrix in C

Posted 03 October 2012 - 11:43 AM

Start by initializing your array to zeros:
int mat[N][N] = {0};


Now you only need to set the non-zero values. Using a counter you can iterate through the rows setting elements starting with the column number equal to the row number and ending row numbers before the last column. It doesn't get much easier.

This post has been edited by CTphpnwb: 03 October 2012 - 11:43 AM

Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg  Icon User is offline

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: A simple matrix in C

Posted 03 October 2012 - 11:44 AM

Quote

In my course the main goal of my teacher is to teach us how to think properly.

I actually disagree with this assessment, in my opinion your teacher is teaching you how to write difficult to maintain programs, will have little if any future value. They should be teaching how to write programs that are easy to understand and maintain.

Anyway, the location of your problem has already been pointed out by Skydiver in post #9. The solution is another matter.

Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2