S
S
skaf12018-05-14 20:52:30
C++ / C#
skaf1, 2018-05-14 20:52:30

Correct C++ code?

Task:
C++ Calculate the average speed of a motorcyclist in the section from point A to B through point B, if the distance between points A and B is S1, and between B and C - S2. The time of movement between A and B is t1, and between B and C is t 2. The average speed is defined as: V = (S1 + S2) / (t1 + t2). Parameters S1,S2,t1,t2 are entered by the user from the keyboard
Solution:

#include "stdafx.h"
#include <string>
#include <iostream>

using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{

setlocale(LC_ALL, "Russian");
int s1,s2,t1,t2;
float U;
cout<<"Введите скорость от А до Б s1 = ";
cin>>s1;
cout<<"Введите скорость от Б до В s2 = ";
cin>>s2;
cout<<"Введите время от А до Б t1 = ";
cin>>t1;
cout<<"Введите время от Б до В s2 = ";
cin>>t2;
if ((t1 > 0) || (t2 > 0))
{
U = (s1 + s2)/(t1 + t2);
cout<<"Средняя скорость U = " << U;
}
getchar();
return 0;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2018-05-14
@GavriKos

If the answer is correct for all input data, then the code is correct.
And also - use the markup when writing on the toaster - otherwise the inclusions were eaten

P
Pavel K, 2018-05-14
@PavelK

Not really.
At the output, you will have an integer (if it was intended, then it is correct, but it was hardly intended).
Otherwise, read about type casting.
Namely: U = (float)(s1 + s2)/(t1 + t2);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question