2017-07-03 142 views
0

我正在嘗試編寫一個C程序來生成解決線性方程組的R文件。在主,我有六個嵌套的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; 
} 
+0

您的代碼中沒有二維數組,可以作爲一個使用。 – Olaf

+0

是一個不是二維數組的數組? – Matt

+0

當然是陣列數組的一個二維數組。但是你的代碼中沒有數組的數組。指針是**不是數組**。 – Olaf

回答

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];