Sunday, August 2, 2009

I need help w/ a C++ loop based program?

Windchillold = 0.081 x (3.71 x Öv + 5.81 - 0.25 x v) x (t - 91.4) + 91.4


Windchillnew = 35.74 + 0.6215 t - 35.75(v0.16) + 0.4275T(v0.16)





where v is the wind speed in MPH, and t is the temperature in Fahrenheit.





i need to write a C++ program that will create wind chill charts for a given temperature. the program should display the following menu to the user and prompts for his/her choice:


O: Display old wind chill index chart


N: Display new wind chill index chart


C: Compare old and new wind chill index charts





If the user enters 'o' or 'O', then the program should ask the user for a temperature (in degrees F) and then display the old wind chill values for wind speeds from 0 to 50 mph in increments of 5 mph. It is assumed that the user will enter a temperature value between –40 and 40.


If the user enters 'n' or 'N', then the program should ask the user for a temperature (in degrees F) and then display the new wind

I need help w/ a C++ loop based program?
I can only say so much because those equations are too long for two in the morning. Not that it matters; I just won't be retyping them. But you certainly need a bunch of random things for this. First, you need a variable -- a char, probably, since you don't care about any input beyond that. I don't know what the compare thing is going to output, but that'll come.





I suggest an array for the old and new charts.





Geez, what's the syntax? int oldchart[11], or something? I'm getting too used to Java. Anyway. Since you're going in increments of 5 from 0 to 50, you'll have a 0, 5, 10, ... 45, 50 -- that is, 11 different speeds, right? So you'll need an array for the old chart and an array for the new chart, both with a size of 11. Do you get where I'm going?





Calculating the chart takes a bit more thinking. We'll ignore the actual process of getting input for the moment and just deal with filling it in. I'll suggest a method, and explain afterwards:





int speed;


for(int i = 0; i %26lt; 11; i++)


{


speed = i*5;


oldchart[i] = %26lt;calculate wind chill%26gt;;


}





The same thing could be applies to the newchart array. The index, i, will go from 0 to 10, matching the indexes of the array, right? Since you're going by increments of 5, the speed for slot i will be i*5. That takes you from 0 to 50.





So after you pull that off for the old and new, you've got charts saved in the variables. Printing them is kind of similar to what happened above -- you use a loop to output what's in the array, using i*5 for the speed.





Now, granted, you could combine this and the above step so you wouldn't need the arrays at all--you'd just recalculate and print every time. But I find it easier to organize my thoughts by going a way that deals with one issue at a time... It depends on what you prefer at this point.





for(int i = 0; i %26lt; 11; i++)


{


speed = i*5;


cout %26lt;%26lt; "Speed: "%26lt;%26lt; speed %26lt;%26lt; " Wind Chill: " %26lt;%26lt; oldarray[i] %26lt;%26lt; endl;


}





Let's see...





Displaying the menu is easy--just use cout %26lt;%26lt; "your menu stuff" %26lt;%26lt; endl;


Input is a bit stranger, I think. For something like this, just use a cin and check afterwards if it's right. Um, let's see... I'll give this to you in pseudocode--you should be able to work out the actual stuff.





while ( the user doesn't want to exit )


//this is just a logic thing -- if the user is done, then what?


{


take the user's input;





if the input is 'o' or 'O'


{


calculate and print the old chart


}





else if the input is 'n' or 'N'


{


calculate and print the new chart


}





else if the input is 'c' or 'C'


{


calculate and print both the old and new charts (?) (whatever compare does)


}





else if the user wants to exit


{


end the program (usu. with return 0 or something of that nature)


}





else the user's input was bad


}





Mind that for this to go well, you'd have to mind a few issues -- as I wrote above, if the user doesn't enter what you expect them to. Also, you'd have to deal with a case where the user decided to print charts before entering data into the old or new charts. For that, you should give the temperature a default value, like 0, so the charts can still be calculated.





You can see that I wrote in a calculation for every print, which is what I was saying before with it not being necessary. You could technically use the for loop I gave for printing but just replace the reference to the array with the calculation for the wind chill.





...Don't know if I covered all of it, but I should be getting to bed. ^^;


No comments:

Post a Comment