having trouble with exponents in equation for array

I am trying to recreate a table in c++ for homework and I am having trouble getting the equation to get the value to work. I get an error, and I am not sure if I am using the power code correctly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <math.h>

using namespace std;

const int TEMPS = 18;
const int WINDS = 12;

int main () {
    
    
    
    int windChill[TEMPS][WINDS] = {0};
    
    for (int temp = 45; temp < -45; temp-5) {
        for (int wind = 5; wind < 61; wind+5) {
            windChill[TEMPS][WINDS] = 35.74 + (.6215 * temp) - (35.76)(pow(wind,.16)) + (.4275 * temp)(pow(wind,.16));
             
        }
    }
    
    for (int temp = 45; temp < -45; temp - 50) {
        for (int wind = 5; wind <61; wind+5) {
            cout << windChill[TEMPS][WINDS] << "\t";
            }
            cout << "\n";
        
    }
    
    cout << "test line\n";
    
    return 0;

}           


I am trying to recreate the table located at:
http://www.nws.noaa.gov/om/winter/windchill.shtml

I am not able to run the code due to these errors:
Line 17 3.57599999999999980104803398717194795608520507812e+1' cannot be used as a function
17 (#`float_expr' not supported by dump_expr#<expression error> * 4.27499999999999991118215802998747676610946655273e-1)' cannot be used as a function
Last edited on
Hi,

You are missing some operators on this line:

windChill[TEMPS][WINDS] = 35.74 + (.6215 * temp) - (35.76)/* operator*/ (pow(wind,.16)) + (.4275 * temp)/* operator*/(pow(wind,.16));

Edit:
So it is the * 's (multiplication) you are musing

Please use code tags, it makes it easier for us in lots of ways:

http://www.cplusplus.com/articles/z13hAqkS/
Last edited on
Lines 17 and 24 produce undefined behavior because you are accessing the windChill array out of bounds. C++ arrays are indexed starting at zero, sowhen you declare windChill[TEMPS][WINDS], it means the last item is windChill[TEMPS-1][WINDS-1].

My advice is to change the loop indexes to be the indexes of windChill, and then set the wind and temp accordingly:
1
2
3
4
5
6
7
8
9
    for (int j=0; j<WINDS; ++j) {
	for (int i=0; i<TEMPS; ++i) {
	    int temp = 40 - i*5;
	    int wind = 5 + j*5;
	    windChill[i][j] =
		35.74 + (.6215 * temp) - (35.76) * (pow(wind, .16)) +
		(.4275 * temp) * (pow(wind, .16));
	}
    }


Change the loop that prints the results the same way.

This will get you very close but some of the answers will be off by one. Use the round() function when assigning to windChill to round the numbers off to the nearest integer.
Phew, after your guys help and even more research, i got everything to work.
Wasn't aware that you could declare variables inside of for loops and other functions, that was an eye opener for sure. made everything else fall into place.

My question now, is I have recreated the table perfectly (aside from two of the values rounding higher, which I'm going to chalk up to math since the rest of them came out perfectly). Is there a way to put the actual temperature values and wind speed values as the x and y axis, that way the person looking at my table would actually know what they are looking at??

I tried to manual enter them via cout<< but nothing lined up correctly.

My updated code is below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <math.h>
#include <iomanip>

using namespace std;

const int TEMPS = 18;
const int WINDS = 12;

int main () {
    
    
    
    double windChill[TEMPS][WINDS] = {0};
    
    for (int j=0; j<WINDS; ++j) {
	    for (int i=0; i<TEMPS; ++i) {
	        int temp = 40 - i*5;
	        int wind = 5 + j*5;
	        windChill[i][j] =  round (35.74 + (.6215 * temp) - ((35.76) * (pow(wind, .16)))
            + ((.4275 * temp) * (pow(wind, .16))));
        }
    }
    
    for (int j=0; j<WINDS; ++j) {
	    for (int i=0; i<TEMPS; ++i) {
	        cout << setw (4);
            cout << windChill[i][j];    //two numbers different outputs, rounding?
        }
        cout << "\n";
    }
    
    cout << "test line\n";
    
    return 0;

}           


Again, any help is appreciated. I don't need to be told the answer, just pointed in the right direction! Thanks
Topic archived. No new replies allowed.