Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8773297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:15:33+00:00 2026-06-13T18:15:33+00:00

I am trying to write a C program to print a magic square. However,

  • 0

I am trying to write a C program to print a magic square. However, I am encountering an error when building the square. Could anyone help to figure out what is causing these bugs? The following is my relevant code, and my output:

Code:

  int main(void) {
      int size = 0;
      int r, c;
      int i = 1;

      printf("What is the size of the square: ");
      scanf("%d", &size);

      int square[size][size];
      int range = size * size;
      r = 0;
      c = size/2;

      do {
          square[r][c] = i;

          i++;
          r--;
          c++;

          if ((r == -1) && (c == size)) {
              r = r + 2;
              c--;
          } else if (r == -1) {
              r = size - 1;
          } else if (c == size) {
              c = 0;
          } else if (square[r][c] != 0) {
              r = r + 2;
              c--;
          }
      } while (i < range);

      for (r = 0; r < size; r++) {
          for (c = 0; c < size; c++) {
              printf("%d \t", square[r][c]);
          }
          printf("\n");
      }

      return 0;
  }

Output:

What is the size of the square: 3
-4196312        1       0
3       -4196352        -13339222
4       -13360148       2
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:15:34+00:00Added an answer on June 13, 2026 at 6:15 pm

    All variables in a function scope are not initialized to zero. Instead they are assigned to a random value.

    Use this:

    int square[size][size] = {0};
    

    You can also use memset or calloc, but this one is the simplest.


    Correction:

    The list initializer does not work with variable size array. Use memset instead:

    int square[size][size];
    memset(square, 0, size * size * sizeof(int));
    

    Edit:

    Entire working code

    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
        int size = 0;
        int r, c;
        int i = 1;
    
        printf("What is the size of the square: ");
        scanf("%d", &size);
    
        int square[size][size];
        memset(square, 0, size * size * sizeof(int));
        int range = size * size;
        r = 0;
        c = size/2;
    
        do {
            square[r][c] = i;
    
            i++;
            r--;
            c++;
    
            if ((r == -1) && (c == size)) {
                r = r + 2;
                c--;
            } else if (r == -1) {
                r = size - 1;
            } else if (c == size) {
                c = 0;
            } else if (square[r][c] != 0) {
                r = r + 2;
                c--;
            }
        } while (i < range);
    
        for (r = 0; r < size; r++) {
            for (c = 0; c < size; c++) {
                printf("%d \t", square[r][c]);
            }
            printf("\n");
        }
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a very simple program, I want to print out the
I am trying to write a Prolog program that will print out the male
Greetings, I'm trying to write a program in Python which would print a string
I am trying to write a program that allows me to print a ssrs
I'm trying to write a small program that prints out distinct numbers in an
I am trying to write a kiosk program for my print center at a
I am trying to write a simple program that prints out a C string
I am trying to write a program which gets input files and prints included
I'm trying to write a program that would convert celcius to fahrenheit and visa-versa.
I am trying to write a program that will prompt you to enter someone's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.