Please Help with this program
Author 
Message 
Rya #1 / 4

Please Help with this program
I would REALLY appreciate any help anyone could give on this program as to why it is not doing what it should. This program is supposed to use the multiaray 'index' as a lookup table for calculating the wind chill factor. Temperature is on the x axis and Wind velocity is on the y axis. It would be a simple lookup with array subscripting except my instructor threw a twist on it. He wants it to calculate any number entered. (Each row and columns are in multiples of 10) So if a user enters a temp of 12 and wind velocity of 23 then the computer can figure it by linear interpolation. My instructor said to use the distance formula for getting the distance to each of the corners (do,d1,d2,d3) then to take those numbers and plug them into a weight function. The formulas are in the program but something isn't working right. Another immediate problem is that I JUST began using a macro definition (SQR) and for some reason it isn't working. It keeps giving me a 'undefined x' error. I am not sure how to #define this macro so that it will work with my formulas. Well you could have a field day with this program. If you can make any sense out of it at all, you have my respect! Please ask any questions for more info if you need it. I even asked the c.s. tutor and he couldn't even figure this one out. Thanks again, Ryan The terror begins here:  #include <stdio.h> #include <math.h> #define SQR ((x)*(x)) #define NUM_TEMP 9 #define NUM_WIND 6 void error(void); main() { int itemp=0, iwind=0, d0=0, d1=0, d2=0, d3=0, wc=0, nd0=0, nd1=0, nd2=0, nd3=0; double temp=0, wind=0, ftemp=0, fwind=0, ntemp=0, nwind=0; double index[NUM_TEMP][NUM_WIND] = /*I moved the array down for clarity */ {{20, 30, 55, 80, 105,115}, {10, 20, 46, 63, 82, 102}, { 0, 14, 33, 52, 68, 78 }, { 10, 5, 18, 35, 57, 75 }, { 20, 1, 12, 28, 42, 62 }, { 30, 10, 2, 14, 24, 34 } { 40, 21, 8, 1, 10, 29 }, { 50, 33, 22, 11, 2, 8 }, { 60, 42, 38, 22, 9, 12 }}; puts("Enter temperature (in F, 50 to 20): "); scanf("%lf", &temp); if (temp > 59  temp < 20) error(); /* Give an error */ puts("Enter wind speed (in MPH): "); scanf("%lf", &wind); if (wind > 49  wind < 0) error(); /* Give an error */ ntemp = ((temp + 20) / 10); nwind = (wind / 10); ftemp = temp  (int) temp; fwind = wind  (int) wind; itemp = ntemp  (float) temp; iwind  nwind  (float) wind; d0=sqrt((SQR(ntempftemp)) + (SQR(nwindfwind))); d1=sqrt((SQR(ntempftemp)) + (SQR(nwindfwind))); d2=sqrt((SQR(ntempftemp)) + (SQR(nwindfwind))); d3=sqrt((SQR(ntempftemp)) + (SQR(nwindfwind))); nd0=(SQR(1.4142135623  d0) / 2); nd1=(SQR(1.4142135623  d1) / 2); nd2=(SQR(1.4142135623  d2) / 2); nd3=(SQR(1.4142135623  d3) / 2); wc=(nd0*index[itemp][iwind])+(nd1*index[itemp+1][iwind])+(nd2*index[itemp][iwind+1])+(nd3*index[itemp+1][iwind+1]); printf("\n\nThe wind chill factor would be: %g Degrees Fahrenheit.",wc); return 0; Quote: }
void error(void) { printf("\nYou entered a value out of range"); exit (1); Quote: }

Sat, 28 Aug 1999 03:00:00 GMT 


Andy Knigh #2 / 4

Please Help with this program
The SQR macro should read... #define SQR(x) ((x) * (x)) Even with that fix, the program gives weird results!
Quote: > I would REALLY appreciate any help anyone could give on this program

Sat, 28 Aug 1999 03:00:00 GMT 


Stephan Wilm #3 / 4

Please Help with this program
Quote:
> I would REALLY appreciate any help anyone could give on this program > as to why it is not doing what it should. This program is supposed to > use the multiaray 'index' as a lookup table for calculating the wind > chill factor. Temperature is on the x axis and Wind velocity is on the > y axis. It would be a simple lookup with array subscripting except my > instructor threw a twist on it. He wants it to calculate any number > entered. (Each row and columns are in multiples of 10) So if a user > enters a temp of 12 and wind velocity of 23 then the computer can > figure it by linear interpolation. My instructor said to use the > distance formula for getting the distance to each of the corners > (do,d1,d2,d3) then to take those numbers and plug them into a weight > function. The formulas are in the program but something isn't working > right. Another immediate problem is that I JUST began using a macro > definition (SQR) and for some reason it isn't working. It keeps giving > me a 'undefined x' error. I am not sure how to #define this macro so > that it will work with my formulas. Well you could have a field day > with this program. If you can make any sense out of it at all, you > have my respect! Please ask any questions for more info if you need > it. I even asked the c.s. tutor and he couldn't even figure this one > out. > Thanks again, > Ryan > The terror begins here: >  > #include <stdio.h> > #include <math.h> > #define SQR ((x)*(x))
Write "#define SQR(x) ((x)*(x))" instead (this *should* be in your compiler manual). Quote: > #define NUM_TEMP 9 > #define NUM_WIND 6 > void error(void); > main() > { > int itemp=0, iwind=0, d0=0, d1=0, d2=0, d3=0, wc=0, nd0=0, nd1=0, > nd2=0, nd3=0; > double temp=0, wind=0, ftemp=0, fwind=0, ntemp=0, nwind=0; > double index[NUM_TEMP][NUM_WIND] = /*I moved the array down for > clarity */ > {{20, 30, 55, 80, 105,115}, > {10, 20, 46, 63, 82, 102}, > { 0, 14, 33, 52, 68, 78 }, > { 10, 5, 18, 35, 57, 75 }, > { 20, 1, 12, 28, 42, 62 }, > { 30, 10, 2, 14, 24, 34 } ^^^
There is a ',' missing here. Quote: > { 40, 21, 8, 1, 10, 29 }, > { 50, 33, 22, 11, 2, 8 }, > { 60, 42, 38, 22, 9, 12 }}; > puts("Enter temperature (in F, 50 to 20): "); > scanf("%lf", &temp); > if (temp > 59  temp < 20) > error(); /* Give an error */ > puts("Enter wind speed (in MPH): "); > scanf("%lf", &wind); > if (wind > 49  wind < 0) > error(); /* Give an error */ > ntemp = ((temp + 20) / 10); > nwind = (wind / 10); > ftemp = temp  (int) temp; > fwind = wind  (int) wind; > itemp = ntemp  (float) temp; > iwind  nwind  (float) wind;
This as a 'void' expression: it calculates something, but the result is lost, because you do not assign it to anything. Maybe you wanted to write: "iwind = nwind  (float) wind;" Quote: > d0=sqrt((SQR(ntempftemp)) + (SQR(nwindfwind))); > d1=sqrt((SQR(ntempftemp)) + (SQR(nwindfwind))); > d2=sqrt((SQR(ntempftemp)) + (SQR(nwindfwind))); > d3=sqrt((SQR(ntempftemp)) + (SQR(nwindfwind))); > nd0=(SQR(1.4142135623  d0) / 2); > nd1=(SQR(1.4142135623  d1) / 2); > nd2=(SQR(1.4142135623  d2) / 2); > nd3=(SQR(1.4142135623  d3) / 2); > wc=(nd0*index[itemp][iwind])+(nd1*index[itemp+1][iwind])+(nd2*index[itemp][iwind+1])+(nd3*index[itemp+1][iwind+1]); > printf("\n\nThe wind chill factor would be: %g Degrees Fahrenheit.",wc); ^^^^
Wrong 'printf' format specification: the argument is of type 'int' not 'double', so you should use '%d'. Quote: > return 0; > } > void error(void) > { > printf("\nYou entered a value out of range"); > exit (1);
The prototype for 'exit()' is not in scope. You should include "stdlib.h". Quote: > }
MAYBE, just maybe, your program will work after making the corrections indicated above, but it is unlikely. I didn't check, if the results are correct, but your program contains a wild mix of various different data types (mostly 'int' and 'double') in very suspect combinations. Your compiler will have to perform a lot of implicite type conversions. Stephan

Sun, 29 Aug 1999 03:00:00 GMT 


John Bicke #4 / 4

Please Help with this program
Quote:
> #define SQR ((x)*(x))
This should be something like... #define SQR(x) ( (x) * (x) ) Perhaps you should output intermediate results between your calculations, to check that you're getting the right numbers at each step.

Sun, 29 Aug 1999 03:00:00 GMT 


