473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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
4 1969
gpraghuram
1,275 Expert 1GB
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
canteyn
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
canteyn
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
1,275 Expert 1GB
Hi,
I ran the code in cygwin using g++.
Which OS/compiler r u using?

Thanks
Raghuram
Apr 25 '07 #5

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

Similar topics

16
by: Ian Davies | last post by:
Hello Needing help with a suitable solution. I have extracted records into a table under three columns 'category', 'comment' and share (the category column also holds the index no of the record...
0
by: antique | last post by:
iam supposed to CREATE A FUNCTION INT ADJUSTEDSUM (INT,INT) 1. find the sum of the array of size 100 2. find the number of occurrence of a MIN and MAX number if its twice or more only use 1...
7
sammyboy78
by: sammyboy78 | last post by:
I was wondering about the format of calling methods using arrays. I know that to call a method when not using arrays it would be formatted in the first class like: public void methodName(...
2
by: =?Utf-8?B?d2lubGlu?= | last post by:
Hello 1) I agree that useing classes is the best way to use VB.net. The question is how do you write a VB program without using classes? 2) For my own edification where can I see an actual...
3
Blackout
by: Blackout | last post by:
Hi, I'm new to C and need to make a program that displays the output of the first six powers of 2 like this: 1 2 4 8 16 32 (2 to the power of 0, 2 to the power of 1, 2 to...
16
by: mike3 | last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms- windows.programmer.win32 since there's Windows material in here as well as questions related to standard C++. Not sure how that'd go over...
2
by: theblissfulwizard | last post by:
FYI - New to ASP I'm trying to get multiple values from checkboxes inserted into a table using arrays. I'm using VBscript - ASP 1.1 (not 2.0) and here is what I want to do. I have a page that...
1
by: James Brown | last post by:
Hi, I'm fairly new to VBA and was looking to start using arrays to calculate medians of monte carlo simulations. I know how to set up an array as I have been reading through such websites as: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.