2012-02-25 89 views
-3

我试图找出如何以这种方式填充多维数组: 输入:行数= 3的cols = 3:C++多维数组

1 4 7 
2 5 8 
3 6 9 

有人可以给我一个想法?

P.S我的任务是找出在两种排列中有多少个纽结在同一位置。例如:

1 4 7  1 2 3 
2 5 8  4 5 6 
3 6 9  7 8 9 

,这样在同一个位置上的数字是:1 5 9 我已经tryied:

//n = 3 , m = 3 
for(int i = 0; i <n; i++) { 
for(int j = 0; j <m; j++){ 
if(array[i][j] == array2[i][j]) { 
lol++; 
} 
} 

} 
cout<<lol; 


/* 
1 2 3 
4 5 6 
7 8 9 


1 4 7 
2 5 8 
3 8 9 
*/ 

它必须告诉我3,但它显示0,这里是问题?

+0

我很好奇,你有没有把任何努力进入发布前解决这个问题?我知道答案,但为什么重复的代码可以在30秒内通过简单的Google搜索找到?哦,如果你不喜欢谷歌,这里是直接链接:http://www.cplusplus.com/forum/articles/7459/ – 2012-02-25 15:45:06

+0

我给你的代码。 'int i = 1; i <= n'不同于'int i = 0;我 2012-02-25 15:59:00

+0

@LuchianGrigore哦,我错过了。但现在,主要的问题是,如何找到我在说明中解释的那些数字。 – ddacot 2012-02-25 16:04:35

回答

2

填充在初始化:

int a[3][3] = { { 1, 4, 7}, 
       { 2, 5, 8}, 
       { 3, 6, 9} 
       }; 

EDIT(不确定如果解决):

更新到这里的问题是一个示例应用程序(具有修改以接受来自用户的输入)将会给出两个阵列和后构造的阵列表示是相同的元件和相同的元件的数目的计数:

#include <iostream> 

int** make_array(const size_t a_rows, const size_t a_columns) 
{ 
    int** result = new int*[a_rows]; 
    for (size_t i = 0; i < a_rows; i++) 
    { 
     *(result + i) = new int[a_columns]; 
    } 
    return result; 
} 

void print_array(int** a_array, const size_t a_rows, const size_t a_columns) 
{ 
    for (size_t r = 0; r < a_rows; r++) 
    { 
     for (size_t c = 0; c < a_columns; c++) 
     { 
      std::cout << *(*(a_array + r) + c) << " "; 
     } 
     std::cout << "\n"; 
    } 
    std::cout << "\n"; 
} 

int main() 
{ 
    // Example data. 
    int a[3][3] = { { 1, 4, 7}, 
        { 2, 5, 8}, 
        { 3, 6, 9} 
        }; 
    int b[3][3] = { { 1, 2, 3}, 
        { 4, 5, 6}, 
        { 7, 8, 9} 
        }; 
    size_t rows = 3; 
    size_t columns = 3; 

    // Create three arrays: 
    // - two input arrays 
    // - array that represents which elements are the same 
    int** in_1 = make_array(rows, columns); 
    int** in_2 = make_array(rows, columns); 
    int** diff = make_array(rows, columns); 

    // Populate with example data. 
    for (size_t r = 0; r < rows; r++) 
    { 
     for (size_t c = 0; c < columns; c++) 
     { 
      *(*(in_1 + r) + c) = a[r][c]; 
      *(*(in_2 + r) + c) = b[r][c]; 
     } 
    } 

    // Diff. 
    // The 'diff' array will hold '1' for elements that 
    // were the same and '0' for elements that were not. 
    size_t same_count = 0; 
    for (size_t r = 0; r < rows; r++) 
    { 
     for (size_t c = 0; c < columns; c++) 
     { 
      *(*(diff + r) + c) = *(*(in_1 + r) + c) == *(*(in_2 + r) + c); 
      same_count += *(*(diff + r) + c); 
     } 
    } 
    std::cout << "\n"; 

    // Results. 
    print_array(in_1, rows, columns); 
    print_array(in_2, rows, columns); 
    print_array(diff, rows, columns); 
    std::cout << "Same element count: " << same_count << "\n"; 

    // Free... 

    return 0; 
} 

输出:

$ ./cpp/main.exe 

1 4 7 
2 5 8 
3 6 9 

1 2 3 
4 5 6 
7 8 9 

1 0 0 
0 1 0 
0 0 1 

Same element count: 3 
1

创建一个动态分配的数组,如果你只知道在运行时尺寸:

int** x = new int*[rows]; 
for (int i = 0 ; i < rows ; i++) 
    x[i] = new int[cols]; 

然后填充它:

for (int i = 0 ; i < rows ; i++) 
for (int j = 0 ; i < cols ; j++) 
    x[i][j] = y; 

或者更好的是,使用向量的向量,这会给你更多的灵活性:

std::vector<std::vector<int> > x; 
0

简单的解决办法是这样的: -

int k = 1; 
for(int i = 0; i < row; i++){ 
    for(int j = 0; j < col; j++){ 
    a[j][i] = k; // filling it in the column first order 
    ++k; 
    } 
}