View Full Version : Discussion Need C programming help...
tommyeflight89
Oct 14, 2007, 02:45 PM
Hey,
I need some help writing a C program for school. I need to read a bunch of data from a .data file. The file contains data about E. coli levels in water at different beaches.
Lake identifier,Beach Identifier, #of samples, levels....... (organisms/100ml)
Here is the layout of the file:
2 202 5 33.3 37.6 37.2 37.2 37.4
2 201 9 44.1 44.9 44.2 54.5 54.6 54.1 54.2 54.4 43.3
For example first line is lake 2, beach 202, has 5 samples, 37.6/100ml etc...
We have figured out how to display a line from the file, but it involves assigning variables to every single number in the program-for every line.
We want to go through the file line by line, and display whether the beach should be closed or opened based on the average organism level.
We need a more efficient method, and it needs to work with .data files that have more lines etc.
Thanks,
Tommy
HFG
Oct 14, 2007, 04:24 PM
Haha I am from Ryerson too (and I am still here 6-7 years later) and i remember that lab. Sorry to say but your doing yourself a disservice by not figuring out how to do it yourself. You don't learn anything from easy answers. Just consider yourself lucky you don't have labs in the old T building.
pmackenzie
Oct 14, 2007, 04:30 PM
It has been a while since I did C, but that looks like a good application for a linked list.
Basically a list that points to the next member in the list.
Allocate memory as you add elements to the list.
It should all be in the K&R book, but Google will also find you some details.
http://www.cee.hw.ac.uk/~rjp/Coursewww/Cwww/linklist.html
Pat MacKenzie
Richard Ingram
Oct 14, 2007, 05:56 PM
When I went to college there was no internet and we had to do it the old fashion way. Man how times have changed. Come on now, you can figure out how to code that problem.
:)
pmackenzie
Oct 14, 2007, 06:12 PM
When I went to college there was no internet and we had to do it the old fashion way. Man how times have changed. Come on now, you can figure out how to code that problem.
:)
Often the "old fashion way" was to find someone in class who knew the answer and bug him for it :p.
Pat MacKenzie
vintage1
Oct 14, 2007, 07:50 PM
FILE *fp;
char sample_data [256];
int lake, beach, samples;
float sample, sample sum;
int i;
char *p; /* a pointer into the line string; */
fp=fopen(filename);
while(fscanf(fp, "% d %d %d %s", &lake, &beach, &samples, sample_data))
{
printf ("lake is no %d, beach is no %d ",lake, beach);
for (i=0, sample_sum=0;i<samples, p=sample_data;i++)
{
sscanf(p, "%f ",&sample);
sample_sum+=sample;
while( *p!=' ') p++;
}
sample=sample_sum/ (float)samples;
print("average organism level is %2.2f/100l\n",sample);
}
fclose(fp);
/* syntax and actual variable order not guaranteed: the exercise is to add in comments so you understand why this code does what you want and debug it till it DOES*/
tommyeflight89
Oct 14, 2007, 08:21 PM
Thanks for the responses guys. I can code the program, I just did not know how to get data from the file line by line. We have not been taught it, my textbook only has an example for a 1 line data file, and my prof is zero help. I understand this stuff pretty well, I just didn't know how to collect all the data from the file.
Vintage1: Thanks for the code. I'll go through it and figure everything out.
vintage1
Oct 15, 2007, 07:00 AM
Thanks for the responses guys. I can code the program, I just did not know how to get data from the file line by line. We have not been taught it, my textbook only has an example for a 1 line data file, and my prof is zero help. I understand this stuff pretty well, I just didn't know how to collect all the data from the file.
Vintage1: Thanks for the code. I'll go through it and figure everything out.
fegts() is th generic read a line at a time.
My fscanf alspo works, but IIRC it needs the \n in the format statement.
Theres one bug fixed..
MatC
Oct 15, 2007, 07:51 AM
Apart from the fact that you want other people to do your homework, this is just too far off topic. This is an rc (usually) electronics (always) forum.
tommyeflight89
Oct 15, 2007, 11:33 AM
Guys, I do not want other people to do my homework! I simply asked how to get data line by line. I have not been taught anything like this, nor is it in my textbook. The most I have seen is how to get data from a program with 3 single float pt #'s in it by definging each variable. I have not been taught Strings either.
ie
12.3 7.4
3.2
Give me a break MatC
I made this (http://www.rcgroups.com/forums/showthread.php?t=548239) post last summer and got alot of help. Sure, it had no RC application (it was to test medical electrodes), but people still gave me good advice. I completed the project for my employer and they were happy. If you want MatC, I can join another forum and ask questions there so I don't mess this place up with all my useless off topic posts and threads.
HFG
Oct 15, 2007, 12:17 PM
Sorry but this is really gets on my nerve, as i still get this in the classes i TA for 4th year students. The textbook and lectures ARE NOT the only source of information. They are a starting place, you will have to learn about 99% of what you need to know on your own. When you graduate and find a job how do you think you will solve problems?? The point of school is to learn how to learn not learn everything you need to know to be an engineer.
vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.