By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,888 Members | 1,500 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,888 IT Pros & Developers. It's quick & easy.

Problem(s) with my program ( involves using arrays)

P: 22
Ok here is my problem. I am coding a program that uses 3 different functions, and the end result is that the program reads in (x,y) points from a file, and then outputs those points to another file, and then calculates the area of the points(which form a polygon). I am able to compile the program just fine, but every time I try and run the program I just get a "segmentation fault". It may be in regards to my arrays, in fact I am almost sure that is what the problem is, however I could be completely wrong.

Here is what my input file looks like:
4 0
4 7.5
7 7.5
7 3
9 0
7 0
4 0

The end result in my output file should look like this:

x y
-----------
4 0
4 7.5
7 7.5
7 3
9 0
7 0
4 0
======
Area: 25.5 square units

And now finally here is the code

Expand|Select|Wrap|Line Numbers
  1. /* 
  2.     File Name: lab6.c
  3.     Author:  Nick Cantey
  4.     Description:  This program computes the area of a polygon with at most 20
  5.                   points (includes counting the starting point twice).
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <math.h>
  10.  
  11. int get_corners(FILE*, double*, double*);
  12. void output_corners(FILE*, double*, double*, int);
  13. double polygon_area(double*, double*, int);
  14.  
  15. int main()
  16. {
  17. double area;
  18. int n; /* variable for the number of points */
  19. double xArray[20]; /* variable for the array that holds the x values */
  20. double yArray[20]; /* variable for teh array that holds the y values */
  21. FILE* in; /* allows the program to open outside files */ 
  22. FILE* out; /* allows the program to send information to outside files */
  23.  
  24. /* Opens and closes the input file and calls the get_corners function */
  25. in = fopen("lab6.in", "r");
  26. n = get_corners(in, xArray, yArray);
  27. fclose(in);
  28.  
  29. /* Opens the output file and calls output_corners and polygon_area */
  30. out = fopen("lab6.out", "w");
  31. output_corners(out, xArray, yArray, n);
  32. area = polygon_area( xArray, yArray, n);
  33. printf("=========\n");
  34. printf("Area:  %lf square units", area);
  35. /* Closes the output file */
  36. fclose(out);
  37.  
  38.  
  39. }
  40.  
  41.  
  42. /*
  43.     Function Name:  get_corners
  44.     Parameters:  in, xArray, yArray
  45.     Description:  This function fills the arrays with the point information   
  46. */
  47.  
  48. int get_corners(FILE* in, double* xArray, double* yArray)
  49.  
  50. {
  51. int number, i;
  52.  
  53. /* Starts the for loop and reads the points into their respected arrays */
  54. while(fscanf(in, "%lf%lf", &xArray[i], &yArray[i]) !=EOF)
  55.     {
  56.       i++;  
  57.     }
  58. number = i;
  59.  
  60. return(number); 
  61. }
  62.  
  63. /*
  64.     Function Name:  output_corners
  65.     Parameters:  out, xArray, yArray, n
  66.     Description:  Outputs the x,y coordinates in a table to lab6.out file
  67. */
  68.  
  69. void output_corners(FILE *out, double *xArray, double *yArray, int n)
  70.  
  71. {
  72. int i; 
  73.  
  74. printf("x     y\n---------\n");
  75.  
  76. for(i = 0; i <= n; i++)
  77.      {
  78.        printf("%lf      %lf\n", &xArray[i], &yArray[i]);
  79.      }
  80. }
  81.  
  82. /*
  83.      Function Name:  polygon_area
  84.      Parameters:  xArray, yArray, n
  85.      Description:  Calculates the area of the polygon and returns it
  86. */
  87.  
  88. double polygon_area(double* xArray, double* yArray, int n)
  89.  
  90. {
  91. double area;
  92. int i;
  93.  
  94. for(i=0; i <= n; i++)
  95.      {
  96.        area = (yArray[i]*i+1-yArray[i])*(xArray[i]*i+1+xArray[i]);
  97.      }
  98. area = area*0.5;
  99. return(area);
  100. }
  101.  
I have spent all day today trying to fix this program, and I think I have just gotten myself into a loop, and I am unable to recover. PLEASE HELP :D
Apr 17 '07 #1
Share this Question
Share on Google+
4 Replies


gpraghuram
Expert 100+
P: 1,275
HI,
I tried your code in my cygwin and compiled using gcc.
It ran successfully.
I have made quiet a few changes.
Expand|Select|Wrap|Line Numbers
  1. /* 
  2.     File Name: lab6.c
  3.     Author:  Nick Cantey
  4.     Description:  This program computes the area of a polygon with at most 20
  5.                   points (includes counting the starting point twice).
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <math.h>
  10.  
  11. int get_corners(FILE*, double*, double*);
  12. void output_corners(FILE*, double*, double*, int);
  13. double polygon_area(double*, double*, int);
  14.  
  15. int main()
  16. {
  17. double area;
  18. int n; /* variable for the number of points */
  19. double xArray[20]; /* variable for the array that holds the x values */
  20. double yArray[20]; /* variable for teh array that holds the y values */
  21. FILE* in; /* allows the program to open outside files */ 
  22. FILE* out; /* allows the program to send information to outside files */
  23.  
  24. /* Opens and closes the input file and calls the get_corners function */
  25. in = fopen("lab6.in", "r");
  26. n = get_corners(in, xArray, yArray);
  27. fclose(in);
  28.  
  29. /* Opens the output file and calls output_corners and polygon_area */
  30. out = fopen("lab6.out", "w");
  31. output_corners(out, xArray, yArray, n);
  32. area = polygon_area( xArray, yArray, n);
  33. printf("=========\n");
  34. printf("Area:  %lf square units\n", area);
  35. /* Closes the output file */
  36. fclose(out);
  37.  
  38.  
  39. }
  40.  
  41.  
  42. /*
  43.     Function Name:  get_corners
  44.     Parameters:  in, xArray, yArray
  45.     Description:  This function fills the arrays with the point information   
  46. */
  47.  
  48. int get_corners(FILE* in, double* xArray, double* yArray)
  49.  
  50. {
  51. int number, i;
  52.  
  53. /* Starts the for loop and reads the points into their respected arrays */
  54. while(fscanf(in, "%lf%lf", &xArray[i], &yArray[i]) !=EOF)
  55.     {
  56.       i++;  
  57.     }
  58. number = i;
  59.  
  60. return(number); 
  61. }
  62.  
  63. /*
  64.     Function Name:  output_corners
  65.     Parameters:  out, xArray, yArray, n
  66.     Description:  Outputs the x,y coordinates in a table to lab6.out file
  67. */
  68.  
  69. void output_corners(FILE *out, double *xArray, double *yArray, int n)
  70.  
  71. {
  72. int i; 
  73.  
  74. fprintf(out,"x     y\n---------\n");
  75.  
  76.      for(i = 0; i < n; i++)
  77.      {
  78.        fprintf(out,"%lf      %lf\n", xArray[i], yArray[i]);
  79.      }
  80. }
  81.  
  82. /*
  83.      Function Name:  polygon_area
  84.      Parameters:  xArray, yArray, n
  85.      Description:  Calculates the area of the polygon and returns it
  86. */
  87.  
  88. double polygon_area(double* xArray, double* yArray, int n)
  89.  
  90. {
  91. double area;
  92. int i;
  93.  
  94.      for(i=0; i < n; i++)
  95.      {
  96.        area += (yArray[i]*i+1-yArray[i])*(xArray[i]*i+1+xArray[i]);
  97.      }
  98. area = area*0.5;
  99. return(area);
  100. }
  101.  
Thanks
Raghuram
Apr 17 '07 #2

P: 22
thanks for looking into my problem, by chance do you think you could help me out and let me know what changes you made and where so I may be able to see my mistakes and make the proper corrections. thanks again!
Apr 17 '07 #3

P: 22
disregard that one post ......
however I did try your code that you repost with the corrections and it still is giving me a segmentation fault :/ .... any ideas why that may be.
Apr 17 '07 #4

gpraghuram
Expert 100+
P: 1,275
Hi,
I ran the code in cygwin using g++.
Which OS/compiler r u using?

Thanks
Raghuram
Apr 25 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.