Please need help with C program!


 
Thread Tools Search this Thread
Top Forums Programming Please need help with C program!
# 1  
Old 03-29-2002
Data Please need help with C program!

My program only ouputs the correct magic square sum totals for the number 15.If I enter any odd number smaller than 15 my sum totals are incorrect. I have define "size" to 15. How or what do I change so that my program will output the magic square results for the odd numbers 1 through 15 without having to change the value in my "#define size 15"?Or do I need to get rid of this define statement? How do I define my array size to be the max 15 by 15. Then, input the size of the array that I want to build?

Thanks,

J.J.

Code:
#include<stdio.h>
#define size 15  

void input_square (int order, int magic[size][size])
{

     int row,column;
     int entry = 1;
	 

     printf("Enter the size of the odd Magic Square : " );
     scanf("%d",&order);
     printf ("\n");


  if ( order % 2 )  {
   
     row = 0;
     column = (order - 1) / 2;

     while (entry <= order * order) {

          magic[row][column] = entry;

          if (entry == order * (entry / order))   {
		   row++;
          }

          else     {
              row--;
              column++;

              if (row < 0)	 {	 
		   row = order - 1;
              }

              if (column > order - 1)         {
	           column = 0;
              }

          }

          entry++;

     }

     printf("Magic square appears as follows:\n");
     printf ("\n");

     for(row = 0; row < order; row++)
     {

     for (column = 0; column < order; column++)
	   printf("%4d",magic[row][column]);

     printf("\n");

     }
  }

  else  {

       printf ("\nThe number: %d is not Odd\n", order);
          printf ("\n");

  }

}

void print_sum (int order,int magic [size][size])
{

     int row, column;	 
     int magic_number;
     int count = 1;
     int count_2 = 1;

     magic_number = 0;

     for (row = 0; row < order; row++)
          magic_number += magic[row][0];	    

     for (column = 0; column < order; column++) {
			 	
          printf("\nThe Sum for Column %d is %d.\n", count, magic_number);

	  count++;
     }   			 
         
     magic_number = 0;
	 

     for (column = 0; column < order; column++) 
		 magic_number += magic[0][column];	    

     for (row = 0; row < order; row++)		 {
			 	
            printf("\nThe Sum for Row %d is %d.\n", count_2, magic_number);

	    count_2++;

     }

     magic_number = 0;

     row = 0;
     column = 0;

     while (row < order && column < order)     {

        magic_number += magic[row][column];
	row++;
	column++;

     }
              	    			 	
     printf("\nThe Sum for Diagonal from top left to bottom right is %d.\n",magic_number);

     magic_number = 0;

     row = order - 1;
     column = 0;

     while (row <= order && column < order)    {

        magic_number += magic[row][column];
	row--;
	column++;
      }
	    
			 	
     printf("\nThe Sum for Diagonal from bottom left to top right is %d.\n",magic_number);
     printf("\n");

     magic_number = 0;

}



main()
{
     
         int order = size;
	 int magic[size][size];
   
	 input_square (order, magic);
	 print_sum (order, magic); 
 
}


Last edited by PxT; 03-29-2002 at 04:36 PM..
# 2  
Old 03-29-2002
Note: above post edited to add "code" tags and for readability.

This sounds like a homework problem. Please note the forum rules about posting homework. As a hint: please review the information on "pass by value" and "pass by reference" and how functions handle passed arguments. Using gdb may help you see what is going on at various points in your program.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl program get a response before the program quits

I created a program, so a kid can practice there math on it. It dispenses varies math problems and the kid must input an answer. I also want it to grade the work they have done, but I can't find the best place for it to print out the grade. I have: if ( $response =~ m/^/ ) { $user_wants_to_quit... (1 Reply)
Discussion started by: germany1517
1 Replies

2. Homework & Coursework Questions

Calling compiled C program with Perl program

Long story short: I'm working inside of a Unix SSH under a bash shell. I have to code a C program that generates a random number. Then I have to call the compiled C program with a Perl program to run the C program 20 times and put all the generated random #s into a text file, then print that text... (1 Reply)
Discussion started by: jdkirby
1 Replies

3. Programming

Calling c program from another c program

Hi All, Probably this is a repeated question. My knowledge in this is limited and i got confused on all those materials i got in google search. We use #include <> to include a predefined library like stdio.h i saw somewhere that #include "" includes a man made module(another C program). IS... (2 Replies)
Discussion started by: jisha
2 Replies

4. Programming

Python program faster than C++ program.

I wrote a simple program that generates a random word 10,000,000 times. I wrote it in python, then in C++ and compared the two completion times. The python script was faster! Is that normal? Why would the python script be faster? I was under the impression that C++ was faster. What are some of... (2 Replies)
Discussion started by: cbreiny
2 Replies

5. Programming

Program

Hi guys I so upset today i have this question about socket and i made the program do you know what wrong with it this the question : Write one TCP socket program (named webclient.cpp) to simulate a web browser by using HTTP/1.1. The client should take three... (1 Reply)
Discussion started by: Net Star
1 Replies

6. UNIX for Dummies Questions & Answers

Script to open program and send/execute command in program

Hi, i want to write a script that executes a program (exec?) . this program then requires a filename as input. how do i give it this input in the script so the program will be complete run and close by the script. e.g. exec prog.exe program then asks for filename "enter filename:"... (1 Reply)
Discussion started by: tuathan
1 Replies

7. Programming

A program to trace execution of another program

Hi, I wanted to know if i can write a program using switches and signals, etc to trace execution of other unix program which calls c program internally. If yes how? If not with signals and switches then are there any other methods apart from debugging with gdb/dbx. (3 Replies)
Discussion started by: jiten_hegde
3 Replies

8. UNIX for Dummies Questions & Answers

How to write to stdin of another program (program A -> [stdin]program B)

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question: ---------... (3 Replies)
Discussion started by: vvaidyan
3 Replies

9. Programming

How to write to stdin of another program (program A -> [stdin]program B)

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question: ---------... (1 Reply)
Discussion started by: vvaidyan
1 Replies

10. Programming

executing a program within a program

Read the title: how do i do it? (4 Replies)
Discussion started by: Gekko
4 Replies
Login or Register to Ask a Question