Program is working, just wanting some opinions.

Just wanting some opinions on my code really. Below is a program that coverts temperature, wind, and relative humidity into a "Heat Index" amd a "Windchill".

1. Is there anything I should improve on here?

2. Would this program be efficient enough for an internship somewhere? I've been writing C++ code for maybe a year and a half but I'm not sure "where I stand" as a programmer, any thoughts?

Thanks for any input.

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <ostream>
#include <iostream>
#include <string>
#include <cmath>
using namespace std;



void Input(int *v, int *t, int *r) // <--- This is declaring a pointer of type int for v and t. These are our variables
{
	int x, y, z; // <--- So we can store our answers
	cout << "Please type in the wind speed in mph: " << endl;
	cin >> x;
	*v = x; // <---- This is taking our answer and assigning it to a memory address

	cout << "Please type in the temperature degrees Farenheit: " << endl;
	cin >> y;
	*t = y; // <---- This is taking our answer and assigning it to a memory address

	cout << "Please type in the Relavtive Humidity: " << endl;
	cin >> z;
	*r = z; // <---- This is taking our answer and assigning it to a memory address

}

void Windchill(int &t, int &v, double *w) // <--- The & is asking for the memory addresses that our t and v are stored in so we can use t and v below in our equation
										  // *w is declaring a pointer of type double.
{

	double z;
	z = 35.74 + 0.6215 * t - 35.75 * pow(v, 0.16) + 0.4275 * t * pow(v, 0.16);
	*w = z; // <---- This is taking our answer and assigning it to a memory address

}

void HeatIndex(int &t,int &r,double *Heatindex)
{
	double Hindex;
	Hindex = -42.379 + 2.04901523*t + 10.14333127*r - .22475541*t*r - .00683783*t*t - .05481717*r*r + .00122874*t*t*r + .00085282*t*r*r - .00000199*t*t*r*r;

	*Heatindex = Hindex;
}

int main()
{
	int v, t, r;
	double w, Heatindex;

	Input(&v, &t, &r); // <--- This is calling our function by reference. The & is asking for the memory address that our pointer stored it in.
	Windchill(t, v, &w); // <--- This is calling our function by reference. &w is asking for the memory address that our pointer stored the windchill in.
	HeatIndex(t, r, &Heatindex);

	cout << "The Windchill is roughly: " << w << " degrees." << endl;
	cout << "The Heat Index is roughly: " << Heatindex << endl;

	system("Pause");
	return 0;
}
Your comments don't tell the reader anything that the code doesn't. Add comments to show what the functions do. Also put a comment at the top of the program to say what it does. None of these need to be elaborate.

Line 5: It's bad form to include the entire std namespace and it only takes a few minutes to include just the names you need. I removed this and in about 2 minutes had the code compiling again with:
1
2
3
4
5
using std::cin;
using std::cout;
using std::endl;
using std::pow;
using std::system;


Line 9: Use references for output parameters because it's much harder to pass a null reference than it is to pass a null pointer.

Line 26. Since there is only one output, WindChill should return the windchill rather than setting an output parameter to it. That way you can use the result in an expression. Also since you aren't setting t and v, and because they are small, there's no need to pass them by reference.

Line 31. Maybe provide a reference to the formula.

Line 36: Same comment as line 26

Lines 53 & 54: Don't use endl unless you really need to flush the stream. Flushing is expensive.

By changing HeatIndex and Windchill to functions, main() gets a lot simpler:
1
2
3
4
5
6
7
8
9
int main()
{
    int v, t, r;
    Input(v, t, r);

    cout << "The Windchill is roughly: " << Windchill(t,v) << " degrees.\n";
    cout << "The Heat Index is roughly: " << HeatIndex(t,r) << '\n';
    return 0;
}


Putting it all together:
// Program to compute the windchill and heat index from temperature,
// wind speed and relative humidity

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
38
39
40
41
42
43
44
45
46
47
48
49
#include <ostream>
#include <iostream>
#include <string>
#include <cmath>

using std::cin;
using std::cout;
using std::endl;
using std::pow;
using std::system;


void Input(int &windSpeed, int &temp, int &relativeHumidity)
{
    cout << "Please type in the wind speed in mph: " << endl;
    cin >> windSpeed;

    cout << "Please type in the temperature degrees Farenheit: " << endl;
    cin >> temp;

    cout << "Please type in the Relavtive Humidity: " << endl;
    cin >> relativeHumidity;
}

// calculate the heat index from temperature t(degrees F) and windspeed v(MPH)
double Windchill(int t, int v)
{
    double result = 35.74 + 0.6215 * t - 35.75 * pow(v, 0.16) + 0.4275 * t * pow(v, 0.16);
    return result;
}

// compute heat index from temperature t(degrees F) and relative humidity (0-100)
double HeatIndex(int t,int r)
{
    double Hindex = (-42.379 + 2.04901523*t + 10.14333127*r -
		     .22475541*t*r - .00683783*t*t - .05481717*r*r +
		     .00122874*t*t*r + .00085282*t*r*r - .00000199*t*t*r*r);
    return Hindex;
}

int main()
{
    int v, t, r;
    Input(v, t, r);

    cout << "The Windchill is roughly: " << Windchill(t,v) << " degrees.\n";
    cout << "The Heat Index is roughly: " << HeatIndex(t,r) << '\n';
    return 0;
}


This code alone would not be enough for me to recommend you for an internship. The comments I've given are one reason, but also because the code is very simple. It doesn't demonstrate very much of the language. There are no classes, no loops, not even an if statement. I suggest that you write something more intricate.
Topic archived. No new replies allowed.