【问题标题】:Usage of 2D array in C++ globally在 C++ 中全局使用二维数组
【发布时间】:2014-04-23 14:23:24
【问题描述】:

我的问题是我想用两个矩阵相乘来更新一个矩阵,使用线程。以下是我的代码

#include <iostream>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <iostream>
#include <ctime>
#include <cmath>
#include <vector>

using namespace std;

int thread_count; // For Command Line Argument
int l;
int m;
int n;
int start_Task;
int stop_Task;

void *Hello(void* rank); // Prototype of a thread function

int main(int argc,char* argv[])
{      
int thread_id;

thread_count = atoi(argv[1]);
int l = atoi(argv[2]);  
int m = atoi(argv[3]);  
int n = atoi(argv[4]);  

int A[l][m]; //creates A l*m matrix or a 2d array. 

for(int i=0; i<l; i++)    //This loops on the rows.
{
    for(int j=0; j<m; j++) //This loops on the columns
    {
        A[i][j] = i+j; // Allocating values to A matrix
    }
}

int B[m][n]; //creates B m*n matrix or a 2d array.
for(int i=0; i<m; i++)    //This loops on the rows.
{
    for(int j=0; j<n; j++) //This loops on the columns
    {
        B[i][j] = i+j+1; // Allocating values to B matrix
    }
}

int C[l][n]; //creates C m*n matrix or a 2d array.

int remainder = l % thread_count;
int allocatedProcess = l/thread_count;
start_Task =0; 
//cout<<remainder<<" "<<allocatedProcess<<endl;
pthread_t myThreads[thread_count]; 

//creates a certain number of threads
for(thread_id = 0; thread_id < thread_count; thread_id++)
   {    
    stop_Task = start_Task + allocatedProcess;
    pthread_create(&myThreads[thread_id], NULL,Hello, (void*)thread_id);
start_Task = stop_Task; 
}

cout<<"Hello from the main thread \n"<<endl;

for(int i=0; i<l; i++)    //This loops on the rows.
{
    for(int j=0; j<n; j++) //This loops on the columns
    {
        cout<<"Values in Final Matrix is"<<C[i][j]<<endl;
    }
}
 //wait until all threads finish
 for(thread_id = 0; thread_id<thread_count; thread_id++)        
pthread_join(myThreads[thread_id],NULL);

 return 0;
 }//main

 void *Hello(void* rank)
 {
for(int i=start_Task; i<stop_Task; i++)    //This loops on the rows.
{
    for(int j=0; j<n; j++) //This loops on the columns
    {
        int sum = 0;
        for(int k=0; k<m; k++) //This loops on the columns
        {
             sum += A[i][k] * B[k][j]; // Allocating values to C matrix
              //product[row][col] += aMatrix[row][inner] * bMatrix[inner][col];
            }
     C[i][j] = sum;
    cout << C[i][j] << "  ";
          }
       cout << "\n";
}

  int my_rank = (long)rank;

  cout<<"Thread_"<<my_rank<<" of "<<thread_count<<endl;

  return NULL;
  }

我面临的问题是我无法让我的矩阵全局初始化,因为维度本身就是全局的。请更新我正确的方法来实现它。

【问题讨论】:

  • 如果有任何解决方法请更新 ////

标签: c++ dynamic pthreads multidimensional-array


【解决方案1】:

你可以通过声明一个指针来做到这一点。

int *my_array;

在任何方法中,您都可以使用以下几行:

std::cin >> n;
my_array = new int[n];

希望这会有所帮助.. :)

【讨论】:

  • 我对您的回答的唯一批评是该链接指向 C 问题,而不是 C++ 问题。
【解决方案2】:

简短的回答是,C 和 C++ 中的数组必须设置为常量,而不是变量。

l 和 m 在您的示例中不是常量。您可以使用 std::vector。如果您使用的是 C++11 编译器,那么您还有其他几个选择。这是一个可能有帮助的线程,但很严重; google "2d dynamic arrays c++" 看看还有什么结果。这对你来说将是一个很好的练习,并且可以让我免于复制和粘贴所有这些精彩文章!

http://www.cplusplus.com/forum/beginner/12409/

【讨论】:

  • C 中的数组可以设置变量。
猜你喜欢
  • 2021-12-30
  • 2012-12-11
  • 1970-01-01
  • 2013-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-19
相关资源
最近更新 更多