Mohd Naved
Mohd Naved

Reputation: 378

2D array working inside the loop but not working outside the loop

I have coded a formula to read the txt file, store it in array(1D) and then reading the array to calculate the moving average(2D). Program ask the user to input two values (k1 & k2) and calculate the moving average for every value from k1 to k2 (basically to find out the best value) Following is the code

#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]);
  }
}
}

Now when I am trying to print mag[k][i] values outside the for loop, it is showing error 'mag' undeclared. But when I am putting the print command inside the loop (comment out portion in the code), it works fine.

UPDATED CODE AFTER FOLLOWING COMMENTS (STILL NOT WORKING THOUGH)

#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]);
     }
   }
}

Upvotes: 2

Views: 64

Answers (1)

Jabberwocky
Jabberwocky

Reputation: 50775

The problem boils down to this: the scope of mag is limited to the inside of the for loop:

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

You need to declare mag outside the for loop for example like this:

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

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

Beware: there are other problems within your code, mentioned in the comments.

Upvotes: 2

Related Questions