View Single Post
 Go4Expert Member 3Jun2009,07:12 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 #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"); }```