【问题标题】:C program failing to append integers to vector correctlyC 程序未能正确地将整数附加到向量
【发布时间】:2020-11-10 11:02:33
【问题描述】:

我有一个 C 程序,我想在其中数组工作不能完全被进程数整除的进程之间划分一个二维数组。然而,这是我的小程序,我计算将分配给每个进程的大二维数组的起始 x 和 y 索引(以及每个进程将获得多少行和列)并将结果附加到向量。将元素附加到向量时出现问题(我已打印出 i_start 和 j_start 向量的程序输出)。我不确定我做错了什么,但任何帮助都会很棒。谢谢。

#include <stdio.h>
#include <math.h>

int main() {
  int K = 10;
  int L = 10;
  int P = 8;

  double pco = sqrt(P);
  int pcol = (int)pco;

  while((P % pcol) != 0) {
    pcol--;
  }

  int prow = P/pcol;

  int KL = K*L;

  int coldiv = K/pcol;
  int rowdiv = L/prow;

  int colrem = K % pcol;
  int rowrem = L % prow;

  int map[L][K];

  int count = 1;
  for(int i=0; i < L; i++) {
    for(int j=0; j < K;j++) {
      map[i][j] = count;
      count++;
    }
  }

  int i_start[P];
  int j_start[P];
  int n_rows[P];
  int n_cols[P];

  int start;
  int num_cols = 0;
  int count1 = 0;

  // appending to vector here
  for(int j = 0; j < prow; j++) {
    for(int i = 0; i < pcol; i++) {
      if(i < colrem) {
        start = (i*(coldiv+i));
        num_cols = coldiv + 1;
        printf("col index start: %d\n", i*(coldiv+i));
        printf("col index finish: %d\n", i*(coldiv+i)+coldiv+1);
      }
      else {
        start = (i*coldiv+colrem);
        num_cols = coldiv;
        printf("col index start: %d\n", i*coldiv+colrem);
        printf("col index finish: %d\n", i*coldiv+colrem+coldiv);
      }
      i_start[count1] = start;
      n_cols[count1] = num_cols;
      count1++;
    }
  }

  int count2 = 0;
  for(int j = 0; j < pcol; j++) {
    for(int i = 0; i < prow; i++) {
      int start, num_rows;
      if(i < rowrem) {
        int start = i*(rowdiv+i);
        num_rows = rowdiv + 1;
        printf("row index start: %d\n", i*(rowdiv+i));
        printf("row index finish: %d\n", i*(rowdiv+i)+rowdiv+1);
        j_start[count2] = start;
      }
      else {
        int start = i*rowdiv+rowrem;
        num_rows = rowdiv;
        printf("row index start: %d\n", i*rowdiv+rowrem);
        printf("row index finish: %d\n", i*rowdiv+rowrem+rowdiv);
        j_start[count2] = start;
      }
      n_rows[count2] = num_rows;
      count2++;
    }
  }

  // error: printing vectors
  for(int i = 0; i < P; i ++) {
    printf("i: %d\n", i);
    printf("i start: %d\n",i_start[P]);
    printf("j start: %d\n",j_start[P]);
  }
}

程序输出:

col index start: 0
col index finish: 5
col index start: 5
col index finish: 10
col index start: 0
col index finish: 5
col index start: 5
col index finish: 10
col index start: 0
col index finish: 5
col index start: 5
col index finish: 10
col index start: 0
col index finish: 5
col index start: 5
col index finish: 10
row index start: 0
row index finish: 3
row index start: 3
row index finish: 6
row index start: 6
row index finish: 8
row index start: 8
row index finish: 10
row index start: 0
row index finish: 3
row index start: 3
row index finish: 6
row index start: 6
row index finish: 8
row index start: 8
row index finish: 10
i: 0
i start: 1
j start: 0
i: 1
i start: 1
j start: 0
i: 2
i start: 1
j start: 0
i: 3
i start: 1
j start: 0
i: 4
i start: 1
j start: 0
i: 5
i start: 1
j start: 0
i: 6
i start: 1
j start: 0
i: 7
i start: 1
j start: 0

【问题讨论】:

    标签: arrays c vector multidimensional-array integer


    【解决方案1】:

    如果我理解你的意图,你只是打印了错误的输出。

    for(int i = 0; i < P; i ++) {
        printf("i: %d\n", i);
        printf("i start: %d\n",i_start[P]);
        printf("j start: %d\n",j_start[P]);
    }
    

    在这里,您正在为 i_startj_start 打印始终与 P 而不是 i 相同的索引。

    改为:

     for(int i = 0; i < P; i ++) {
         printf("i: %d\n", i);
         printf("i start: %d\n",i_start[i]);
         printf("j start: %d\n",j_start[i]);
     }
    

    您的输出将变为:

    col index start: 0
    col index finish: 5
    col index start: 5
    col index finish: 10
    col index start: 0
    col index finish: 5
    col index start: 5
    col index finish: 10
    col index start: 0
    col index finish: 5
    col index start: 5
    col index finish: 10
    col index start: 0
    col index finish: 5
    col index start: 5
    col index finish: 10
    row index start: 0
    row index finish: 3
    row index start: 3
    row index finish: 6
    row index start: 6
    row index finish: 8
    row index start: 8
    row index finish: 10
    row index start: 0
    row index finish: 3
    row index start: 3
    row index finish: 6
    row index start: 6
    row index finish: 8
    row index start: 8
    row index finish: 10
    i: 0
    i start: 0
    j start: 0
    i: 1
    i start: 5
    j start: 3
    i: 2
    i start: 0
    j start: 6
    i: 3
    i start: 5
    j start: 8
    i: 4
    i start: 0
    j start: 0
    i: 5
    i start: 5
    j start: 3
    i: 6
    i start: 0
    j start: 6
    i: 7
    i start: 5
    j start: 8
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多