Thread: square matrix

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    17

    square matrix

    well,i'm working on this program that has been asked many times.
    "The magic squares". here what i got so far. I can compile it with no errors, but with no output. I think i'm not passing the arrays correctly.
    Any suggestions?
    Code:
    #include<stdio.h>
    
    #define MAX 15
    
    void intarray(int [][MAX],int);
    void calcsquare(int[][MAX],int);
    void printarray(int [][MAX],int);
    
    int main(void)
    {
    	int num;
    	int set_num[MAX][MAX];
    	printf("Enter the size of the magic square <15 or less>: ");
    	scanf("%d",&num);
    
    	intarray(set_num,num);
    	calcsquare(set_num,num);
    	printarray(set_num,num);
    }
    void intarray(int matrix[][MAX],int num)
    {
    	int row,column;
    	//initialize the arrays//
    	for(row=0;row<num;row++)
    		for(column=0;column<num;column++)
    			matrix[row][column] = 0;
    }	
    void calcsquare(int square[] [MAX],int size)
    {
    	int next;
    	int row =0;
    	int column =size/2;
    	square[row][column]=1;//place a 1 on center of 1st row//
    	
    	for(next=2;next<(size*size);next)
    	{
    		row--;
    		column++;
    		if(row<0 && column>size-1)
    		{
    			row=0;
    			column=size-1;
    		}
    		else if(row<0)
    		{
    			row=size-1;
    		}
    		else if(column>size-1)
    		{
    			column=0;
    		}
    		else if(square[row][column]!=0)
    		{
    			row =row+2;
    			column=column-1;
    		}
    		
    		square[row][column]=next;//continue with the next number//
    	}
    
    }
    	
    void printarray(int matrix[][MAX],int n)
    {
    	int row,column;
    	
    	for(row=0;row<n;row++)
    		for(column=0;column<n;column++)
    	
    			printf(" %5d", matrix[row][column]);
    			printf("\n");
     
    
     
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > for(next=2;next<(size*size);next)
    Should you be incrementing next?
    Code:
    	for(next=2;next<(size*size);next++)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  3. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM