2017-07-03 1 views
0

Ich versuche, ein C-Programm zu schreiben, das R-Dateien generiert, die lineare Gleichungssysteme lösen. In Haupt habe ich sechs verschachtelte Schleifen für jede Iteration zu erhalten, wobei die Koeffizienten für ganze Zahlen von 0 bis 9:Create 2-D Array und Set-Werte

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

Jede Gleichung ist eine Anordnung von 6 ganzzahligen Koeffizienten. Ich setze die Werte der Koeffizienten in meiner Hauptfunktion, bevor ich sie an generateContentForSystems übergebe.

jedoch mein Druck Anweisung gibt: 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 Ich glaube, dies wegen der schlechten Zeigerarithmetik ist. Ich versuche jetzt, von und zu einem Zeiger auf ein Array (in main) und habe ein Array von Arrays.

#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

Es gibt kein 2D-Array in Ihrem Code und Nothing, das als eins verwendet werden kann. – Olaf

+0

ist ein Array von int-Arrays nicht ein 2D-Array? – Matt

+0

Sicher ist ein Array von Arrays ein 2D-Array. Aber es gibt kein Array von Arrays in Ihrem Code. Ein Zeiger ist ** kein Array **. – Olaf

Antwort

1

Ihre Anzahl Strings sind nicht lang genug:

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

Ein String besteht aus einer Folge von Zeichen plus ein Nullabschluss Byte. So benötigt sogar eine einzelne Ziffer eine Array-Größe von mindestens 2. Wenn Sie sprintf verwenden, um die Textdarstellung einer Zahl in eines dieser Arrays zu schreiben, schreiben Sie über das Ende des Arrays hinaus. Dies ruft undefined behavior auf.

Diese Arrays müssen groß genug sein, um den von Ihnen übergebenen Wert aufzunehmen, einschließlich eines Negationszeichens, falls erforderlich.

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