Newbie needing assistance!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Newbie needing assistance!

Hybrid View

  1. #1
    Join Date
    Jan 2009
    Posts
    6

    Talking Newbie needing assistance!

    Hi! I am VERY new to the programming world and am trying to complete an assignment. The assignment is as follows:
    /* Purpose: Define a structure type measured_data_t with componets site_id_num (a four dig interger), wind_speed, day_of_month and temperature. Each site measures its daily data at noon. Write a program that inputs a file of measured_data_t records and determines the site with the biggest variation in temp and the site with the highest avg wind speed for the days in the input file. Data inclosed below./

    /*You may assume that there will be at most ten sites */

    I have been working on this thing for days and am attaching my code. The entire code must be in C. I am having trouble sorting the struct. I want to treat it as an array, but even then I'm stuck because my book only shows sorting one-dimensional array's and this looks like a 2D to me. I'm not even sure anymore!

    Any tips would be appreciated!


    Code:
    <script type="text/C">
    
    #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 ????????
    	 */
    Last edited by k_lynn; January 8th, 2009 at 09:05 AM. Reason: Code

  2. #2
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: Newbie needing assistance!

    Please use code tags. They are dicsussed in the "BEFORE you post" announcement. The link is in my signature (be sure to enable signatures and private messaging in your "User CP".

    Once your code is readable (you edit the existing post, not create a duplicate), I will take a look again.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009
    In theory, there is no difference between theory and paractice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Jan 2009
    Posts
    6

    Re: Newbie needing assistance!

    I was able to get the code inserted properly (I hope). Like I said, my main concern is how I am supposed to sort the struct. It has four parts: a site ID, the wind speed, day of month, and temp. I have to be able to sort inside each site ID to find the largest temp varience and the highest average wind speed. I think I would treat it as a 2D array [a][b][c][d] and sort inside [a] and then do the calculations based on either [b] or [d]... but I'm really not sure and my book is absolutely no help.

    Any help would be greatly appreciated!

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,885

    Re: Newbie needing assistance!

    Well, my first question is, why does your struct contain arrays? I see no need for that in the problem description. I'm assuming each measured_data_t would have only 1 site_id_num, not 4, for example.

    Second, I don't think it's strictly necessary to sort the array. You merely need to figure out how many unique Site IDs you have, and keep statistics for each one on a single pass through the array.

  5. #5
    Join Date
    Jan 2009
    Posts
    6

    Re: Newbie needing assistance!

    The book offers a set of "test" variables for the program. These variables list 3 site IDs with 7 inputs each for the day, wind, and temp. What they want is for me to name which site ID shows greatest variance in temp and greatest average wind. That's where I'm getting confused.

    It is also asking me to be able to pull this information from the file measured_data_t, so ultimately, I don't know how many inputs there will be.

    Thanks so much for the QUICK reply!

  6. #6
    Join Date
    Jan 2009
    Posts
    6

    Re: Newbie needing assistance!

    I just re-read the second part of your answer. I have no idea how that would work in C. I understand the concept completely, but the code writing is where I'm having the trouble. Unfortunately, this is an internet-based class and the teacher is non-responsive to emails, etc.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,885

    Re: Newbie needing assistance!

    Showing a bit of the test data might be helpful.

    Are you allowed to use a std::map? If so, that would probably be the easiest way to handle the problem. For instance, a map<int,int> could map siteids to wind speeds----for each record compare the wind speed against the mapped greatest wind speed, and replace it if the new one is larger.

  8. #8
    Join Date
    Jan 2009
    Posts
    6

    Re: Newbie needing assistance!

    The test data is as follows:
    ID Day Wind Speed Temp
    2001 10 11 30
    2001 11 5 22
    2001 12 18 25
    2001 13 16 26
    3345 10 8 29
    3345 11 5 23
    3345 12 12 23
    3345 13 14 24
    3819 10 17 27
    3819 11 20 21
    3819 12 22 21
    3819 13 18 22

    My book has no reference to either std or map, it is straight C... but I see what you're saying. Create a map for each site_ID and then compare the values and replace if larger. And then simply use the map to calculate the mean for each ID?

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,885

    Re: Newbie needing assistance!

    Well, I was slightly wrong about the "replace if larger" bit since I was misreading the problem, but yeah.

    If you're trying to use C rather than C++, then sorting your array may be the best move after all: You would use qsort(), with a predicate that compares based only on Site ID. This would arrange your array so that you could loop over all data for a given site simply by checking for when the site ID changes in the next index.

  10. #10
    Join Date
    Jan 2009
    Posts
    6

    Re: Newbie needing assistance!

    I will see what I can do with the qsort(). I've not heard of it before, but that isn't a big surprise. I really appreciate your help! Thanks, Lindley!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center