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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:40:28+00:00 2026-05-21T18:40:28+00:00

I wrote the following function to make a 2-D array object odd-dimensioned by adding

  • 0

I wrote the following function to make a 2-D array object odd-dimensioned by adding a row or column as needed (where sizeX, sizeY, get, set, and resize are self-explanatory grid2D member functions).

void makeOdd(grid2D<double> *pSrc)
// ---------------------------------------------------------------------------------------------------------
// Make one or both dimensions of input array odd (via row/column copy).
{

    // Variable declarations
    grid2D<double> pTmp = *pSrc;                                // Scratch local source variable
    int simax, sjmax;                                               // Source dimensions

    // Get source dimensions
    sjmax = pSrc->sizeY();
    simax = pSrc->sizeX();

    // Check if source is already odd-dimensioned
    if (sjmax%2 && simax%2) return;

    // Extend row/column of source if necessary
    if (sjmax%2 && !(simax%2))                                      // Odd rows, even columns
    {
        pSrc->resize(simax+1,sjmax);                                // Resize source with extra column
        for(int i=0; i<simax+1; i++)
        {
            for(int j=0; j<sjmax; j++)
            {
                if(i==simax)
                    pSrc->set(i,j,pTmp.get(simax-1,j));             // Copy last column
                else
                    pSrc->set(i,j,pTmp.get(i,j));
            }
        }
        return;
    }
    else if (!(sjmax%2) && simax%2)                                 // Even rows, odd columns
    {
        pSrc->resize(simax,sjmax+1);                                // Resize source with extra row
        for(int i=0; i<simax; i++)
        {
            for(int j=0; j<sjmax+1; j++)
            {
                if(i==simax)
                    pSrc->set(i,j,pTmp.get(i,sjmax-1));             // Copy last row
                else
                    pSrc->set(i,j,pTmp.get(i,j));
            }
        }
        return;
    }
    else                                                            // Even rows, even columns
    {
        pSrc->resize(simax+1,sjmax+1);                              // Resize source with extra row and column
        for(int i=0; i<simax+1; i++)
        {
            for(int j=0; j<sjmax+1; j++)
            {
                if(i==simax && j==sjmax)
                {
                    pSrc->set(i,j,pTmp.get(simax-1,sjmax-1));       // Copy last column and row
                }
                else if(i==simax && j<sjmax)
                {
                    pSrc->set(i,j,pTmp.get(simax-1,j));             // Copy last column
                }
                else if(i<simax && j==sjmax)
                {
                    pSrc->set(i,j,pTmp.get(i,sjmax-1));             // Copy last row
                }
                else
                {
                    pSrc->set(i,j,pTmp.get(i,j));
                }
            }
        }
        return;
    }

}

My question: is there a cleaner/more efficient way of doing this?

Many thanks…

  • 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-05-21T18:40:29+00:00Added an answer on May 21, 2026 at 6:40 pm

    I think it would be easier to check the number of rows first, and extend it by 1 if needed. Then, check the number of columns, and extend each of them by 1 if needed.

    Assuming your grid2d is a wrapper around a vector<vector<T> >, you can pass a value to resize that it will use to fill the newly created space, so when you add a row, you can just pass current last row to get it copied into the new last row.

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

Sidebar

Related Questions

I wrote the following function to view data in a grid from F# interactive:
Let's say I have the following method: public static int CountNonNullMembers<T>(this IEnumerable<T> enumerable) {
I have been unable to figure out this problem - it just doesn't make
Good day, TValue is a Delphi-2010 and up RTTI feature. Following on from my
I am trying to write a php function to save and then display comments
I'm rather new to both Java programming and to Android development, so my learning
Firstly, sorry about the long question... but this is doing my head in. Any
Here's the code I'll be talking on (keep this in mind throughout this post):
Sorry about the title but I can't really think of a quick way of
What I wanted to do was to search in a list and remove a

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.