【问题标题】:Create 2-D Array and Set Values创建二维数组并设置值
【发布时间】:2017-12-07 01:19:18
【问题描述】:

我正在尝试编写一个 C 程序来生成解决线性方程组的 R 文件。在 main 中,我有六个嵌套的 for 循环来获取系数为整数 0 - 9 的每次迭代:

ax + by + c = dx + ey + f
a_2x + b_2y + c_2 = d_2x + e_2y + f

每个方程都是一个由 6 个整数系数组成的数组。在将主函数传递给 generateContentForSystems 之前,我在主函数中设置了系数的值。

但是,我的打印语句返回: value of numx:-000-0 value of numy:000-0 value of numz:00-0 value of numx_2:0-0 value of numy_2:-0 value of numz_2:0 我相信这是因为指针算法不好。我现在正在尝试从一个指向数组的指针(在 main 中)并有一个数组数组。

#include <stdio.h> 
#include <math.h> 
#include <stdlib.h> 
#include <string.h> 
#include "scaffold.c"

void * generateContentForSystems(int * row1, int * row2) {
  static int index = 0;
  int x = row1[0] - row1[3];
  int y = row1[1] - row1[4];
  int z = row1[5] - row1[2];

  int x_2 = row2[0] - row2[3];
  int y_2 = row2[1] - row2[4];
  int z_2 = row2[5] - row2[2];

  int prod1 = x * y_2;
  int prod2 = x_2 * y;
  int determinant = prod1 - prod2;
  if (determinant != 0) {
    printf("the value of determinant: %d", determinant);
    char * error1;
    char Q[1000];
    strcpy(Q, "emake <- function(){\noptions(\"warn\"=-1)\ne <- 0\nfor (n in 0:2000){\ne <- e+ 1/(factorial(n))\n}\nreturn(e)\n}\ne <- emake()\n");
    char numx[1];
    char numy[1];
    char numz[1];
    char numx_2[1];
    char numy_2[1];
    char numz_2[1];
    sprintf(numx, "%d", x); 
    sprintf(numy, "%d", y); 
    sprintf(numz, "%d", z); 
    sprintf(numx_2, "%d", x_2); 
    sprintf(numy_2, "%d", y_2); 
    sprintf(numz_2, "%d", z_2); 

      //debug:
 printf("value of numx:%s value of numy:%s value of numz:%s value of numx_2:%s value of numy_2:%s value of numz_2:%s", numx, numy, numz, numx_2, numy_2, numz_2);

    strcat(Q, "A = array(c(");
    strcat(Q, numx);
    strcat(Q, ", ");
    strcat(Q, numx_2);
    strcat(Q, ", ");
    strcat(Q, numy);
    strcat(Q, ", ");
    strcat(Q, numy_2);
    strcat(Q, "), dim = c(2,2,1))\n");
    strcat(Q, "b = c(");
    strcat(Q, numz);
    strcat(Q, ", ");
    strcat(Q, numz_2);
    strcat(Q, ")\n");
    strcat(Q, "solve(A[,,1],b)\n");

    char filename[100];
    char snum[5];
    itoa(index, snum);
    index++;
    strcpy(filename, "practice/");
    strcat(filename, snum);
    strcat(filename, ".R");
    FILE * F = fopen(filename, "w");
    fputs(Q, F);
    fclose(F);
    char path[1024];
    char command[300];
    strcpy(command, "Rscript ");
    strcat(command, "practice/");
    debug("After Rscript formation");
    strcat(command, snum);
    strcat(command, ".R");
    FILE * fp = popen(command, "r");
    if (!fp) { //validate file is open 
      return NULL;
    }

    while (fgets(path, sizeof(path) - 1, fp) != NULL) {
      debug("in Primary While Loop");
      fflush(stdout);
      printf("the solution: %s", path);
      if (strstr(path, ".") > strstr(path, "with absolute error") || strstr(path, ".5 ") != NULL) {
        printf("answer was accepted");

      }
    }

  }
}

int main() {

  int arrayIndexes = 0;
  int ** myArray = malloc(1 * sizeof( * myArray));

  for (int a = 0; a < 10; a++) {
    for (int b = 0; b < 10; b++) {
      for (int c = 0; c < 10; c++) {
        for (int d = 0; d < 10; d++) {
          for (int e = 0; e < 10; e++) {
            for (int f = 0; f < 10; f++) {


              myArray[arrayIndexes] = malloc(6 * sizeof(int));
              myArray[arrayIndexes][0] = a;
              myArray[arrayIndexes][1] = b;
              myArray[arrayIndexes][2] = c;
              myArray[arrayIndexes][3] = d;
              myArray[arrayIndexes][4] = e;
              myArray[arrayIndexes][5] = f;

              if (arrayIndexes > 0) {
                for (int i = 0; i < arrayIndexes; i++) {
                  generateContentForSystems(myArray[arrayIndexes], myArray[i]);
                }
              }

              ++arrayIndexes;
              myArray = realloc(myArray, (arrayIndexes + 1) * sizeof( * myArray));

            }
          }
        }
      }
    }

  }
  for (int n = 0; n = arrayIndexes; n++) {
    free(myArray[n]);
  }
  free(myArray);

  return 0;
}

【问题讨论】:

  • 您的代码中没有二维数组,也没有可以作为一个数组使用。
  • 是int数组的数组不是二维数组吗?
  • 肯定是数组数组是二维数组。但是您的代码中没有数组数组。指针不是数组
  • 当您可以使用正确的格式说明符调用printf 时,为什么还要使用sprintf 创建字符串以提供给printf
  • strcat 的重复调用可以替换为对sprintf 的一次调用。

标签: c r matrix multidimensional-array


【解决方案1】:

你的数字字符串不够长:

char numx[1];
char numy[1];
char numz[1];
char numx_2[1];
char numy_2[1];
char numz_2[1];

字符串由一系列字符加上一个空终止字节组成。因此,即使是单个数字也需要至少为 2 的数组大小。当您使用 sprintf 将数字的文本表示形式写入其中一个数组时,您将写入数组的末尾。这会调用undefined behavior

这些数组需要足够大以容纳您可能传入的任何值,如果需要,包括一个否定符号。

char numx[10];
char numy[10];
char numz[10];
char numx_2[10];
char numy_2[10];
char numz_2[10];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    • 2016-12-27
    • 2011-10-19
    相关资源
    最近更新 更多