Check out the Pastebin Gadgets Shop. We have thousands of fun, geeky & affordable gadgets on sale :-)Want more features on Pastebin? Sign Up, it's FREE!
tweet

# Program3

By: a guest on Dec 7th, 2010  |  syntax: C  |  size: 3.23 KB  |  views: 51  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. /*
2.
3.  *  prog3A.c
4.
5.  *
6.
7.  *      Calculates the integral of cos^2(x).
8.
9.  *
10.
11.  *  Input command line argument of upper limit between 0 and Pi/2.
12.
13.  *  Lower limit is set by default to 0.
14.
15.  */
16.
17.
18.
19. #include <stdio.h>
20.
21. #include <stdlib.h>
22.
23. #include <math.h>
24.
25. #define NODATA "There was not one argument entered. Exiting...\n"
26.
27. #define INVALID "The data entered was not valid. Entered value must be between 0 and Pi/2\n"
28.
29. #define LOWERLIMIT 0
30.
31. #define MAXLIMIT M_PI/2
32.
33. #define FILENAME "px270prog3a.dat"
34.
35. #define RESULT "The integral of (Cos(x))^2 between %lf and %lf is %lf\n"
36.
37. #define DEBUG 0
38.
39.
40.
41. void integrate(double *xvalues, double *fvalues, int count, double low , double high, double *integral);
42.
43.
44.
45. int main(int argc, char* argv[]) {
46.
47.         /*Values*/
48.
49.         double a,b,value,dummy;
50.
51.         double *xvalues, *fvalues,integral;
52.
53.         /*Counts*/
54.
55.         int size=0,count=0,entrycount=0;
56.
57.         /*Flags*/
58.
59.         int rightnumber=0,validInput;
60.
61.         /*File*/
62.
63.         FILE* input;
64.
65.
66.
67.         /*Set Lower Limit*/
68.
69.         a = LOWERLIMIT;
70.
71.         /*Check and then read command line arguments */
72.
73.         if ((argc < 2)||(argc > 2)){
74.
75.                 printf(NODATA);
76.
77.                 exit(EXIT_FAILURE);
78.
79.         }
80.
81.         else {
82.
83.                 sscanf(argv[1],"%lf", &b);
84.
85.         }
86.
87.         /*Test for valid input. If not valid, exit*/
88.
89.         validInput = ((b > LOWERLIMIT) && (b < MAXLIMIT) && (b != EOF));
90.
91.         if(!validInput){
92.
93.                 printf(INVALID);
94.
95.                 exit(EXIT_FAILURE);
96.
97.         }
98.
99.         /*Open file, read data from file*/
100.
101.         input = fopen(FILENAME,"r");
102.
103.         /*Scan through the file once, to help work out size of array needed*/
104.
105.         while (rightnumber == 0){
106.
107.                 fscanf(input," %lf ",&value);
108.
109.                 fscanf(input," %lf ",&dummy);
110.
111.                 count++;
112.                 printf("Count = %d \n",count);
113.
114.                 /*If the value read for x is greater than the upper limit, stop counting data*/
115.
116.                 if (value >= b) {
117.
118.                         rightnumber = 1;
119.
120.                         rewind(input);
121.
122.                 }
123.
124.         }
125.
126.         /*Set the size of the arrays*/
127.
128.         size = (count+1)*(sizeof(double));
129.
130.         xvalues = (double*)malloc(size);
131.
132.         fvalues = (double*)malloc(size);
133.
134.         /*Use fscanf to fill arrays with values*/
135.
136.         while (entrycount<=count) {
137.
138.                 fscanf(input,"%lf",&xvalues[entrycount]);
139.
140.                 fscanf(input,"%lf",&fvalues[entrycount]);
141.
142.                 entrycount++;
143.                 //printf("Entrycount = %lf, Xvalues = %lf, FXVALues = %lf \n",entrycount,xvalues[entrycount],fvalues[entrycount]);
144.
145.         }
146.
147.         fclose(input);
148.
149.         /*Call integrate function, using pointer for integral, print result*/
150.
151.         integrate(xvalues,fvalues,count+1,a,b,&integral);
152.
153.         printf(RESULT,a,b,integral);
154.
155.
156.
157.         return 0;
158.
159. }
160.
161.
162.
163. void integrate(double *xvalues, double *fvalues, int length, double lowlim , double uplim, double *integral){
164.
165.         double workinginteg;
166.
167.         int count=0;
168.
169.         int firstpoint=0;
170.
171.
172.
173.         while (count<length) {
174.
175.                 /*Check if values within integration limits*/
176.
177.                 if (xvalues[count] <= uplim){
178.
179.                         if (xvalues[count] >= lowlim) {
180.
181.                                 if (firstpoint == 0) {
182.
183.                                         /*If the lower limit is not zero, add in the first part. If it is, this part will be zero anyway*/
184.
185.                                         if (count != 0) *integral += (xvalues[count]-lowlim)*(fvalues[count-1]);
186.
187.                                         firstpoint = 1;
188.
189.                                 }
190.
191.                                 else {
192.
193.                                         /*Add the next part of the integral*/
194.
195.                                         *integral += (xvalues[count]-xvalues[count-1])*(fvalues[count-1]);
196.
197.                                 }
198.
199.                         }
200.
201.                 }
202.
203.                 count++;
204.
205.         }
206.
207. }
clone this paste RAW Paste Data
Top