Ok, Here is the problem in the book;

You are developing a database of measured meteorological data for use in weather and climate research. Define a structure type measured_data_t (which I have done) with components site_id_num (a four digit integer), wind_speed, day_of_month, and temperature. Each site measures its data daily, at noon local time. Write a program that inputs a file of measured_data_t records and determines the site with the greatest variation in temperature (defined here as teh biggest difference between extrema) and the site with the highest average wind speed for all the days in the file. You may assume that there will be at most ten sites. Test the program on the following July daily data collected over one week at three sites:

ID Day Wind Speed (knots) Temperature (deg C)
2001 10 11 30
2001 11 5 22
2001 12 18 25
2001 13 16 26
2001 14 14 26
2001 15 2 25
2001 16 14 22
3345 10 8 29
3345 11 5 23
3345 12 12 23
3345 13 14 24
3345 14 10 24
3345 15 9 22
3345 16 9 20
3819 10 17 27
3819 11 20 21
3819 12 22 21
3819 13 18 22
3819 14 15 22
3819 15 9 19
3819 16 12 18

Ok, here is a modified version of the one from when someone asked the same question on this site. What am I doing wrong as I can't get rid of the errors? (listed underneath) I'm not very good at programming as I am a retired helicopter mechanic and this class is required for my BS in Electronics Engineering Technology. Thank you for any help you guys can give me.
Respectfully,
Paul
USN Retired


#include <stdio.h>
#include <math.h>
#include <string.h>
#define Max_sites 10 /* maximum number of sites allowed */


typedef struct
{
int site_id_num[4]; /* id number of the site */
int wind_speed[3]; /* the wind speed in knots */
int day_of_month[2]; /* the day of the month written as dd */
int temperature[2]; /* temp in celcius */
}measured_data_t;

{
measured_data_t current_measured,
previous_measured,
blank_measured = {"", 0,0,0,0}
}
int
main(void)
{
/* DEFINE UNITS AND VARIABLES HERE!!!!!*/
int fscan_measured(FILE *filep, measured_data_t *measuredp);

/* Fills in input data into measured_data_t
* Integer returns as an indication of success or failure
* With 1 => successful input of one site
* and 0 => error encountered */
int;

measured_data_t *measuredp /* output - measured_data_t structure to fill */
{
int status;

status = fscanf(filep,"%d%d%d%d", measuredp->site_id_num);
measuredp->wind_speed,
measuredp->day_of_month,
measuredp->temperature;
if (status == 4)
status =1;
else if (status !=EOF)
status =0;

return (status);
}
}

/*
*Opens database file measured_data_t.dat and gets data to to place until end of file is encountered
*/

{
FILE *inp;
measured_data_t;
int i, status;

inp = fopen("measured_data_t.dat", "r");
i = 0;

for (status = fscan_measured(inp, &data);
status == 1 && i < Max_sites;
status = fscan_measured(inp, &data)) {
units[i++] = data
}
fclose(inp);

/*
* Compiles data and finds the average windspeed at each site ????????
*/

}


ERRORS Received

Error 1 error C2447: '{' : missing function header (old-style formal list?)Warning 2 warning C4091: '' : ignored on left of 'int' when no variable is declared
Error 3 error C2601: 'measuredp' : local function definitions are illegal
Error 4 error C2065: 'filep' : undeclared identifier
Error 5 error C2447: '{' : missing function header (old-style formal list?)

Why put a pair of code tag around your code?
It's very difficult to study code in detail, if it's squished over like html text.

I couldn't fix your program - it's pretty sloppy (like having comma's at the end of a line of code, instead of a semi-colon), and unfortunately, you chose to declare new variables throughout the code. That's OK for some (few) newer compilers, but mine won't put up with it. If you declare your variables at the start of the function, then I can assist you further.

Here's your program, with several syntax corrections:

#include <stdio.h>
#include <math.h>
#include <string.h>

#define Max_sites 10 /* maximum number of sites allowed */

typedef struct
{
int site_id_num[4]; /* id number of the site */
int wind_speed[3]; /* the wind speed in knots */
int day_of_month[2]; /* the day of the month written as dd */
int temperature[2]; /* temp in celcius */
}measured_data_t;

//8888888888888888888888888888{
measured_data_t current_measured,
previous_measured,
//88888888888888888888888888blank_measured = {"", 0,0,0,0}
blank_measured = { 0,0,0,0};
//88888888888888888888888888888}
int main(void)
{
/* DEFINE UNITS AND VARIABLES HERE!!!!!*/
int fscan_measured(FILE *filep, measured_data_t *measuredp);

/* Fills in input data into measured_data_t
* Integer returns as an indication of success or failure
* With 1 => successful input of one site
* and 0 => error encountered */
//888888888888888888888888888888int;

//88888888888measured_data_t *measuredp /* output - measured_data_t structure to fill */
measured_data_t *measuredp; /* output - measured_data_t structure to fill */

int status;

//888888888888888status = fscanf(filep,"%d%d%d%d", measuredp->site_id_num);
FILE *filep;
status = fscanf(filep,"%d%d%d%d", measuredp->site_id_num);
//8888888888 These next three lines of code, don't do anything
//measuredp->wind_speed;     //needed semi-colon
//measuredp->day_of_month;   //needed semi-colon
//measuredp->temperature;

if (status == 4)
status =1;
else if (status !=EOF)
status =0;

//return (status);
//}
//}

/*
*Opens database file measured_data_t.dat and gets data to to place until end of file is encountered
*/
//{
FILE *inptr;
measured_data_t;
int i, status;

inptr = fopen("measured_data_t.dat", "r");
i = 0;

for (status = fscan_measured(inptr, &data);
status == 1 && i < Max_sites;
status = fscan_measured(inptr, &data)) {
units[i++] = data
}
fclose(inptr);

/*
* Compiles data and finds the average windspeed at each site ????????
*/
  return (status);  //moved to end of program
}

There is a process for writing code that will help you alot. Write a few lines, *and then compile it and see if it's correct syntax*. You won't find logic errors this way, but you will be learning to use good C syntax, and know your code, in it's present state, is "close" to being syntactically correct, at least.

There's nothing worse than having written 20, 40, 100 lines of code, and then finding out that you have 50 errors in it -- where, well nobody knows. It's a bad feeling, I can attest, and wastes a LOT of time.

Note: I changed the FILE pointer, inp to inptr, because inp is a reserved word for my compiler (for input from a port).

Edited 6 Years Ago by Adak: n/a

[...] you chose to declare new variables throughout the code. That's OK for some (few) newer compilers, but mine won't put up with it. If you declare your variables at the start of the function, then I can assist you further.

Hmm... this could have been easier to fix w/ a C++ compiler and minor adjustments.

Anyway, what I really wanted to say was that I think there is the exact same problem in another thread.

Edited 6 Years Ago by creeps: n/a

Gotta disagree, Creeps.

He's taking a C class at University, so a C++ compiler won't be used by the prof, to grade his program.

And you can't put a comma or a misplaced return, into a C++ program, and expect it to work either.

OK, it would make it marginally easier with the declarations, but it still crashes and burns - so it needs fixing.

I realy appreciate all the help with this. I'm so lost with programming, just give me a helicopter in pieces and I'm fine.Also Creeps is right, this same program question was asked in 2009 and that's were I got some of my ideas, actually modified that one, alot, because it would not work on my compiler, which is MS Visual Studio 2008.

I'm sorry if I upset anybody by not cod tagging my question, but I don't know how. I will research it and try to fix my question.

.. but I don't know how. I will research it and try to fix my question.

The code tag instructions in the link seem to be a bit out-of-sync. Here's a version which should work as of this writing ...

Use code tags!
The easiest way to do this is to select all of your code and then click the [code] button on the message editor. This will automatically wrap the selected text in code tags.

You can also manually type in the tags, like so

[code]
... Your code here ...
[/code]

Edited 3 Years Ago by Dani: Formatting fixed

Here's my suggestion:

1) Let's get out of the damn graveyard, looking for something alive. You'll spend more time, and get far less learning done, trying to make a living program, from the bodies of the dead, than if you just wrote a your own program.

The struct part will be about the same. Shit can the rest.

We will assist.

2) Let's use (at least approximate), some good top-down design methodologies. Beats the shit out of trying to resurrect zombie programs.

3) This is the starting point:

You are developing a database of measured meteorological data for use in weather and climate research. Define a structure type measured_data_t (which I have done) with components site_id_num (a four digit integer), wind_speed, day_of_month, and temperature. Each site measures its data daily, at noon local time. Write a program that inputs a file of measured_data_t records and determines the site with the greatest variation in temperature (defined here as teh biggest difference between extrema) and the site with the highest average wind speed for all the days in the file. You may assume that there will be at most ten sites. Test the program on the following July daily data collected over one week at three sites:

ID Day Wind Speed (knots) Temperature (deg C)
2001 10 11 30
2001 11 5 22
2001 12 18 25
2001 13 16 26
2001 14 14 26
2001 15 2 25
2001 16 14 22
3345 10 8 29
3345 11 5 23
3345 12 12 23
3345 13 14 24
3345 14 10 24
3345 15 9 22
3345 16 9 20
3819 10 17 27
3819 11 20 21
3819 12 22 21
3819 13 18 22
3819 14 15 22
3819 15 9 19
3819 16 12 18

The program will:

define the structs for the records we'll be working with
create an array of those structs. Assume 10 sites max * one month (31 days) size will be adequate.

open the data file
verify success of file op

read in all the data, into the array, and:
>>counting number of records.
>>getting max wind speed
>>finds site with greatest temp range

Reports on the above findings.

It doesn't appear that an array will be necessary after all, since we can determine the above, as the records are being read, in this case.

Let's keep the array, because the data isn't going to always be this "nice", and it's very helpful to know how to work with an array of structs.

I'm naming this file: "temp.txt"

Since "unmeasured data" isn't data, "measured" data is redundant, so it's now just a struct named data1. ;) Obviously, the data doesn't agree with the struct description, so we'll move "day", up to the second member in the struct, and move "wind speed", down to the third member in the struct.

I'd like a function for: input, output, and main.

Let's start with the global stuff:

#include <stdio.h>
#define SIZE 310     //31 * 10

typedef struct
{
int site_id_num[4]; /* id number of the site */
int day_of_month[2]; /* the day of the month written as dd */
int wind_speed[3]; /* the wind speed in knots */
int temperature[2]; /* temp in celcius */
}data1;

int input(struct data1 data[SIZE], int *, *int);  

int main(void) {
  int i, count=0;
  
  count = input(data, &topwind, &toprange);
  


  return 0;
}
int input(struct data1 data[SIZE], int *topwind, int *toprange) {
  int i=0, result;

  while(1) {
    result = fscanf(fp, blah, blah, blah, blah);
    if(result < 4)
      break;
    ++i;
  }
  return i;
}

Since the data is strictly formatted, we can use fscanf(), and test it's return number, in an endless while loop, breaking out when it's done.

Paul, we need an output function for the data. Aside from essential details - put them aside, for now. Get the "bones" forming.

Edited 6 Years Ago by Adak: n/a

So from what I'm seing the results you are talking about would be in line 27? If so This is what I would use;

result = fscanf(fp,"%d%d%d%d, measuredp-> site_id_num

Sorry, but I really don't get what I'm doing. I know you guys are trying to help me, but this really is well above my head. I'm still trying though

Making an array of 310 structs (records). Reading the input from the data file, putting it into the struct, and into the array.

Right now, I'm too bushed to work on it, but later tonight or tomorrow, we'll take it to the next step.

We're designing a new program, because the other program was off the mark, rather far, imo. Also, this is what you should develop skill doing. It's one thing to add some function to a working program. It's quite another thing to resurrect the "dead" one's.

A bit of top down design - put off the non-essential details. Get the "backbone" of the programs flow, in place first. Three functions are common in any program - not always needed, but common: input, output, and main. Others for computations, if needed (not needed here).

I'll do the input function. You make the output function. The "blah blah", is just to show that what will go in there is a detail - and I'm putting off the details, for now. (etc., is common however ;) )

I took the data you posted, and put it into a file named "temps.txt" for the program to access.

If you have any questions, please ask away. I haven't checked that this program is accurate, so by all means, test it out for yourself.

#include <stdio.h>
#include <stdlib.h>  //for exit()
#define SIZE 310     //31 * 10 the size of our array of structs


/* prototype and declare a struct named "data1" */
typedef struct
{
int site_id; /* id number of the site */
int date; /* the day of the month written as dd */
int wind; /* the wind speed in knots */
int degrees; /* temp in celcius */
}data1;   


/* input function prototype */
int input(data1 *data, int *topWindSite, int *topTempSite);  

int main(void) {
  int i, count, topWindSite, topTempSite;
  char choice[3];

//make array of structs of type "data1", named data[]. 
//It will hold SIZE (310) structs
  data1 data[SIZE];  
  topWindSite = topTempSite = 0;
//call input() with data (just an address now), and two int variable addresses
//return from input() will be the number of records in the file - count.
  count = input(data, &topWindSite, &topTempSite);
  
  printf("\n\nThere are %d records", count);
  printf("\n Site with the highest average wind speed: %d", topWindSite);
  printf("\n Site with the greatest temperature range: %d", topTempSite);

  printf("\n View the records? [y/n]: ");
  fgets(choice, sizeof(choice), stdin);
  if(choice[0]=='y' || choice[0]=='Y') {
    for(i=0;i<count;i++) {
      printf("\n%4d  %2d  %2d  %2d",\
      data[i].site_id, data[i].date, data[i].wind, data[i].degrees);
    }
  }
  printf("\n\n\t\t\t    press enter when ready");
  i=getchar(); 
  return 0;
}
int input(data1 *data, int *topWindSite, int *topRangeSite) {
  FILE *fp;
  int count, i, result, avgWind, last_id, loTemp, hiTemp, range;
  int topRange, topWind, wind; 

  //Open the data file. If it won't open, close the program.
  if((fp = fopen("temps.txt", "rt")) == NULL) {
    printf("\n Error opening data file");
    exit(1);
  }
  i = count  = last_id = avgWind = wind = 0;
  result = range = topRange = topWind = 0;
  loTemp = 200;    //be sure data less than this value
  hiTemp = -200;   //be sure data greater than this value
  result = 4;
  /* before while loops (and sometimes for loops), you usually need to set 
     some variables to certain values. With loTemp and hiTemp, you have to
     be extra careful, or your data will be goofed.
  */

  while(result > 3) {   //got more data in the file?
    result = fscanf(fp, "%d %d %d %d",\
    &data[count].site_id, &data[count].date, &data[count].wind, &data[count].degrees);

    /*      // for debug only 
    if(result == 4)
      printf("\n%d %d %d %d",\
      data[count].site_id, data[count].date, data[count].wind, data[count].degrees);
    */
    if(data[count].site_id != last_id || result < 4) {
      if(avgWind > topWind) {
        topWind = avgWind;
        *topWindSite = last_id;
      }
      range = hiTemp - loTemp;
      if(range > topRange) {
        topRange = range;
        *topRangeSite = last_id;
      }
      last_id = data[count].site_id;
      range = 0;
      loTemp = hiTemp = data[count].degrees;
      wind = data[count].wind;
      avgWind = wind;
      i=1;
    }
    else {  //site is unchanged
      ++i;
      wind += data[count].wind;
      avgWind = wind/i;
      if(data[count].degrees > hiTemp)
        hiTemp = data[count].degrees;
      if(data[count].degrees < loTemp)
        loTemp = data[count].degrees;
    }
    ++count;
  }
  fclose(fp);       
  return count-1;
}

/*  Description:  
You are developing a database of measured meteorological data for use in 
weather and climate research. 
Define a structure type measured_data_t (which I have done) with components 
site_id_num (a four digit integer), wind_speed, day_of_month, and 
temperature. 

Each site measures its data daily, at noon local time. Write a program 
that inputs a file of measured_data_t records and determines the site 
with the greatest variation in temperature (defined here as the 
biggest difference between extrema) and the site with the highest 
average wind speed for all the days in the file. You may assume that 
there will be at most ten sites. 

Test the program on the following July daily data collected over one 
week at three sites:

ID Day Wind Speed (knots) Temperature (deg C)
<< Data >>
2001 10 11 30
2001 11 5 22
2001 12 18 25
2001 13 16 26
2001 14 14 26
2001 15 2 25
2001 16 14 22
3345 10 8 29
3345 11 5 23
3345 12 12 23
3345 13 14 24
3345 14 10 24
3345 15 9 22
3345 16 9 20
3819 10 17 27
3819 11 20 21
3819 12 22 21
3819 13 18 22
3819 14 15 22
3819 15 9 19
3819 16 12 18 
*/

I did this the hard way, by working the calculations, at the time of input - that's not a good idea. Much better (easier and fewer bugs), if you break it up into two functions 1) get the input into the array, and 2) THEN do the calculations in another function.

This article has been dead for over six months. Start a new discussion instead.