Computing.Net > Forums > Programming > data files..please help..at least t

data files..please help..at least t

Reply to Message Icon

Original Message
Name: Noe_cortez
Date: October 5, 2003 at 20:38:50 Pacific
Subject: data files..please help..at least t
OS: Microsoft Windows XP
CPU/Ram: celeron (R) cpu 2.00GHz,
Comment:

please if you know anything about C..help me out..my teacher asked me to write " three C programs that print out the Max and Min values of the data included below." and to " use data files=counter,sentina and neither counter nore sentinal"...
dada file:
1 130.5
2 140.4
3 140
4 150
5 160.9
6 170
7 180.0
8 190
9 190.6......
and that's it...if you know how to write them ..please help me...



Report Offensive Message For Removal


Response Number 1
Name: Infinite Recursion
Date: October 6, 2003 at 13:40:45 Pacific
Reply: (edit)

What have you done on this so far? Where are your errors?

Look into mathematical / numerical libraries on google.

Remember 7407!

http://www.computing.net/programming/wwwboard/forum/7407.html


Report Offensive Follow Up For Removal

Response Number 2
Name: Noe_cortez
Date: October 6, 2003 at 18:30:27 Pacific
Reply: (edit)

hey...infinite i don't have anything..i mean...i already have some thing..but it isn't nothing....which is the history blocks and the preprocessor directives. and i will chech that address you gave me..thank you bro...

noe


Report Offensive Follow Up For Removal

Response Number 3
Name: Noe_cortez
Date: October 6, 2003 at 18:40:14 Pacific
Reply: (edit)

hey IR i din't mean to tell the experts to write the three programs for me..i just need help....i don't know, help to guide me thruo the three dada files...cuz i do want to learn C programing...

Noe


Report Offensive Follow Up For Removal

Response Number 4
Name: Infinite Recursion
Date: October 7, 2003 at 11:04:11 Pacific
Reply: (edit)

Here is some code to get you started... It may or may not read numbers into an array (the logic is more important, most of the code will work, some errors were deliberately left in), then you will have to compare them using the max and min function of a mathematical library or functions that you define yourself one that you define yourself.

Being that your data file contains floats and integers you will need to accomodate that. You can store them as type string or character array...

The below example on compares two integers... to accomplish your goal you will need a while loop and a temporary swap variable.

int Maximum (int a, int b)
{
if (a > b)
return a;
else
return b;
}

-----

Here is some code to give you a start...

#include <stdio.h>
#include <stdlib.h>

#define MAXLEN 10

void main void
{
FILE *numbers;
/* Notice this array is of type char and not int, nor float */
char nums[MAXLEN];
/* Open File */
if ((numbers = fopen("MY_NUMBERS.DAT"","rb")) == NULL)
{
printf("Cannot open file.\n");
exit(EXIT_FAILURE);
}

while (fgets(nums,MAXLEN,numbers) != NULL)
{
int i;
nums[i] = hold;
printf("Retrieved %s", answer);
}

fclose(numbers);
}


--

IR


Report Offensive Follow Up For Removal

Response Number 5
Name: Noe_cortez
Date: October 7, 2003 at 21:11:36 Pacific
Reply: (edit)

thank you IR.....
see that was the help i was looking for..thank you so much....
if there is something i can do for you..i would be more than welcome to do it for you..and please..i can help you with anything..but programing...

Noe...
tahnkx


Report Offensive Follow Up For Removal


Response Number 6
Name: Infinite Recursion
Date: October 8, 2003 at 05:51:55 Pacific
Reply: (edit)

You're welcome Noe...
Good luck on the program.
I'll keep ya posted, thanks.

IR


Report Offensive Follow Up For Removal

Response Number 7
Name: Noe_cortez
Date: October 9, 2003 at 12:22:14 Pacific
Reply: (edit)

IR...i already wrote the counter and the sentinel data files structures..but i dont know if the answer they give me is right......
this is the answer counter gives me:
number of sensor readings: 9
Max : 1128176026
Min : 1124237312
and sentinel wouln't give me anything...i mean...i tell the computer to compile it and it would, but when it comes to a.out it wouldn't show nothing...like this watch:
triton% gcc sentinel.c
triton% a.out

but it would print anything..do you know why is that?


Report Offensive Follow Up For Removal

Response Number 8
Name: Infinite Recursion
Date: October 9, 2003 at 20:36:06 Pacific
Reply: (edit)

The values are probably wrong (unless you have those values within your list, they may be straight garbage values)...

Make sure that you write to the screen using cout, if you are and it still doesn't display, it is because the values are stuck in the buffer and it needs to be flushed...
You can resolve that with placing
cout << endl; after your last line of display code or using the std flush routine, I forgot the exact syntax for that though ( I think it is cout.flush() or a derivative thereof ).

IR


Report Offensive Follow Up For Removal

Response Number 9
Name: Noe_cortez
Date: October 10, 2003 at 14:07:45 Pacific
Reply: (edit)

look IR....i don't want to be bugging you bro...but i am stack now...i don't know what to do from this point..and like i said i do'nt want to bother you....and that cout << endl; isn't working...it says that i didn't declare it...anyways...bro..i don't know anything about C...i have written these three structures but with help of the book, following step by step...but like i said who knows if they are right...if you would have a chance would you check the three data files for me? i mean..i would just write the three data files and then if you have a chance maybe you can check them...i don't mean to bother you or use you..it's just that i don't have anybody else that can help me but you...they are three long programs...you know what it's ok....i will turn them in like that...i have work on them too much..just forget about this....and thank you so much for dealing with me...all right bro..take care..and once again thank you for your help...

Noe


Report Offensive Follow Up For Removal

Response Number 10
Name: Infinite Recursion
Date: October 10, 2003 at 19:24:19 Pacific
Reply: (edit)

Noe... Its cool dude, I don't feel used or bothered... I do this stuff all of the time online and face-to-face with local students... believe it or not, it is how I stay on top of the programming languages. If I don't use them, I tend to forget them... can't have dull tools in the field ya know.

Anyway, post what you have as of right now either here or on another URL and I will review the code, compile it, test the data files against it and see where your problems reside and give you more detail.

IR


Report Offensive Follow Up For Removal

Response Number 11
Name: Noe_cortez
Date: October 13, 2003 at 11:38:30 Pacific
Reply: (edit)

ok..first of all i am sorry i didn't post them before, it was just that i didnt' have them in hand.....note:i created a data file for each program..for the first one i created Sensor1.dat and sec..sensor2.dat and thi...sensor3.dat...one more thing...i try to paste them as neatly as i could
"Counter"
-----------*/
/* assignment2 */
/* this program generates a summary report from
a data file that has the number of data points
in the first record; */


#include <stdio.h>
#define FILENAME "sensor1.dat"

int main (void)
{
/* declare and initialize variables. */
int num_data_pts, k;
double time, motion, sum=0, max, min;
FILE * sensor1;

/* open file and read the number of data points. */
sensor1 = fopen (FILENAME, "r");
if (sensor1==NULL)
{
printf("error opening input file\n");
}
else
{
fscanf(sensor1,"%i",&num_data_pts);

/* read data and compute summary information. */
for (k=1; k<=num_data_pts; k++)
{
fscanf (sensor1,"%i %f",&time,&motion);
if (k == 1)
max = min = motion;
sum += motion;
if (motion > max)
max=motion;
if (motion< min)
min = motion;
}
/* print summary information. */
printf("number of sensor readings: %i \n",
num_data_pts);
printf ("Max : %i \n", max);
printf ("Min : %i \n", min);

/* close file and exit porgram. */
fclose (sensor1);

}
return 0;

}

------------*/
Sentinel:
-------------*/
/* assignment2 */
/* this program generates a summary report from */
/* a data file that has the number of data points */
/* in the first record; */


#include <stdio.h>
#define FILENAME "sensor2.dat"

int main (void)
{
/* declare and initialize variables. */
int num_data_pts=0, k;
double time, motion, sum=0, max, min;
FILE * sensor2;

/* open file and read the first data point. */
sensor2 = fopen (FILENAME, "r");
if (sensor2 == NULL)
{
printf("error opening input file\n");
}
else
{
fscanf(sensor2,"%i %f", &time,&motion);

/* initialize variables using first data point. */
max = min = motion;

/* update summary data until trailer record read. */
do
{
sum += motion;
if (motion > max)
max = motion;
if (motion < min)
min = motion;
num_data_pts++;
fscanf (sensor2,"%i %f", &time,&motion);
} while (time >= 0);

/* print summary information. */
printf("number of sensor readings: %i \n",
num_data_pts);
printf ("Max : %i \n", max);
printf ("Min : %i \n", min);

/* close file and exit porgram. */
fclose (sensor2);

}
return 0;
}
------------*/

i don't know how to call this program, but it isn't sentinel nor counter as you might already know:

--------------*/
/* assignment_2 */
/* */
/* this program generates a summary report from a data file that */
/* has the number of data points in the first record */

#include <stdio.h>
#define FILENAME "sensor3.dat"

int main (void)

{

/* Declare and initialize variables. */
int num_data_pts=0, k;
double time, motion, sum=0, max, min;
FILE *sensor3;

/* open file */
sensor3= fopen (FILENAME, "r");
if (sensor3 == NULL)
{

Printf (" Error opening input file \n");

}

else

{


/* while not at the end of the file. */
/* read and accumulate information */
while ((fscanf (sensor3, "%i %f", &time,&motion)) == 2)

{

num_data_pts++;
if (num_data_pts == 1)
max = min = motion;
sum += motion;
if (motion > max)
max = motion;
if (motion < min)
min = motion;

}

/* print summary information. */
printf ("Number of sensor readings: %i \n",
num_data_pts);
printf (" Max : %i \n", max);
printf (" Min : %i \n", min);

/* close file and exit program. */
fclose (sensor3);
}

return 0;

}

---------------------*/

chech them only if you have time please. and if you can't...then that's also ok...

Noe


Report Offensive Follow Up For Removal

Response Number 12
Name: Infinite Recursion
Date: October 14, 2003 at 06:32:25 Pacific
Reply: (edit)

I need copies of your data files as well so I have something to test it with.

IR


Report Offensive Follow Up For Removal

Response Number 13
Name: noe_cortez
Date: October 15, 2003 at 11:12:55 Pacific
Reply: (edit)

hey ir...guess what?? i already figured the first two programs out..i don't have any problems with them..now i am only having troube withe last one...so don't worry about the two first programs...just help me with the third one...and for the 3th i used the next data file:

1 130.5
2 140.4
3 140.0
4 150.0
5 160.9
6 170.0
7 180.0
8 190.0
9 190.6


take care.....oh and i save it as sensor3.dat...see ya...and thank you once again...


Report Offensive Follow Up For Removal

Response Number 14
Name: Infinite Recursion
Date: October 16, 2003 at 09:00:48 Pacific
Reply: (edit)

Below you will find the partial solution to your assignment... I left comments in the form of

// *** comment

The code generates number of entries and max correctly. I will leave the min for you to figure out.

The code:

#include <stdio.h>
#include <stdlib.h>

#define FILENAME "sensor3.dat"

int main (void)

{
/* Declare and initialize variables. */
int num_data_pts=0, k;
// **** max and min set to 0, took out ; after max and changed from double to float
float motion, sum=0, max=0, min=0;
int time; // *** used to be float
FILE *sensor3;

/* open file */
sensor3= fopen (FILENAME, "r");
if (sensor3 == NULL)
{
printf (" Error opening input file \n");
}
else
{
/* while not at the end of the file. */
/* read and accumulate information */
while ((fscanf (sensor3, "%i %f", &time,&motion)) == 2)
{
num_data_pts++;
// *** This is wrong ---> max = min = motion;
if (num_data_pts == 1)
{
// *** only one entry detected.
max = motion;
sum = motion;
}
else
{
sum += motion;
if (motion > max)
max = motion;
if (motion < min)
min = motion;
}
} // *** end first else

/* print summary information. */

// *** The number of readings is fine.... ***
printf ("Number of sensor readings: %i \n",
num_data_pts);
// *** changed from %i to %f
printf (" Max : %f \n", max);
printf (" Min : %f \n", min);

/* close file and exit program. */
fclose (sensor3);


} // *** end while

// *** to see results - temporary
float bye;
printf("See ya...");
scanf("%f",&bye);


return 0;

}



Report Offensive Follow Up For Removal

Response Number 15
Name: Noe_cortez
Date: October 17, 2003 at 00:22:10 Pacific
Reply: (edit)

ir thank yo so much..i already turn in the assingment thankx to you...i think i will get an A++..hehe..which is nice.....and it wansnt that hard to understand what you explained on the last post....anyways..take care..and if i need help again..would you help me once agaiN??? please....this teacher never stopos giving assingnments..i think it's fine but he got to come down...ok..got to go..take care bro and thankx once again...and be a good teacher....see ya.......

Noe


Report Offensive Follow Up For Removal

Response Number 16
Name: Infinite Recursion
Date: October 17, 2003 at 06:25:21 Pacific
Reply: (edit)

No problem, glad it worked out.
If you have any further questions, you can always post them here and we'll take a look at them.

IR


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: data files..please help..at least t

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge