Find speed of man from speed of stream and ratio of time with up and down streams in C++

C++Server Side ProgrammingProgramming

C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

In this problem, we are given two values S and N denoting the speed of stream in Km/h and ratio of time with up and down streams. Our task is to Find speed of man from the speed of stream and ratio of time with up and down streams.

Let’s take an example to understand the problem,

Input

S = 5, N = 2

Output

15

Solution Approach

A simple solution to the problem is by using the mathematical formula for the rowing problems. So, let’s see how the formula will work −

speed of man = x km/h
speed of stream = S km/h
speed of man downstream i.e. with stream = (x+S) km/h
speed of man upstream i.e. against stream = (x-S) km/h
Time to travel the distance downstream = T
Time to travel the distance upstream = n*T
Distance travelled upstream = (x - S)*n*T
Distance travelled upstream = (x + S)*T
As both the distances are same,
(x + S) * T = (x - S)*n*T
x + S = nx - nS
s + nS = nx - x
s*(n + 1) = x(n - 1)

$$x=\frac{S*(S+1)}{(S-1)}$$

Program to illustrate the working of our solution,

Example

Live Demo

#include <iostream>
using namespace std;
float calcManSpeed(float S, int n) {
return ( S * (n + 1) / (n - 1) );
}
int main() {
float S = 12;
int n = 3;
cout<<"The speed of man is "<<calcManSpeed(S, n)<<" km/hr";
return 0;
}

Output

The speed of man is 24 km/hr
Updated on 16-Mar-2021 06:41:00