Link to home
Start Free TrialLog in
Avatar of leonx01
leonx01

asked on

general protection exception in borland turbo C++

Hi, i am trying to read a file (contain 49 rows and 14 columns) into a 2d array and then display the array. i wrote a program and it compiled successfully. when i run it, some general gerenal protection fault happened. i dont know whats wrong, any one can help me please? (im using borland turbo C++)
here is my program:

#include <stdio.h>
#define DATA_FILE      "c:\data\q1new.txt"
void get_data(double array[14][49]);
void print_array(double array [14][49]);

void main(void)
{
 double array[14][49];

 
 get_data(array);
 print_array(array);

 
}

/*sub-program*/
/*Fills the array with data from file q1new.txt */
void get_data (double array [14] [49])
{
 int i, j;
 FILE *fp;
 fp = fopen(DATA_FILE, "r");
 for (i=0; i < 14; ++i)
     for (j=0; j < 49; ++j)
          fscanf ( fp, "%lf", &array [i][j]);
 fclose(fp);
}

/*Display contents of the array*/
void print_array(double array [14] [49])
{
 int i,j;
 for (i=0; i <  14; ++i) {
       for ( j=0; j < 49; ++j)
            printf("%f", array[i][j]);
       printf("\n");
       }
}
Avatar of frankieli_98
frankieli_98

Can you post the file as well?
Avatar of leonx01

ASKER

here it is:
-4.62    -2.82    -3.02    -1.49    -0.10     0.95     2.10     2.93     3.00     2.59     4.65     5.09     5.89     8.05
-3.99    -3.70    -2.67    -1.48    -0.11    -0.35     0.37     2.07     2.24     2.95     3.66     5.82     5.65     5.44
-5.75    -3.78    -4.91    -2.56    -2.62    -0.32    -1.36     0.73     0.95     2.59     2.75     5.13     4.67     6.87
-6.01    -5.21    -3.70    -3.11    -2.60    -1.03    -0.91    -0.15     0.43     1.23     1.88     2.48     5.12     5.22
-6.88    -6.20    -4.88    -4.12    -3.28    -1.84    -2.33    -0.70     0.25     0.15     2.06     2.30     3.04     4.75
-9.05    -6.68    -6.00    -5.86    -3.63    -3.74    -2.20    -0.24    -0.68     0.04     1.66     2.68     1.77     3.71
-9.38    -8.22    -6.12    -5.76    -4.61    -4.62    -2.91    -1.99    -2.41    -1.19     0.91    -0.22     2.38     2.60
-9.97    -8.77    -7.59    -5.85    -6.97    -5.72    -4.49    -2.92    -2.33    -1.11     0.39     0.26     1.52     3.02
-10.34   -10.20    -8.59    -8.61    -5.50    -5.55    -4.39    -4.19    -4.33    -1.67    -1.90    -0.06     0.24     0.66
-11.68   -10.06    -9.62    -9.05    -7.85    -7.24    -4.82    -4.07    -3.54    -3.86    -3.85     0.16    -0.75    -0.37
-10.46   -11.77    -8.94    -9.52    -8.00    -7.45    -6.12    -5.88    -4.28    -4.08    -2.33    -2.13    -1.58    -0.73
-12.03   -11.79   -11.77   -10.98   -10.41    -7.80    -6.61    -7.49    -5.03    -5.92    -3.54    -3.60    -2.46     0.02
-14.42   -13.07   -11.74   -10.63   -10.07   -10.44    -8.87    -9.04    -6.23    -4.87    -5.00    -4.24    -3.26    -2.60
-13.80   -13.68   -13.08   -11.43   -12.45   -10.53    -9.58    -8.51    -8.60    -6.52    -6.92    -4.18    -3.38    -4.39
-15.02   -14.92   -13.81   -12.72   -12.00   -12.08   -10.42    -9.18    -8.01    -6.06    -5.70    -4.65    -4.73    -3.42
-16.77   -14.98   -14.99   -13.44   -12.09    -9.78   -10.61    -9.75    -9.60    -8.87    -8.02    -6.66    -5.50    -4.74
-16.80   -15.48   -15.05   -13.80   -13.43   -12.25   -11.22    -9.91    -9.39    -8.55    -8.88    -7.25    -6.67    -5.54
-18.69   -16.25   -16.61   -14.75   -13.67   -12.90   -12.16   -10.70   -11.08   -10.13    -9.00    -6.45    -7.46    -5.92
-18.96   -17.51   -16.23   -16.90   -15.23   -14.71   -14.55   -12.87   -11.48   -10.08   -10.35    -9.48    -7.62    -7.25
-19.04   -18.47   -18.92   -17.45   -15.37   -14.58   -13.53   -12.80   -11.92   -10.33    -9.97    -9.28    -8.94    -8.15
-20.39   -19.89   -18.50   -17.30   -17.18   -16.19   -15.16   -14.08   -13.76   -12.54   -11.99   -10.65    -8.88    -8.80
-20.60   -20.80   -19.50   -19.07   -18.04   -16.61   -16.32   -15.82   -13.46   -13.17   -12.04   -11.77    -9.51    -8.88
-21.66   -20.33   -20.87   -19.10   -18.23   -17.21   -16.34   -15.73   -15.01   -14.61   -14.22   -12.66   -10.88   -10.88
-22.11   -20.28   -20.03   -20.85   -19.12   -17.74   -17.84   -15.97   -15.86   -15.29   -14.13   -13.73   -11.87   -10.63
-22.69   -23.74   -21.40   -21.90   -20.76   -18.61   -18.53   -17.02   -15.81   -14.71   -15.39   -15.10   -13.21   -11.71
-24.50   -23.62   -23.58   -22.09   -20.83   -19.46   -18.56   -18.37   -16.17   -16.70   -15.85   -14.66   -13.83   -14.03
-25.68   -23.21   -23.95   -23.17   -21.37   -20.83   -19.09   -19.58   -17.66   -18.18   -16.57   -16.36   -14.42   -14.73
-25.98   -25.69   -25.50   -24.59   -22.22   -21.10   -20.23   -20.27   -18.70   -17.73   -17.86   -16.40   -15.86   -14.86
-26.74   -25.49   -25.32   -24.39   -22.97   -21.67   -21.37   -20.41   -19.36   -18.50   -17.68   -16.92   -16.70   -15.16
-28.30   -25.45   -25.90   -24.97   -23.57   -22.76   -22.80   -21.36   -21.23   -19.31   -19.30   -17.00   -17.45   -17.02
-28.28   -27.10   -25.85   -25.04   -25.56   -23.86   -24.14   -23.19   -21.31   -20.29   -20.90   -19.04   -17.48   -16.48
-30.19   -29.80   -26.24   -26.49   -25.00   -24.70   -22.52   -22.14   -21.23   -21.71   -20.43   -19.85   -19.67   -18.17
-29.20   -28.69   -28.08   -28.22   -26.02   -26.82   -25.66   -23.87   -23.30   -21.65   -20.84   -20.35   -19.78   -18.26
-31.25   -30.52   -30.07   -28.65   -26.20   -26.46   -25.28   -25.73   -23.95   -22.92   -23.61   -20.41   -19.67   -18.87
-31.57   -30.03   -30.24   -29.21   -28.50   -27.85   -25.64   -26.56   -24.94   -23.39   -23.58   -22.42   -21.68   -20.28
-32.28   -32.10   -30.61   -30.05   -29.32   -27.54   -27.56   -27.63   -25.55   -25.57   -24.30   -23.52   -21.98   -21.82
-33.86   -32.87   -32.69   -31.54   -29.05   -29.41   -27.75   -27.07   -26.28   -25.72   -23.99   -23.99   -22.06   -21.83
-33.93   -33.78   -31.37   -31.36   -29.51   -28.87   -29.21   -28.02   -28.09   -26.67   -25.22   -23.86   -23.31   -23.11
-35.12   -34.42   -32.35   -31.90   -30.96   -31.08   -30.40   -28.86   -28.34   -26.82   -26.77   -25.94   -24.88   -22.51
-36.31   -36.14   -34.38   -32.63   -33.50   -31.91   -29.57   -30.94   -27.91   -28.31   -25.65   -26.54   -25.52   -25.35
-36.56   -35.33   -34.33   -33.53   -33.55   -32.58   -31.22   -31.32   -30.71   -28.37   -27.89   -27.94   -25.82   -25.39
-37.37   -36.97   -35.49   -35.21   -34.72   -33.18   -32.83   -31.27   -30.61   -29.35   -28.53   -28.11   -27.37   -26.11
-38.41   -37.66   -35.50   -36.38   -34.92   -33.49   -33.21   -31.97   -31.16   -31.36   -29.68   -29.67   -28.70   -26.20
-39.81   -38.76   -37.83   -36.43   -35.85   -35.28   -33.82   -33.32   -32.84   -30.88   -30.76   -29.93   -28.45   -27.48
-38.81   -39.93   -36.86   -37.80   -36.72   -35.76   -35.47   -33.84   -32.89   -31.71   -32.60   -30.77   -30.01   -28.85
-42.03   -39.74   -39.02   -38.87   -37.04   -36.76   -36.79   -35.17   -33.32   -34.17   -31.71   -32.62   -30.60   -29.94
-41.75   -41.96   -39.45   -38.35   -38.14   -36.17   -36.82   -35.47   -34.42   -32.81   -32.51   -30.57   -30.52   -30.13
-42.73   -41.83   -40.43   -39.04   -38.06   -38.78   -36.65   -36.91   -35.66   -34.05   -33.48   -32.50   -32.10   -31.67
-43.55   -42.64   -42.84   -39.54   -39.67   -39.66   -38.60   -36.68   -35.14   -35.37   -34.24   -34.16   -32.94   -32.00
ASKER CERTIFIED SOLUTION
Avatar of Exceter
Exceter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of leonx01

ASKER

i tried to reverse the rows and cols, but it still give me the same error message. is there something wrong with my complier?
Strange... Both ways compile and run on my system except that the way you had it originally yielded only zeros while the code I posted above produced the correct results. Did you try cut and pasting the code I provided? After all, every instance of 49 must be changed to 14 and vice versa.

What compiler are you using?

Exceter
Avatar of leonx01

ASKER

yes, i tried cut and past. looks like there is something wrong with the C complier that im using. it is borland turbo C++ 4.5
The code runs properly for me when compiled with all of the following compilers,

Borland C/C++ Version 3.1
Borland Turbo C Version 2.01
Microsoft C/C++ Version 12.00.8168

The code shouldn't bother. Are you getting any warning messages?

Exceter
Avatar of leonx01

ASKER

after i complie it and run, a message pops up. i dont know what does it means.
General protection exception 0x247F:0x66F9
ESTING(1) 0x247F:0x66F9 processor fault
Ahhh....

Okay, check out this site.

http://infocom.cqu.edu.au/Units/win2000/85102/Mailing_List/2000_July/msg00064.html

It seems that your problem is a bug in the compiler itself. :-(

Exceter
You can download Borland's 5.5 C/C++ compiler for free at their site. They make you register, which is rather annoying, but hey, it is free.

http://www.borland.com/products/downloads/download_cbuilder.html

Exceter
Avatar of leonx01

ASKER

thanks
Avatar of ozo
fp = fopen(DATA_FILE, "r");
if( !fp ){
    printf("fopen %s failed\n",DATA_FILE);
    exit(1);
}
So, you are saying that leonx01 was using the wrong file name?

Exceter
Avatar of leonx01

ASKER

Hi, i think exceter was right. i tried the program on another computer with borland C++ 5.5, it worked. so it was my complier's problem.
Glad I could help.
Does that solve your problem or do you have more difficulties that I can help you with?

Exceter
Avatar of leonx01

ASKER

thank you, that problem was solved. one more question, i wanted to find the sum of all the elements in the array, sum_y, can i use two for loops like this:

sum_z = 0;
 for (i=0; i <= 49; ++i)
     for (j=0; j <= 14; ++j)
       sum_z = sum_z+ array[i][j];
Avatar of leonx01

ASKER

sorry, i mean sum_z
Avatar of leonx01

ASKER

please disregard the question i just posted above.
Yes, that should work. I would like to make one suggestion, however. Instead of having two sets of loops, one for reading in the data and one for getting the sum, I would recommend keeping track of the sum as the data is read in from the file. That way you have the sum already in a variable ready for use in addition to the fact that this would increase performance slightly. For example,

int sum_z = 0;

for (int i = 0; i < 49; i++ )
{
    for ( int j = 0; j < 14; j++ )
    {
        fscanf ( fp, "%lf", &array [i][j]);
        sum_z += array[i][j];
    }
}

Exceter
Avatar of leonx01

ASKER

Hi, Exceter, i have problems with my array again. i got a 2d array. array[49][14]. i need to find out the value of something like this: array[0][j]*0+array[1][j]*1+array[2][j]*2+...+ array[48][j]*48. where the value of j goes form 0 to 13. i write a code using for loop. but i keep getting 0. i think my code is wrong. would you please take a look at it? thanks! i dont have enough points to post a new message. so i have to post here.

sum_zx = 0;
for (i=0; i < 49; ++i)
     for (j=0; j < 14; ++j)
       sum_zx = sum_zx + j*array[i][j];
what datatype is sum_zx? it should be double if your array values are double.

vo1d
>> what datatype is sum_zx? it should be double if your array values are double.

It is.

Refer to this PAQ,
https://www.experts-exchange.com/questions/20448957/array-in-C.html

Exceter