微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用指针传递/返回2d数组的c ++程序不会打印任何内容

如何解决使用指针传递/返回2d数组的c ++程序不会打印任何内容

我仍然试图摆脱使用指针向函数传递/返回数组的麻烦,还是它也被称为动态数组?

我仍然不清楚如何以这种方式传递数组/返回数组(请说明)

我真的无法在线找到解决方案。我尝试使用在线提供的资料来编写代码,但似乎无济于事。

在下面的代码中,程序应调用makeMS函数以创建n x n的幻方(ms),n的大小由用户指定,然后应将其返回。

然后,将ms数组传递给printTable函数以打印数组元素。

然后isMagicSquare将使用数组并检查所有行,列和对角线是否具有相同的总和(魔术常数)。

请,任何帮助都将是惊人的

这也是输出的屏幕截图

enter image description here

#include <iostream>
#include <iomanip>
#include <cstring>

int i,j,n,num;

using namespace std;

//Function to check if array is a magic square or not
bool isMagicSquare (int **magicSquare,int n) {
    
    //get the sums of the 2 diagonals if the sum is the same
    
    int sum1 = 0,sum2 = 0; 
    for (i = 0; i < n; i++) {
        sum1 += magicSquare[i][i];  
    }
    
    for (i = 0; i < n; i++) {
        sum2 += magicSquare[i][n-1-i];
    }
    
    if(sum1 != sum2) 
        return false;
    
        
    //Now for the sums of rows
    for ( i = 0; i < n; i++) {
        
        int rowSum = 0;
        for (j = 0; j < n; j++) {
            rowSum += magicSquare[i][j];
        }
        
        if (rowSum != sum1) 
            return false;
        
    }
    
    //sum of the columns
    for ( i = 0; i < n; i++) {
        
        int colSum = 0;
        for (j = 0; j < n; j++) {
            colSum += magicSquare[j][i];
        }
        
        if ( sum1 != colSum )
        return false;
        
    
}

return true;

}

// Print magic square 
void printTable (int **magicSquare,int n) {
    
       
    
    cout << "The Magic Square for n=" << n << ":\nSum of "
   
    "each row or column " << n * (n*n+1) / 2 << ":\n\n";  
    
    
    for (i = 0; i < n; i++)  
    {  
        for (j = 0; j < n; j++)  
            cout << setw(5) << magicSquare[i][j] << " ";  
        cout << endl; 
    }  
    
}

//Function that constructs the magic square
int **makeMS (int n) {
    
    int **tableArr = new int*[n];
    
    //makes all slots 0
    memset(tableArr,sizeof(tableArr));
    
    
    //Position of 1st number
    i = 0;
    j = n/2;
    
    //Step to magic square construction
    for (num = 1; num <= n*n;) {
        
        //if the position of k + 1 is tableArr[-1][n],3rd condition
        if ( i == -1 && j == n) {
            
            j -= 1;
            i += 2;
        }
        
        else {
            
    
            if (j == n) {
                j = 0;
            }
            
            if(i < 0) {
                i = n - 1; 
            }
            
        }
        
        //3rd condition (occupied slot)
        if (tableArr[i][j]) {
            i+= 2;
            j -= 1;
            continue;
        }
        
        else {
            tableArr[i][j] = num++; //set number  
  
        j++; i--; //1st step
        
        }   
    }

    return tableArr;
    
 
}

int main () {
    
    int sum;
    int **magicSquare;

        cout << "Please enter size of array [n] \n";
        cin >> n; 
        
        if(n%2) {
            magicSquare = makeMS(n);
        }
        else
            cout << "Please enter an odd no for the array size";
    
    printTable(magicSquare,n);

    
    if (isMagicSquare(magicSquare,n)) {
        cout << "\n \nThe Matrix/Array is a Magic Square \n\n" ;
            
    }
    
    else {
        cout << "\n The Matrix/Array is NOT a Magic Square";
        }
        
}

解决方法

您需要使用makeMS方法修复二维数组分配和初始化,如下所示-

int** tableArr = new int* [n];

for (int i = 0; i < n; i++) {
   tableArr[i] = new int[n] {0};
}

由于在for循环中进行了上述初始化,因此不需要调用memset方法。

,

主要问题是:使用c样式的数据结构,例如array [],[] [],**等。

不幸的是,c ++没有内置矩阵数据结构(有许多库提供诸如之类的矩阵实现)。我强烈建议使用适当的矩阵库。也可以使用以下方法轻松制作稀疏矩阵(如果需要,可以自己跟踪最大尺寸),例如:

std::map< std::tuple<size_t,size_t>,int> sparse_matrix;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。