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 936637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:20:50+00:00 2026-05-15T21:20:50+00:00

I am not a C programmer. I have just started reading K&R’s TCPL last

  • 0

I am not a C programmer. I have just started reading K&R’s TCPL last week.
I have written this 42 line code in Java. I tried converting it to C, but it is giving me a segmentation fault.

Here is the Java version: http://codepaste.net/m8jz6m

My failed attempt to port it to C:

//Not working.
#include <stdlib.h>
#include <string.h>
 
void caesar ( const int SIDE )
{    
    int array [SIDE] [SIDE] ;

    for ( int number = 1; number <= SIDE * SIDE; ++ number )
        array [ getY ( number, SIDE ) ] [ getX ( number, SIDE ) ] = number ;

    for ( int Y = 0; Y < SIDE; ++ Y ) {
        for ( int X = 0; X < SIDE; ++ X)
            printf ( sprintf ("%%%dd" , strlen(itoa(SIDE*SIDE))), array [Y] [X] );
        printf ("\n");
    }
}
 
int getX ( const int number, const int SIDE )
{
    return SIDE == 1 ? 0 : number > 4 * SIDE - 4 ? 1 + getX ( number - 4 * SIDE + 4, SIDE - 2 ) : number >= 3 * SIDE - 2 ? 0 : number >= 2 * SIDE - 1 ? 3 * SIDE - 2 - number : number > SIDE ? SIDE - 1 : number - 1 ;
}
 
int getY ( const int number, const int SIDE )
{
    return SIDE == 1 ? 0 : number > 4 * SIDE - 4 ? 1 + getY ( number - 4 * SIDE + 4, SIDE - 2 ) : number >= 3 * SIDE - 2 ? 4 * SIDE - 3 - number : number  >= 2 * SIDE - 1 ? SIDE - 1 : number > SIDE ? number - SIDE : 0 ;
}
 
void main ( int argc, char *argv )
{
    if ( argc == 0 )
        printf ("\tUsage: java Caesar [side] | side:> Length and breadth of the square.\n");
    else
        caesar ( atoi( argv[1] ) );
}
 
 void itoa(int n, char s[])
 {
     int i, sign;

     if ((sign = n) < 0)  /* record sign */
         n = -n;          /* make n positive */
     i = 0;
     do {       /* generate digits in reverse order */
         s[i++] = n % 10 + '0';   /* get next digit */
     } while ((n /= 10) > 0);     /* delete it */
     if (sign < 0)
         s[i++] = '-';
     s[i] = '\0';
     reverse(s);
 }

  /* reverse:  reverse string s in place */
 void reverse(char s[])
 {
     int i, j;
     char c;

     for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
         c = s[i];
         s[i] = s[j];
         s[j] = c;
     }
 }

Please let me know where I am going wrong.

  • 1 1 Answer
  • 1 View
  • 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-05-15T21:20:51+00:00Added an answer on May 15, 2026 at 9:20 pm
    • You are calling your itoa() function with one argument, while the function actually expects two.

    • Also, it seems that this line:

      array [ getY ( number, SIDE ) ] [ getX ( number, SIDE ) ] = number ;
      

      could be better expressed with something like:

      array[number / SIDE][number % SIDE] = number;
      

      without using those getX and getY functions (that are curiously recursive). I haven’t examined those functions in detail to see what they actually do, but you may consider the above modification in any case.

    • sprintf() is called with incorrect arguments. The first parameter should be the destination buffer. You seem to be using it as if it returned a string (it doesn’t).

    • Look up the function of * inside a printf format specifier.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I am obviously not a very good programmer. I have written this small
I have an issue and NO it is not homework, it's just a programmer
I have just started my career as an android programmer, and am currently relying
Okay firstly I just started C# so I'm not exactly the most skilled programmer
I'm a Java programmer, but not a JavaScript programmer. I have just discovered YUI,
I'm not a C++ programmer and have difficulty understanding the explanations given on websites.
Since I'm not an Php programmer I have to ask you, I have to
I am not a VBA programmer. However, I have the 'unpleasant' task of re-implementing
This not a programming question but Most of the programmers using Eclipse should have
I am not professional programmer so i can not be sure about this.How many

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.