2D数组在循环内工作,但不在循环外工作

时间:2018-12-20 08:22:29

标签: c arrays multidimensional-array

我已经编写了一个公式来读取txt文件,将其存储在array(1D)中,然后读取该数组以计算移动平均值(2D)。程序要求用户输入两个值(k1k2)并计算从k1k2的每个值的移动平均值(基本上是找出最佳值) 以下是代码

#include<stdlib.h>
#define MAX_FILE_NAME 100
#define MAXCHAR 1000
int main()
{
  FILE *fp;
  int count = 0,k1=0,k2=0,k=0; // Line counter (result)
  int buy[k2][count],sell[k2][count];

  char filename[MAX_FILE_NAME];
  char c; // To store a character read from file

  // Get file name from user. The file should be
  // either in current folder or complete path should be provided
  printf("Enter file name or full path: ");
  scanf("%s", filename);
  printf("Enter the minimum rolling period for calculation : \n");
  scanf("%d", &k1);
  printf("Enter the maximum rolling period for calculation : \n");
  scanf("%d", &k2);
  // Open the file

  fp = fopen(filename, "r");

  // Check if file exists
  if (fp == NULL)
  {
    printf("Could not open file %s", filename);
    return 0;
  }

  // Extract characters from file and store in character c
  for (c = getc(fp); c != EOF; c = getc(fp))
    if (c == '\n') // Increment count if this character is newline
      count = count + 1;

  // Close the file
  fclose(fp);
  //printf("The file %s has %d lines\n", filename, count);

  FILE *myFile;
  myFile = fopen(filename, "r");

  //read file into array
  float numberArray[count];
  int i;

  if (myFile == NULL){
    printf("Error Reading File\n");
    exit (0);
  }
  for (i = 0; i < count; i++){
    fscanf(myFile, "%f,", &numberArray[i]);
  }

  fclose(myFile);

  for (k=k1;k<=k2;k++)
  {
    float n;
    float data[count],mag[k2][count];
    double avg,sum;
    for (i=0;i<k-1;i++)
    {
      mag[k][i-1]=0;
      sum=sum+numberArray[i];
    }

    for(i=k-1;i<=count;i++)
    {
      mag[k][i-1]=avg;
      sum=sum+numberArray[i]-numberArray[i-k];
      avg = sum/k;
    }

    //    for(i=0;i<=count;i++)
    //    {
    //        printf("MA[%d][%d] = %0.2lf     ",k,i,mag[k][i]);
    //        if (i%3==0)
    //            printf("\n");
    //    }
  }

  for(k=k1;k<=k2;k++)
  {
    for(i=0;i<=count;i++)
      printf("MA[%d][%d] = %0.2lf     ",k,i,mag[k][i]);
  }
}
}

现在,当我尝试在for循环外打印mag[k][i]值时,它显示未声明错误'mag'。但是,当我将print命令放入循环内时(代码中的注释部分),它可以正常工作。

评论后更新代码(仍然无法正常工作)

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>

#define MAX_FILE_NAME 100
#define MAXCHAR 1000

int main()
{
  FILE *fp;
  int count,k1,k2,k; // Line counter (result)
  char filename[MAX_FILE_NAME];
  char c; // To store a character read from file

  // Get file name from user. The file should be
  // either in current folder or complete path should be provided
  printf("Enter file name or full path: ");
  scanf("%s", filename);

  printf("Enter the minimum rolling period for calculation : \n");
  scanf("%d", &k1);

  printf("Enter the maximum rolling period for calculation : \n");
  scanf("%d", &k2);

  // Open the file
  fp = fopen(filename, "r");
  // Check if file exists
  if (fp == NULL)
  {
    printf("Could not open file %s", filename);
    return 0;
  }

  // Extract characters from file and store in character c
  for (c = getc(fp); c != EOF; c = getc(fp))
    if (c == '\n') // Increment count if this character is newline
      count = count + 1;

  // Close the file
  fclose(fp);
  //printf("The file %s has %d lines\n", filename, count);

  /****************
    File opening and reading section
   *****************************************************/
  FILE *myFile;
  myFile = fopen(filename, "r");

  //read file into array
  float numberArray[count];
  int i;

  if (myFile == NULL){
    printf("Error Reading File\n");
    exit (0);
  }
  for (i = 0; i < count; i++){
    fscanf(myFile, "%f,", &numberArray[i] );
  }
  fclose(myFile);

  /***********************************************
   Calculation of Moving Average and storing it in array
   ******************************************/
  int buy[k2][count],sell[k2][count];
  float mag[k2][count];
  for (k=k1;k<=k2;k++)
  {
    float data[count];
    double avg,sum;
    for (i=1;i<k;i++)
    {
      mag[k][i-1]=0;
      sum=sum+numberArray[i];
    }

    for (i=k-1;i<=count;i++)
    {
      mag[k][i-1]=avg;
      sum=sum+numberArray[i]-numberArray[i-k];
      avg = sum/k;
    }

//    for(i=0;i<=count;i++)
//    {
//        printf("MA[%d][%d] = %0.2lf     ",k,i,mag[k][i]);
//        if (i%3==0)
//            printf("\n");
//    }
   }

   for (k=k1;k<=k2;k++)
   {
     for (i=0;i<=count;i++)
     {
       printf("MA[%d][%d] = %0.2lf     ",k,i,mag[k][i]);
     }
   }
}

1 个答案:

答案 0 :(得分:2)

问题归结为:mag的范围仅限于for循环的内部:

  for (k = k1; k <= k2; k++)
  {
    ...
    int mag[k2][count];
    ... 
  }

  // mag is out of scope here
  // therefore following line won't compile:

  printf("%d", mag[0][0]);

您需要在for循环外声明mag,例如:

  int mag[k2][count];      
  for (k = k1; k <= k2; k++)
  {
    ...
  }

  printf("%d", mag[0][0]);
  ...

当心:注释中提到了您的代码中还有其他问题。