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

  • Home
  • SEARCH
  • 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 8903297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:50:03+00:00 2026-06-15T01:50:03+00:00

Suppose: 2D array: abcdef ghijkl mnopqr Stored in simple string of length width *

  • 0

Suppose:

2D array:  abcdef
           ghijkl
           mnopqr

Stored in simple string of length width * height, thus, let’s call it arr.

arr = abcdefghijklmnopqr
width = 6
height = strlen ( arr ) / width

The goal is to rotate this array by 45 degrees ( PI/4 ) and get the following result:

arr = abgchmdinejofkplqr
width = 3
height = 8
converted to 2D array:  a..
                        bg.
                        chm
                        din
                        ejo
                        fkp
                        .lq
                        ..r

I have spent a few hours trying to figure out how to make this conversion and came up with a few semi-functional solutions, but I can’t get it fully working. Can you describe / write an algorithm that would solve this? Preferably in C.

Thanks for any help

Edit: This is what I’ve tried already
Edit2: The purpose of 45 degree rotation is to turn diagonals into lines so that they can be searched using strstr.

// this is 90 degree rotation. pretty simple
for ( i = 0; i < width * height; i++ ) {
  if ( i != 0 && !(i % height) ) row++;
  fieldVertical[i] = field[( ( i % height ) * width ) + row];
}   

// but I just can't get my head over rotating it 45 degrees. 
// this is what I've tried. It works untile 'border' is near the first edge.

row = 0;
int border = 1, rowMax = 0, col = 0; // Note that the array may be longer
// than wider and vice versa. In that case rowMax should be called colMax.

for ( i = 0; i < width * height; ) { 
  for ( j = 0; j < border; j++, i++ ) {
    fieldCClockwise[row * width + col] = field[i];
    col--;
    row++;
  }

  col = border;
  row = 0;
  border++;
}

The ‘border’ in my code is an imaginary borderline. In the source, it is a diagonal
line that separates diagonals. In the result, it would be a horizontal line between
each row.

1   2   3 / 4   5
6   7 / 8   9   10
11 /12  13  14  15

Those slashes are our borderline.
The algorithm shoul be pretty simple and just read riagonals:
first number 1, then 2, then 6, then 3, then 7, then 11, then 4 and so on.

  • 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-15T01:50:05+00:00Added an answer on June 15, 2026 at 1:50 am

    I’d call this diagonal scanning, not rotation by 45 degrees.

    In your example, the diagonals are running down-left; we can enumerate them 1, 2, …:

    123456
    234567
    345678
    

    This will be the counter for the iterations of the outer loop. The inner loop will run 1, 2 or 3 iterations. In order to jump from one numbered symbol to the other, do col--; row++; like you did, or add width-1 to a linear index:

    ....5. (example)
    ...5..
    ..5...
    

    Code (untested):

    char *field;
    int width = 6;
    int height = 3;
    char *field45 = malloc(width * height);
    int diag_x = 0, diag_y = 0; // coordinate at which the diagonal starts
    int x, y; // coordinate of the symbol to output
    while (diag_y < height)
    {
        x = diag_x; y = diag_y;
        while (x >= 0 && y < height) // repeat until out of field
        {
            *field45++ = field[y * width + x]; // output current symbol
            --x; ++y; // go to next symbol on the diagonal
        }
        // Now go to next diagonal - either right or down, whatever is possible
        if (diag_x == width - 1)
            ++diag_y;
        else
            ++diag_x;
    }
    

    If you want to “rotate” in another direction, you may want to change ++ to -- around the code, and maybe change bound checking in the loops to opposite.

    In addition, you can replace (x,y) coordinates by just one index (replacing ++y by index+=width); i used (x,y) for clarity.

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

Sidebar

Related Questions

So suppose I create an array with 5 spaces, like so - String[] myArray
Suppose I have an array unsigned char arr[]= {0,1,2,3,4,5,6,7,8,9}; Is there a way to
Suppose I have an array of strings in Scala : val strings = Array[String](1,
Suppose I have an array String[] which has 47 characters in each element. The
Suppose I have a two dimensional array in C++ under CUDA, stored in the
Suppose I have an array int arr[] = {...}; arr = function(arr); I have
Suppose array index according to hashing function for string temp is 155 and location
Suppose I have an array of string boxed to an object: string[] files =
Suppose i have an array $x = (31,12,13,25,18,10); I want to reduce this array
Suppose I have an array of values [a,b,c,d,...] and a function f(x,...) which returns

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.