SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
C++ Basics
Exercise:
Programming Projects
Question:15 | ISBN:9780321531346 | Edition: 7

Question

15. Sound travels through air as a result of collisions between the molecules in the air. The temperature of the air affects the speed of the molecules,which in turn affects the speed of sound. The velocity of sound in dry air can be approximated by the formula:

                                velocity _ 331.3 _ 0.61 _ Tc

Where Tc is the temperature of the air in degrees Celsius and the velocity is in meters/second.

Write a program that allows the user to input a starting and an ending temperature. Within this temperature range, the program should output the temperature and the corresponding velocity in one-degree increments.

For example, if the user entered 0 as the start temperature and 2 as the end temperature then the program should output:

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

// main.cpp
#include <iostream>
using namespace std;

int main()
{
	double start;
	double end;
	double velocity;

	cout << "Enter the starting temperature: ";
	cin >> start;

	cout << "Enter the ending temperature: ";
	cin >> end;

	while(start <= end)
	{
		velocity = 331.3 + 0.61 * start;

		cout << "At " << start 
			<< " degrees Celsius the velocity of sound is " 
			<< velocity << " m/s" << endl;

		start++;
	}

	return 0;
}

Output:

Enter the starting temperature: 0
Enter the ending temperature: 2
At 0 degrees Celsius the velocity of sound is 331.3 m/s
At 1 degrees Celsius the velocity of sound is 331.91 m/s
At 2 degrees Celsius the velocity of sound is 332.52 m/s

 

0 0

Discussions

Post the discussion to improve the above solution.