创建二维数组和设置值

时间:2017-07-03 17:31:44

标签: c r matrix multidimensional-array

我正在尝试编写一个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之前,我在main函数中设置了系数的值。

然而,我的print语句返回: 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;
}

1 个答案:

答案 0 :(得分: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];