为什么我的2D数组打印不正确?(使用fprintf)

问题描述 投票:0回答:1

我有一个2D VLA,包含X行和6列。

这个SaveInfo函数将每行的值存储到每一列中,并将函数打印到文本文件中。

除了最后一列 "已付利息 "之外,所有的内容都能正确打印出来。

是什么原因导致这个问题?会不会是我给printArray释放内存的地方?目前,它在我的主函数

下面是main中的数组声明。

  double **printArray = malloc(arrSize * sizeof *printArray);
  for(int i = 0; i < arrSize; i++) {
    printArray[i] = malloc(arrSize * sizeof *printArray[i]);
  }
void SaveInfo(double interestRate, int duration, double principalAmt, double monthlyPayment, double** printArray) {
  double remainingBalance = principalAmt;
  double monthlyPrinciple;
  double totalPaid = 0;
  double interestPaid;
  double monthlyInterest;

  //open file in write mode
  FILE *p;
  p = fopen("outputFile.txt", "w");

  //With every month as a row, assign variable values to column in current row (month)
  for(int i = 0; i < duration * 12; i++) {

    printArray[i][0] = i + 1;

    monthlyInterest = (interestRate/12) * remainingBalance;
    printArray[i][1] = monthlyInterest;

    monthlyPrinciple = monthlyPayment - monthlyInterest;
    printArray[i][2] = monthlyPrinciple;

    remainingBalance = remainingBalance - monthlyPrinciple;
    printArray[i][3] = remainingBalance;

    totalPaid = totalPaid + monthlyPayment;
    printArray[i][4] = totalPaid;

    interestPaid = interestPaid + monthlyInterest;
    printArray[i][5] = interestPaid;

  }

  //Print printArray using nested loop
  fprintf(p, "%-7s %-14s %-14s %-14s %-14s %-14s", "Month", "Interest", "Principal", "Balance", "Total Paid", "Interest Paid");
  fprintf(p, "\n");
  for(int i = 0; i < duration * 12; i++) {
    for(int j = 0; j < 6; j++) {
      if(j == 0) {
        fprintf(p, "| %-3.0lf | ", printArray[i][j]);
      }
      else {
        fprintf(p, "| %10.2lf | ", printArray[i][j]);
      }
    }
    fprintf(p, "\n");
  }
}

Month   Interest       Principal      Balance        Total Paid     Interest Paid 
| 1   | |       4.17 | |      81.44 | |     918.56 | |      85.61 | |      -1.#R | 
| 2   | |       3.83 | |      81.78 | |     836.78 | |     171.21 | |      -1.#R | 
| 3   | |       3.49 | |      82.12 | |     754.66 | |     256.82 | |      -1.#R | 
| 4   | |       3.14 | |      82.46 | |     672.20 | |     342.43 | |      -1.#R | 
| 5   | |       2.80 | |      82.81 | |     589.39 | |     428.04 | |      -1.#R | 
| 6   | |       2.46 | |      83.15 | |     506.24 | |     513.64 | |      -1.#R | 
| 7   | |       2.11 | |      83.50 | |     422.74 | |     599.25 | |      -1.#R | 
| 8   | |       1.76 | |      83.85 | |     338.89 | |     684.86 | |      -1.#R | 
| 9   | |       1.41 | |      84.20 | |     254.70 | |     770.47 | |      -1.#R | 
| 10  | |       1.06 | |      84.55 | |     170.15 | |     856.07 | |      -1.#R | 
| 11  | |       0.71 | |      84.90 | |      85.25 | |     941.68 | |      -1.#R | 
| 12  | |       0.36 | |      85.25 | |      -0.00 | |    1027.29 | |      -1.#R | 

c arrays printf 2d
1个回答
3
投票

OP代码使用 interestPaid 未初始化。随着 double interestPaid;, interestPaid宣称但有一个 未定 值。代码继续尝试使用这个不确定的值(导致未定义的行为)。

interestPaid = interestPaid + monthlyInterest;

要解决这个问题,用以下方法初始化:

double interestPaid = 0;
© www.soinside.com 2019 - 2024. All rights reserved.