# Function Program

How do I write a program that calculates the speed of sound (a) in air of a given temperature T (oF). The speed of sound must be calculated in a function and the results printed to the screen from the loop() function. Formula to compute the speed in ft/sec: a=1086√((5T+297)/247)

I did my own program but it didn't seem right and I would like to compare.

How to write a program that asks the user for a number and then determines if the number is prime or not. This determination is done in a function called prime() that accepts an integer number and returns a result of prime or not. (Use a flag to do this). See appendix for how to determine if a number is prime and how to use flags. Here is the skeleton (pseudocode) -Setup your serial port in setup() -In the loop() function, ask the user for a number to check, N -Call the function prime() and pass to it the value of N. The function will return a flag that can be checked to see if N is prime or not -Inside the function, using a loop, check if N is prime -Do this by checking if N is divisible by any number from 2 to the square root of N. -Use the modulus (%) operator. If the remainder is zero, you found a factor and the number N is not prime. - Return a TRUE/FALSE flag value to the loop() function.

Well that's obviously a homework problem. How far have you got? We are definitely not going to write it for you and there is more than enough in the question to at least get a start.

Why don't you post the one you wrote?

Is this a homework problem like your other post today? The point of homework is that YOU do it and thereby learn something from it.

void setup() {
Serial.begin(9600);

}

void loop() {
float A = 0; // Speed of Sound
float T = 0; // Temperature

while(T <= 0){
Serial.println(“Enter the temperature”);
while(Serial.available() <= 0);
T = Serial.parseFloat();
Serial.println("");
}

A = 1086*(sqrt(5T+297)/(247));
Serial.println("The speed of sound at “);
Serial.print(T);
Serial.print(” temp is ");
Serial.print(A
T);
Serial.print(" F");
Serial.println("");

}

well, this is my code where did I miss up?

I agree with Delta_G - The information in your question, along with the pseudo code you were provided with is more than enough to complete this task.

In the loop() function, ask the user for a number to check, N

Just to help you with this bit, since some people have trouble in this area, read this:- Serial Input Basics

How Write a program that calculates the speed of sound (a) in air of a given temperature T (oF). The speed of sound must be calculated in a function and the results printed to the screen from the loop() function. Formula to compute the speed in ft/sec: a=1086√((5T+297)/247)

This is what I did so far, where did I miss up? because it is not calculating the right values

void setup() {
Serial.begin(9600);

}

void loop() {
float A = 0; // Speed of Sound
float T = 0; // Temperature

while(T <= 0){
Serial.println(“Enter the temperature”);
while(Serial.available() <= 0);
T = Serial.parseFloat();
Serial.println("");
}

A = 1086*(sqrt(5T+297)/(247));
Serial.println("The speed of sound at “);
Serial.print(T);
Serial.print(” temp is ");
Serial.print(A
T);
Serial.print(" F");
Serial.println("");

}

A = 1086*(sqrt(5*T+297)/(247));

should be

A = 1086*(sqrt((5*T+297)/247));

and

Serial.print(A*T);

Why would you do this? It's the second problem.

@xoxoxo, it is bad practice to cross-post. Functions Program
Also bad practice to post your code inline. It must be between code tags. </> in the post window.

And this is wrong:-

``````while(Serial.available() <= 0);
``````

Remove the semicolon from the end, then wrap the code you want to execute during the ‘while’ loop in {}

Plus what Jimmy60 just said…

Edit: Also the speed of sound is not measured in Fahrenheit, (F):-

Serial.println(“The speed of sound at “);
Serial.print(T);
Serial.print(” temp is “);
Serial.print(A*T);
Serial.print(” F”);

xoxoxo: I did my own program but it didn't seem right and I would like to compare.

I don't understand "seem". Are you telling me that you simply look at your code without actually trying it out to see if it goes?

What one would normally do in this situation is to work out the expected values at (say) freezing, boiling, and room temperature with a calculator, then throw those numbers at the arduino and see if it says the same thing.

Or is the problem that your code to read Serial to get the number relies entirely on available(), without checking for delimiters?

@xoxoxo, do not cross-post. Threads merged.

xoxoxo: How to write a program that (detailed stetp-by-step description of how to do this in the homework problem).

How to write?

Start arduino IDE. Plug in arduino board. Upload empty program. IDE should say "OK!". Program should do nothing. If not work, adjust your port/board settings in the menu

In setup, turn on serial, print hello world. Upload program. Check serial monitor. Serial monitor should say "hello world".

In loop. read character and echo it back out. Open serial monitor and type. You should see double characters

In loop, read into array, limited by any white space. Then print out array. In serial monitor, you shouls see yu characters echoed a word at a time.

In loop, convert array into number with stod (i think) echo number back to serial. You should see the number you enter, but the format might change. eg, you enter 1e-2 , you get back 0.01 .

Write empty primecheck function. Make it always return false. In loop, pass number to primecheck, inspect result, print "(number) is prime" or "(number) is not prime". Confirm that in serial, you can put in a number and it always says "(number) is not prime".

Alter primecheck function to always return true. Confirm that in serial, you can puti n a number and it always says "(number) is prime".

Code up primecheck according to the very detailed and explicit instructions your teacher gave you.

Job done.

@xoxoxo : stop cross-posting before you get banned. Threads merged.

How to write a program that asks the user for a number and then determines if the number is prime or not. This determination is done in a function called prime() that accepts an integer number and returns a result of prime or not. (Use a flag to do this). See appendix for how to determine if a number is prime and how to use flags. Here is the skeleton (pseudocode)

So this is your homework assignment, right? And if we give you the code you will hand that in, yes? And you are hoping to get a good mark? And then you will understand nothing?

What a mess. Just before I unsubscribe, @xoxoxo, besides the double posting issue, and trying to get others to do your homework for you, and posting code inline rather than between code tags, when posting different questions on different subjects, please use a different title for each!

And while you're at it, also please make them "descriptive" titles, that give some indication of what the topic is. You take the prize this week for the most problems caused by a single poster in one day. :D

A = 1086*(sqrt(5*T+297)/(247));

as the formula is in essence float, you better define the constants as float too. () are good but you do not need that much.

A = 1086.0 * sqrt(5.0 * T + 297.0)/247.0;