Consider a sound that is played over a speaker in a rectangular room: The sound
ID: 2075691 • Letter: C
Question
Consider a sound that is played over a speaker in a rectangular room: The sound will lake different amounts of time to propagate to different parts of the room, depending on the speed of sound. Hide speed of sound at 15 degree C at sea level is 343.21 m/s, and the distance between any two points (x_0, y_0) and (x_1, y_1) in a two-dimensional plane is: squareroot (x_1 - x_0)^2 + (y_1 - y_0)^2 We can model the room as an n times m matrix of 1 m-spaced points, where the room is (n - 1) m long and (m - 1) m wide. Each element in this matrix will contain the time at which the sound reached that point. For example, the point in the grid closest to the speaker will have a time value very close to zero, whereas points further away will have higher times. Define a C++ function that will calculate the time (in $) that a sound played at time t from a speaker at location (x, y) will reach every point in this grid. You may assume that the values of n, m, x and y (all in meters) arc pasted into your function, as is the time I that die sound is played on the speaker. You must design an algorithm to calculate the time values in the matrix, design an appropriate prototype for the function and write a C++ implementation. I would strongly suggest that you sketch out an algorithm in pseudocode before starting on your C++ implementation, but such workings arc optional. Workings (optional):Explanation / Answer
Below is the C++ program to find total time and time at which sound will reach all the grid points.
#include <iostream>
#include <math.h>
#include <algorithm>
using namespace std;
double sound_speed;
double total_time(int n, int m, int x0, int y0, int time_hour, int time_minutes, int time_seconds){
double dist1 = sqrt(pow(x0-n,2)+pow(y0-m,2));
double dist2 = sqrt(pow(x0-n,2)+pow(y0,2));
double dist3 = sqrt(pow(x0,2)+pow(y0-m,2));
double dist4 = sqrt(pow(x0,2)+pow(y0,2));
double dist = max(dist1,dist2);
dist = max(dist,dist3);
dist = max(dist,dist4);
double total_time = dist/sound_speed;
int seconds = (int)(time_seconds + total_time)%60;
int minutes = (int)(time_minutes+ (time_seconds + total_time)/60)%60;
int hours = (int)(time_hour + ((time_minutes+ (time_seconds + total_time)/60)/60))%12;
cout<<"Time at which sound will reach all the points on the grid is: "<< hours << ":"<<minutes<<":"<<seconds<<endl;
cout<<"Total time taken to reach sound to all points of the grid is: "<< total_time << " seconds."<<endl;
return total_time;
}
int main()
{
int n,m,x0,y0;
int time_hour, time_minutes, time_seconds;
string time;
cout<<"Enter grid size:"<<endl;
cout<<"n = ";
cin>> n ;
cout<<"m = ";
cin>> m ;
cout<<"Enter Position of speaker:"<<endl;
cout<<"Xo = ";
cin>> x0 ;
cout<<"Yo = ";
cin>> y0;
cout<<"Speed of sound (in m/s) = ";
cin>> sound_speed ;
cout<<"Enter start time (format => HH:MM:SS): ";
char c;
cin>> time_hour>> c >> time_minutes>> c >> time_seconds;
double time_taken = total_time(n, m, x0, y0,time_hour,time_minutes,time_seconds);
return 0;
}