Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

c programming magic square

Options
  • 07-11-2006 8:44pm
    #1
    Registered Users Posts: 232 ✭✭


    I am trying to write a simple programme for a 3x3 magic square. user inputs 9 numbers and if it satisfys a magic square it comes up with congrat r what ever. if not it says error. i am having trouble with the loop adding the diagnals
    If someone could help i would appreacate it.
    Kevin


Comments

  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    What exactly is the problem with the loops? Post your code so we'll see what's going wrong.


  • Registered Users Posts: 232 ✭✭kryan1


    first time at this and only at it 2months so i no that there is mistakes and advance apology to the pros

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int table [3][3];
    int a [3];
    int b [3];
    int c [3];
    int I, J, d, e,f,g,h,k;
    clrscr();
    puts ("\n\n\tPlease Enter a Number\n");
    k=1;
    for (I=0;I<3;I++)
    {
    a=0;
    b=0;
    c=0;
    for (J=0;J<3;J++)
    {
    printf ("%d No is = ",k);
    scanf ("%d",&table[J]);
    a=a+table[J];
    k=k+1;
    }
    }
    for (I=0;I<3;I++)
    {
    for (J=0;J<3;J++);
    b=b+table[J];
    }
    for (I=0;I<2;I++)
    {
    for (J=0;J<3;J++);
    c=c+table[J];
    {
    printf ("\n\tUser Number Input");
    for (I=0;I<3;I++)
    {
    clrscr();
    printf ("\n");
    for (J=0;J<3;J++)
    printf ("\t%d",table[J]);
    }
    for (I=0;I<3;I++)
    {
    printf ("\n Sum Horz %d line is = %d", I+1, a);
    printf ("\n Sum Verf %d line is = %d", I+1, b);
    printf ("\n Sum diag %d line is = %d", I+1, c);
    }
    }
    getch();
    }
    }


  • Registered Users Posts: 205 ✭✭Stugots


    A few suggestions:
    Break the task into two parts, loading the data into the table and then processing the data.

    Use meaningful and consistent variable names - makes it easier to debug, e.g. horizontal_loop, vertical_loop.

    Your code assumes three diagonals - there are only two.

    You attempt to nest loops several times using this line:
    for (J=0;J<3;J++);
    It effectively does nothing - the ';' at the end means that this is the end of the loop. Its the same as doing this:
    for (J=0;J<3;J++)
    {
    // Do nothing.
    }

    It is a good idea to encapsulate and indent every code block (for, if, else, etc) with braces, even if it is not syntactically required (i.e. if there is only a single statement in the block). It makes it easier to visually identify nesting errors later and means that you are less likely to forget to add the braces if you add lines to the block of code later. If you do this to your code, some more errors in loop nesting should become apparent to you.

    Keep posting your progress.


  • Registered Users Posts: 232 ✭✭kryan1


    Ok, got the program working as far as addint the vertical and horizontal and diagonal lines. I am tring to use an "if" statement so that if they all match its a magic square. "else" it prints a message sorry its not a magic square.
    i can't seem to get it to work tho. any advice pls?
    progam as below.
    for (J=0;J<3;J++)
    {
    printf ("%d No is = ",k);
    scanf ("%d",&table[J]);
    a=a+table[J];
    k=k+1;
    }
    }
    for (J=0;J<3;J++)
    {
    b[J]=0;
    for (I=0;I<3;I++)
    b[J] +=table[J];
    }
    for (J=0;J<3;J++)
    {
    c=0;
    d=0;
    for (I=0;I<3;I++)
    {
    c += table;
    d += table[2-I];
    }
    }
    if (a[0]==a[1]==a[2]==b[0]==b[1]==b[2]==c==d)
    {
    {
    printf ("\n\tUser Number Input");
    for (I=0;I<3;I++)
    {
    printf ("\n");
    for (J=0;J<3;J++)
    printf ("\t%d",table[J]);
    }
    for (I=0;I<3;I++)
    printf ("\nLine %d horz %d",I+1,a);
    for (I=0;I<3;I++)
    printf ("\nLine %d vert %d",I+1,b);
    printf ("\nDiagnial \t%d",c);
    printf ("\n2nd Dia \t%d",d);
    }
    getch();
    }
    }
    else
    {
    printf ("Magic Square Not satisified");
    Printf ("Please Try Again");
    getch();
    }
    }


  • Registered Users Posts: 205 ✭✭Stugots


    It doesn't appear to me that your program should compile. Your block nesting is incorrect in several places and you're also mixing upper and lower case 'i'/'I' as an index to arrays.

    Make sure that every functional block is explicitly encapsulated with braces, for example, every 'for' line should be followed by an open brace '{', and indented block of code and a closing brace '}'.

    Regarding 'i'/'I', C is case sensitive, so you should use one or the other. Typically lower case i, j, k are used for array indices, loop counters etc.


  • Advertisement
  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Split the app up into four functions.

    First function: Reads in the data from the keyboard.

    Second function: Calculates that all columns are correct.

    Third Function: Calculates that all rows are correct.

    Fourth Function: Checks the two diagonals to make sure they are correct.

    Give that a shot and post your code up here again, but put it in [ CODE ] [ /CODE ] blocks to make it show up correctly.


  • Registered Users Posts: 232 ✭✭kryan1


    Got it working. I replaced the
    If (a[0]==a[2]==..........)
    with
    	if (a[0]==a[1])
    	{
    		if(a[1]==a[2])
    		{
    			if(a[2]==b[0])
    			{
    				if(b[0]==b[1])
    				{
    					if(b[1]==b[2])
    					{
    						if(b[2]==c)
    						{
    							if(c==d)
    
    	{
            	clrscr();
    	puts ("\n\n\t***********************************************************\n");
    	puts ("\tCongratulations. You Have Just Formed A Magic Square.\n");
    	printf ("\tYour Magic Number is %d\n ",c);
    	puts ("\tThe Magic Square You Entered is as Follows\n");
    	for (I=0;I<3;I++)
    	{
    	printf ("\n\t\tLine %d = ",I+1);
    	for (J=0;J<3;J++)
    	printf ("\t%d",table[I][J]);
    	}
    	puts ("\n\n\t************************************************************");
    	getch();
    
    
    	}
    		}
    			}
    			   }
    				}
    					}
    						}
    else
    
    {
    	printf ("\t\tMagic Square Not satisified\n");
    	printf ("\t\tPlease Try Again");
    	getch();
    
    
    }
    
    }
    }
    
    this works fine.
    thanks for all the help.
    Kevin


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    I don't know but instead of all them ifs could you have one like:
    if (a[0]==a[1] && a[1]==a[2] && a[2]==b[0] && b[0]==b[1] && b[1]==b[2] && [2]==c && c==d)
    
    {
          //stuff
    } else 
    {
            printf ("\t\tMagic Square Not satisified\n");
    	printf ("\t\tPlease Try Again");
    	getch();
    }
    


Advertisement