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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:14:29+00:00 2026-06-02T05:14:29+00:00

I am new to C++ and am trying to write code for a multi-dimensional

  • 0

I am new to C++ and am trying to write code for a multi-dimensional array using double pointers. This is my code:

Class Declaration:

class magicMat{

    private:
         int** ptrnum;

    public:
        void init(int);
        void Display(int);
        void set(int);
        void message();
        void errorhandling(int);    
};

Function definitions:

void magicMat::init(int input)
{       
    ptrnum=new int*[input];

    for (int row=0;row<input;row++)
        ptrnum[row]=new int[input]; 

    for(int x=0;x<input;x++)
    {
        for (int y=0;y<input;y++)
        {
            *(ptrnum[x]+y)=0;
        }
    }
}

void magicMat::set(int input)
{
    int row=1,col=input/2,otherdiag=0;

    for(int value=1;value<=input*input;value++)
    {
        if (*(ptrnum[row]+col)>0)
        {
            row=row+2;
            if(row>input)
                row=row-input;

            col--;
            if(col<1)
                col=input;
        }
        *(ptrnum[row]+col)+=value;
        *(ptrnum[0]+col)+=value;
        *(ptrnum[row]+0)+=value;

        if (row==col)
            *(ptrnum[0]+0)+=value;          

        if (row+col==input+1)
            otherdiag+=value;                 
/*                                                                        */
/*       Determine where new row and col are                              */
/*                                                                     */
         row--;
         if (row < 1)                       /* If row exceeds side then   */
            row = input;                    /*  goto other side.          */
         col++;
         if (col > input)                   /* If col exceeds side then   */
            col = 1; 
    }       
}

Main function:

int main()
{
    int num;
    magicMat newMat;
    newMat.message();
    while(1)
    {
        cin>>num;
        if (cin.good())
        {
            newMat.errorhandling(num);
        }
        else if (!isdigit(num))
        {
            cout<<"Please enter only digits"<<endl;
        }    
        newMat.init(num);
        newMat.set(num);
        newMat.Display(num);
    }
    cout<<"\nBye bye!\n"<<endl;
    return 0;
}

It works in the init function but when in the set function I try to check the value it breaks at the first if statement in the set data function.

  • 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-02T05:14:30+00:00Added an answer on June 2, 2026 at 5:14 am

    You are stepping outside the bounds of your array. Just as an example, if I run your code and enter 5 as my first digit which sets the value of input to 5, look at these lines at the end of your set function:

    row--;
    if (row < 1)
        row = input;
    

    At the end of the first time through the loop in your set function, right before these lines, row is equal to 1. These three lines execute and as a result, row is set equal to 5.

    At the beginning of your next loop in set, you do this:

    if (*(ptrnum[row]+col)>0)
    

    The problem is that ptrnum is a 5×5 array, which means valid indexes are from 0-4. At the end of the previous loop, however, you set row equal to 5 and as a result, you’re indexing outside the bounds of ptrnum and thus crashing your program.

    I highly recommend either stepping through your code in a debugger or if you’re not sure how to do that, at least put in a bunch of cout statements in your set function so you can check that the variables are getting set to what you think they should get set to.

    Good luck!

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

Sidebar

Related Questions

I am trying to write code to shuffle an Array without using Collections. My
I am a programmer, new to jsp, trying to write some code. i have
All, I am new to JQuery and trying to write JQuery code to create
in this code I'm trying to Listening to multi connection at the same time
I have a multi-dimensional array that I am trying to feed into difflib.get_close_matches() .
I'm new to Visual Studio, and I'm just trying to write code as efficiently
I'm trying to write code to convert a WCF wsHttpBinding to customBinding, using the
I am trying to write a code to generate a graph like this: http://www.mathgoodies.com/lessons/graphs/images/line_example1.jpg
I'm new to jQuery and i'm trying to write some code to go through
I am new to php and trying to write a login function. Bit stuck

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.