Please, I have tried everything that I can learn from a book...any help with this problem is greatly appreciated. I've spent days writing and modifying and I am just lost now.

Is there anybody that can help me solve this question using C language?

You are developing a database of measured meteorological data for use in weather and climate research. Define a structure type measured_data_t 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 C 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)
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

Recommended Answers

All 25 Replies

Where's the code ?

Here is the code I was working with:

#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
	fscan_measured(FILE *filep, /* input - file pointer */
	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
	*/
	void
	measured_data_t[] /* output - array of data */

	{
	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 ????????
	*/

			}
		}
           }

padalhan u naman poh aq ng database program s turbo c sa email q ***

padalhan u naman poh aq ng database program s turbo c sa email q ***

1) Put your hands on the proper keys when you start typing.
2) Type full English words, not Gobbledygook
3) If you have nothing to add to this thread, start your own.

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

#define MAX_SITES 10 /* maximum number <strong class="highlight">of</strong> sites allowed */

typedef struct measured_data_t
{
	int site_id_num; /* id number <strong class="highlight">of</strong> the site */
	int wind_speed; /* the wind speed <strong class="highlight">in</strong> knots */
	int day_of_month; /* the day <strong class="highlight">of</strong> the month written as dd */
	int temperature; /* temp <strong class="highlight">in</strong> celcius */
};

{
/* 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
fscan_measured (FILE *filep,   /* input - file pointer */
measured_data_t *measuredp) /* output - measured_data_t structure to fill */

{
int status;
status = fscanf(filep, "%d%d%d%d", _id_num,
&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
*/
void
measured_data_t  /* output - array of data */
{
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 ????????
*/

Where exactly are you having a problem ? These are some of the mistakes that I could see
1. Line 8- Dont use typedef in front of the struct
2. Line 29- You have written 4 %d but then you are trying to input 5 things.
3. Line 46- Function definition, syntax is wrong
4. Line 55- For loop syntax is wrong

Make these changes, compile and run the code, then post where you are having problems

Thanks, for your third and fourth recommended change I am not quite sure what you are saying. Also, these are the two error messages I get when I run this

1. Line 16: error: expected unqualified-id before '{' token
2. Line 48: error: invalid function declaration

Once again thanks for your help, sorry to seem so lost but I really am on this assignment.

Where did you get this code from ? Is this your code ?

1. The void measured_data_t function declaration is wrong. Correct that. Google for the correct syntax for function declaration
2. Delete the opening brace on line 16 and its corresponding closing brace.
3. I dont think line 18 is necessary

The problem is from my coursebook Problem Solving and Program Design in C. I am re-writing/modofying a code that was goven on a handout for the class that is "similiar" to what the original question is. I think this is why I am running into so many problems....I've tried starting from scratch using the text from the book but I can not succeed.

The for loop on line 55 is actually correct (I think the other poster misread it, it took me a couple of times too). It's legal it just seems a little unorthodox, IMO and definitely hard to read. You don't define the function that it calls yet either. You'd written what looks like a prototype and stopped, at least put the braces in and a "dummy" return value or a message to yourself that you need to implement it.

Line 47 is still wrong. What the other poster was trying to get you to do was look up any function with no arguments. You still need the () after the name for valid syntax.

Keep on trucking. It's much improved. Try not to emulate the other code so much that you're not thinking about what you need to do.

You are right ... My mistake
The for loop is indeed correct

I was stuck and sent my instructor what I have had so far....she gave me more advice and here is my code reworked...however I am still getting errors. Any help with this new code? Please, any help is appreciated.

#include <stdio.h>
 
#define NumberOfSites 10
 
typedef struct
{
                int site_id_num;
                int wind_speed;
                int day_of_month;
                int temperature;
}               measured_data_t;
 
 
typedef struct
{
                int site_id_num;
                int wind_speed;
                int count;
                int low;
                int high;
}   SiteRecord;    
 
int ReadRecord (FILE* input, measured_data_t* record);
int FindRecord (SiteRecord sites[], int index, measured_data_t* record);
void InitializeRecord (SiteRecord* site, measured_data_t* record);
void UpdateRecord (SiteRecord* site, measured_data_t* record);
int ReadRecords (FILE* input, SiteRecord sites[]);
void ProcessRecords (SiteRecord site[], int sites, int* variationSite, int* windSite);
FILE* GetInput (void);
 
void main (void)
{
                SiteRecord site [NumberOfSites];
 
                FILE* input = GetInput();
 
                if (input)
                {
                                int sites = ReadRecords (input, site);
 
                                fclose (input);
 
                                if (sites)
                                {
                                                int variationSite, windSite;
                                
                                                ProcessRecords (site, sites, &variationSite, &windSite);
 
                                                printf ("\nGreatest temperature variation:  Site %d", variationSite);
                                                printf ("\nGreatest average windspeed    :  Site %d", windSite);
                                }
                }
}

Where are the function definitions? The compiler is just telling you there's no definitions for the functions at the linker stage.

Also, main() should always return an int.

The first struct you have defined is wrong. The second struct you have defined is correct. So make this change

It is not necessary that the main function return an int .
void main is also fine

I was stuck and sent my instructor what I have had so far....she gave me more advice and here is my code reworked...however I am still getting errors.

You're still getting errors.... Hmmmm, my psychic powers are picking up
Error E2265 x.cpp 2: No file name ending
Error E2062 x.cpp 47: Invalid indirection in function main()
Error E2451 x.cpp 53: Undefined symbol 'r' in function main()
*** 3 errors in Compile ***
You forgot the > in the include line on line 2
You have too man *s on line 47
The variable r was not defined when used on 53

How'd I do?

void main is also fine

void main is NEVER fine... Read this.

Here are the errors I am getting to the code written below...I am so close but I think Im going blind from looking at this thing so much. I have started from scratch and without emulating a previous example and taking some instructor advice, I come up with the following errors.

(.text+0x34)||undefined reference to `GetInput()'

(.text+0x5f)||undefined reference to `ReadRecords(_iobuf*, SiteRecord*)'

(.text+0xa8)||undefined reference to `ProcessRecords(SiteRecord*, int, int*, int*)'

|=== Build finished: 3 errors, 0 warnings ===|

#include <stdio.h>
#include <stdlib.h>
#define NumberOfSites 10

typedef struct
{
                int site_id_num;
                int wind_speed;
                int day_of_month;
                int temperature;
}               measured_data_t;


typedef struct
{
                int site_id_num;
                int wind_speed;
                int count;
                int low;
                int high;
}               SiteRecord;

int ReadRecord (FILE* input, measured_data_t* record);
int FindRecord (SiteRecord sites[], int index, measured_data_t* record);
void InitializeRecord (SiteRecord* site, measured_data_t* record);
void UpdateRecord (SiteRecord* site, measured_data_t* record);
int ReadRecords (FILE* input, SiteRecord sites[]);
void ProcessRecords (SiteRecord site[], int sites, int* variationSite, int* windSite);
FILE* GetInput (void);

int main ()
{
                SiteRecord site [NumberOfSites];

                FILE* input = GetInput();

                if (input)
                {
                                int sites = ReadRecords (input, site);

                                fclose (input);

                                if (sites)
                                {
                                                int variationSite, windSite;

                                                ProcessRecords (site, sites, &variationSite, &windSite);

                                                printf ("\nGreatest temperature variation:  Site %d", variationSite);
                                                printf ("\nGreatest average windspeed    :  Site %d", windSite);
                                }
                }
}

Did you implement those functions or just declare them?

I thought I was implementing them....am I missing something?

>am I missing something?
Besides all of the code that makes a function do something? Let's take GetInput as an example:

FILE* GetInput (void);

This is a declaration. It tells the compiler that when you say GetInput, you mean a function with no parameters and a return type of FILE*. You still need to say what GetInput does when it's called. I can only imagine that you expected the compiler to read your mind, figure out what you wanted GetInput to do, and convert your brainwaves to machine code.

Unfortunately, it doesn't work that way. You need to provide a definition as well, so that the compiler knows what to do when the function is called:

FILE* GetInput (void)
{
  return fopen("myfile", "r");
}

Thanks Narue and everybody else. Sorry to come off as such a noob but I'm only recently exposed to programming. I'm still having difficulties with this. I defined the GetInput by tellign it to open the measured_data_t file like I did in my earlier program. Somewhere along the line I have lost track...Do you see any more mistakes? I'm not trying to have you guys solve the problem for me but I'm not sure what mistakes I'm making and how to fix them....I'm trying to learn.

I defined the GetInput by tellign it to open the measured_data_t file like I did in my earlier program.

Ok, but do you have it defined in that external file someplace? If so you'll have to compile this file a link with the compiled version of that file (or just compile them both at once). Otherwise the definitions (like what Narue wrote out) will have to be in this file after main().

Thanks again everybody, I have no choice but to submit what I have...it's a shame that I couldn't figure this out. Thanks again.

Keith

So my instructor gave me a 60 on this...more out of pity I'm sure....but I'm not happy with the fact that I couldn;t figure out this problem....can somebody please show me strp by step what I was doing wrong...perhaps a side by side comparison of my code compared to a code that works correctly. I knwo this is asking a lot, but I need to see where my mistakes were because I do want to learn this. FYI, do not take on-line classes for classes like this....the text is only minimly helpful at best and without an instructor to show you what is wrong, it's impossible to learn correctly. Thanks again for everybody's help.

Ok, well you have to get some more info on functions from somewhere. There are probably thousands of sites that cover them online, pick one you like and go through it. But for now...

You know that functions require a declaration (also known as a prototype) a.k.a. the thing at the top of the code with a semicolon after it. A declaration lets the compiler know that "hey, there's a function out there somewhere that takes this number and type of parameters." When you see that code has a header file (usually for accessing a library) it will usually contain these prototypes among other things.

To go along with the prototype your function needs a definition. This is normally found after the closing brace of main() but it's legal to just define your functions before main() and you can skip the prototyping. We'll assume the former for now. So the definition tells the compiler what the function does what it returns and it spells out all the code necessary.
The difficulty you were having was you didn't put any function definitions in your code. You had the prototypes correct at the top but there were no actual functions to go along with them. The compiler was giving you the benefit of the doubt that maybe they were in another file but when it came time to link all the binaries together they were nowhere.

A quick example:

#include <stdio.h>

int addone(int myvar); //your prototype 
                                   //could just be int addone(int); since the 
                                 //compiler doesn't care about the name of the 
                                //variable right now.

int main()
{
     int returnvalue;
     int inputtofcn = 3;
     returnvalue = addone(inputtofcn);
     printf("%d\n",returnvalue);
     return 0;
}
//ok, main is over with now we can place our defnintion
int addone(int inputval) //parameter name not related to anything else
                                      //except in the function body
{
    int localval;  //make a local variable to store the sum, don't need to
    localval = inputval+1;  
    return localval;  //note we could just have returned inpuvtval+1
                             //directly
} //so now we've defined what the function does and the main 
   //program can call it
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.