Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Finding average of a two dimensional array (http://www.go4expert.com/forums/finding-average-dimensional-array-t17840/)

CrazyGal 3Jun2009 07:12

Finding average of a two dimensional array
 
Hi there,

I did some problem based on two dimensional array. I'm not quite happy with the code I've produced. Could someone tell me if there's a better way of doing this stuff? The problem demands the use of one/two dimensional arrays.

Problem Statement:
1. Read a bunch of temperature readings. ie, A latitude and a corresponding temperature value (both are ints). Latitude to be from -90 to 90.
2. I need to find the average of the temperature at a particular latitude
3. Print the average temperature for all latitudes from -90 to 90 if present, else "NO DATA"

Below is my code.

Any comments are welcome.

Thanks,
Tina

P.S: Could you tell me how to close a thread? I do not find a close thread option and I did the FAQS too. Perhaps I missed it. Let me know. Thanks again!

Code:

#include <stdio.h>

#define NO_OF_LATITUDES 181 /* possible values -90 to +90 */

void findAverage(int (*)[2], int);

int main()
{
    int n = 0;
    int a[181][2]; /* array of latitude and temperature */
 
    scanf("%d", &n);
   
    int i, j;
    for(i = 0; i < n; i++)
        for(j = 0; j < 2; j++)
          scanf("%d", &a[i][j]);
   
    findAverage(a, n);
    return 0;
}

void findAverage(int (*q)[2], int rows)
{
    int i,  sign = 1;
    int *p;
    static int arr[NO_OF_LATITUDES][2];

    for ( i = 0; i < rows; i++)
    {
        p = q + i;
        if(*p < 0) /*store negative latitude(-90 to -1) values in array range from 1 - 90 */
        {
            arr[abs(*p)][0] += *(p+1);
            arr[abs(*p)][1] += 1;
        }
        else if(*p > 0)
        {
            arr[(*p) + 90][0] += *(p+1); /*store positive latitude(1 to 90) in array range 91 - 180*/
            arr[(*p) + 90][1] += 1;
        }
        else
            printf("0 is not a valid latitude\n");
           
    }

    for(i = 1; i <= NO_OF_LATITUDES; i++)
        if(arr[i][0])
        {
            if (i <= 90)
            {
                sign = -1;
                printf("Latitude = %d  : Temperature = %f\n", sign*i, (float)arr[i][0]/arr[i][1]);
            }
            else
            {
                printf("Latitude = %d  : Temperature = %f\n", (i - 90), (float)arr[i][0]/arr[i][1]);
            }
           
        }
        //else
        //    printf("NO DATA\n");
}


xpi0t0s 3Jun2009 12:46

Re: Finding average of a two dimensional array
 
Could you be a bit more specific please? Why aren't you happy with the code? Does it produce correct results and run in reasonable time?
Why do you duplicate the array in findAverage?

CrazyGal 3Jun2009 18:37

Re: Finding average of a two dimensional array
 
I felt having array indices like arr[abs(*p)][0] is not appropriate. And the overall logic, I believe is not good. It does produce correct results and run in reasonable time. I've two arrays because the first one scans the latitude and temperature values and the second one has the temperature sum and their count values at their respective latitude index. Perhaps, I could have two simple integer variables like latitude and temperature while scanf and store them in one two dimensional array in main. Also, to store latitude values -90 to -1, I've used array ranges 1 to 90 and for 1 to 90 I've used 90 to 180. Is there any other way to do this?

These are the few questions I've in mind.

Please let me know if you've questions.

Thanks,
Tina

xpi0t0s 4Jun2009 04:33

Re: Finding average of a two dimensional array
 
Oh I see, sorry it's not a duplicate. Still seems unnecessary to me though, although the benefit of this is that you only have to parse the data once. Still, at only 180 values, unless you're programming a ZX81 or something, scanning through that 180 times is still going to take a few milliseconds at the worst. Is it necessary to store intermediate data, and if so why?

> I felt having array indices like arr[abs(*p)][0] is not appropriate

You need to explain comments like this. Why don't you feel that is appropriate? If that's not appropriate, what would be, and why didn't you code it that way in the first place?

> And the overall logic, I believe is not good

Again, why exactly? What logic would be good? Statements like this make no sense if they are not explained.

> printf("0 is not a valid latitude\n");

Why is zero not a valid latitude? What is the latitude of the equator?
http://en.wikipedia.org/wiki/Latitude seems to suggest the equator's latitude is 0.

> Also, to store latitude values -90 to -1, I've used array ranges 1 to 90 and for 1 to 90 I've used 90 to 180. Is there any other way to do this?

Well, you could create a values array (let's call it v_arr) from 0 to 181 and an integer pointer to the midpoint, i.e. int *i_ptr=&v_arr[90]; Then you can use the values -90 to +90 as indices into i_ptr, thus treating it as if it were defined as int i_ptr[-90...+90], which is a sort of C/Pascal hybrid definition and otherwise impossible in C. Negative indices are perfectly OK in C, as long as the resulting address points to valid memory. It's essentially the same thing. Or you could simply add 90 to the latitude values and store data in v_arr[lat+90] which again more or less amounts to the same thing. Using negative indices will make anyone reviewing your code think twice, check carefully and deduce that you must be smarter than they originally thought, but only IF you've done it right!

CrazyGal 4Jun2009 05:24

Re: Finding average of a two dimensional array
 
Umm..If having arr[lat+90] is not a bad way of doing things, then it's alright.

> I felt having array indices like arr[abs(*p)][0] is not appropriate
You need to explain comments like this. Why don't you feel that is appropriate? If that's not appropriate, what would be, and why didn't you code it that way in the first place?

ANSWER: I thought its inappropriate because of the hard coding of the array indices (column value). I did try for ways to get rid of implanting the column index but I couldn't succeed. So, I followed this option.

> And the overall logic, I believe is not good
Again, why exactly? What logic would be good? Statements like this make no sense if they are not explained.

ANSWER: By this, again I meant the same explanation as above and also using two 2D arrays. I mean finding the average of 1D array is straightforward. So, for two 2D array, I wanted to know if there are any key techniques to do the same.

> printf("0 is not a valid latitude\n");
Why is zero not a valid latitude? What is the latitude of the equator?
http://en.wikipedia.org/wiki/Latitude seems to suggest the equator's latitude is 0.

ANSWER: I skipped through latitude 0 because it was not mentioned in the problem statement. No particular reason for that.

Thanks for taking time to respond to my queries.


All times are GMT +5.5. The time now is 10:26.