| bigman40 (24) | |
|
Hey every body once again here for some help, not as much as help as before, lol and thanks again for help last time it really taught me a lot... I have written a program that computes the mean and standard deviation of a data set collected from a .txt file. Its supposed to output the Mean of data, standard deviation (if 30 or more data points) or sample standard deviation (if fewer than 30 data points) of data. I thought I had it pretty good, It finds the file appears to read the file. knows when it has 30< or 30> but doesn't give me the correct numbers. I have been starring at this program and cant for the life of me see what is wrong with it. It also crashes now, smh. I now its a small thing that my eyes for some reason is not picking up. A point in the right direction would be greatly appreciated. here is the program: #include<stdio.h> #include<stdlib.h> #include<math.h> #define file_name "my_data.txt" /************************** MESSAGE FUNCTION ****************************/ void msg1(void) { printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" ); printf("\n EE 150 \n" ); printf("\n Here you should put a message that the user"); printf("\n can read to know what the program is about" ); printf("\n and what to do to use it. \n\n\n\n\n\n\n\n" ); } /* SOMETIMES YOU WILL PUT OTHER FUNCTIONS HERE */ /*************************** MAIN FUNCTION *******************************/ int main(void) { /********************* DECLARATION OF VARIABLES **************************/ double num=0, sum=0, mean, sdev; int SIZE, k, n, num_data_pts; FILE *sensor; sensor=fopen (file_name,"r"); /*******************THE EXECUTABLE PART OF THE PROGRAM ********************/ /******* Print the Opening Message *******/ msg1(); /******* Computate ************/ if (sensor==NULL) printf("Error opening input file\n\n"); num_data_pts = fscanf(sensor, "%d", &num_data_pts); for (k=1; k<=num_data_pts; k++) { fscanf(sensor,"%lf", &num); printf("entered number %d is %lf\n",n , num); sum +=num; mean = sum/num_data_pts; printf("\n The mean of the data set is %lf", mean); if(num_data_pts >=30) { while (n != num_data_pts ) { n = n+1; sum = ((num-mean)*(num-mean)+sum); sdev = sqrt(sum/num_data_pts); printf("\n population standard deviation was used. \n\n"); printf("\n\n\n****The sdev of the data set is %lf****\n\n", sdev); } } else { while (n != num_data_pts ) { n = n+1; sum = ((num-mean)*(num-mean)+sum); sdev = sqrt(sum/(num_data_pts-1)); printf("\n Sample standard deviation was used. \n\n"); printf("\n\n\n****The sdev of the data set is %lf %s****\n\n", sdev); } } } return 0; | |
|
|
|
| bigman40 (24) | |
| There has to be some body. : ) | |
|
|
|
| guestgulkan (2831) | |||
|
OK - I'll get the ball rolling.. This starts to go wrong almost from the start unfortunately:
That line should just be: fscanf(sensor, "%d", &num_data_pts);
| |||
|
Last edited on
|
|||
| bigman40 (24) | |
|
Thanks for the reply and the info. I made the corrections suggested by guestgulkan. this is what it looks like now. [code]if (sensor==NULL) { printf("Error opening input file\n\n"); } {code] fscanf(sensor, "%d", &num_data_pts); I actually argued about the num_data_pts = fscanf to. I lost the arguement do to my noobness. thanks for the fix. Also it does make sense now to exit the program. after finding the error. thanks for that also. still no changes, program still acts the same. its seems to be reading from a ghost txt file. cause I actually put 5 data points. 1,2,3,4,5 . and its not reading it and keeps giving me 2.0 for an answer. any other suggestions would be greatly appreciated. | |
|
Last edited on
|
|
| coder777 (2378) | |
Please use code tags: [code]Your code[/code]See: http://www.cplusplus.com/articles/z13hAqkS/ This while (n != num_data_pts ) is within the for (k=1; k<=num_data_pts; k++) loop. they run just once when k is 1. I don't see the use of it
| |
|
|
|
| TheIdeasMan (1564) | ||
|
@OP Just so you know about fscanf - read this:
fscanf returns an int which is the number of items filled. You can use this to do some error checking. If the value return is zero then the fscanf failed. You can also check to see if only some of the items were filled. Unfortunately the example doesn't show it, but it does mention it under the return value heading. Edit: My general comment is that you should read up about all the functions you use, so that you understand how they work. The reference section on this site is a really good resource - you can get at it from the link at the top left of this page. Oh, and my omnipresent request for code tags | ||
|
Last edited on
|
||
| bigman40 (24) | |||
Made some adjustments. removed the k<= num_data_pts from the for loop. also added code tags. Now reading reference for fscanf. from reading the reference for fscanf it reads the info from the stream or data you told it to read and stores its weighting for you to tell it what to do with it.
| |||
|
Last edited on
|
|||
| TheIdeasMan (1564) | |||||
I meant to make use of the return value for fscanf, to do error checking like this:
It is an important idea in programming, to be always try to think of what might go wrong, and to do tests to see whether they have gone wrong so you can do something about it. This is much better than the code failing, which is what you are trying avoid wherever possible. Another thing, even though it is only a few lines of code, I would have had functions to work out the 2 different std deviations. This would make the code a little clearer:
You would need to declare these functions before main. Did you forget to include stdio.h ? Anyway good luck with your C Programming - Hope all goes well. If you have any further problems, post the compiler output. | |||||
|
|
|||||
| TheIdeasMan (1564) | |
|
Actually, there is only one line that is different between the 2 functions, so you could have 1 simple function, with an if at the end to decide which std dev to calc. You should always try to avoid duplicating code, it sometimes means putting code into functions, but in this case it is solved by a simple if statement. | |
|
Last edited on
|
|
| bigman40 (24) | |
| thank you very much for the replies. let me soak all this in then i will get back tomorrow with what I have. | |
|
|
|
| bigman40 (24) | |||
had to re-think my program and using some of the ideas from above statements and suggestions came up with this. it works perfectly. here is the code what do you guys think.
| |||
|
|
|||
| TheIdeasMan (1564) | ||||
|
You still have two while loops that are almost identical. TheIdeasMan wrote:
These two lines are the same for each calculation:
So then you need an if statement to decide which of these to do: std=sqrt(std/count);or std=sqrt(std/(count-1));and this line is the same: printf("\nThe Sample Standard Deviation equals %f.\n\n",std,unit,u); Can you figure out how to do this? As I said earlier, you should look for code that is the same, and think of a way to avoid it. | ||||
|
|
||||
| bigman40 (24) | |
| I would like to be able to do that. it would definitely shorten the program. How would i put the two together cause i cant figure out how. I have always used 2 while loops and even 3 sometimes. | |
|
|
|
| TheIdeasMan (1564) | |
| Well, I spelled it out in my last post - give it a try. | |
|
|
|
| bigman40 (24) | |
| sure will i will get back.. | |
|
|
|