C++: a beginner's Guide, Second Edition


Project 2-1 Talking to Mars



Download 11,33 Mb.
Pdf ko'rish
bet34/194
Sana12.03.2022
Hajmi11,33 Mb.
#491693
1   ...   30   31   32   33   34   35   36   37   ...   194
Bog'liq
C A Beginner\'s Guide 2nd Edition (2003)

Project 2-1 Talking to Mars 
At its closest point to Earth, Mars is approximately 34,000,000 miles away. Assuming there is someone 
on Mars that you want to talk with, what is the delay between the time a radio signal leaves Earth and 
the time it arrives on Mars? This project creates a program that answers this question. Recall that radio 
signals travel at the speed of light, approximately 186,000 miles per second. Thus, to compute the delay, 
you will need to divide the distance by the speed of light. Display the delay in terms of seconds and also 
in minutes.
Step by Step
1.
Create a new file called Mars.cpp.
2.
To compute the delay, you will need to use floating-point values. Why? Because the time interval 
will have a fractional component. Here are the variables used by the program:
double distance;


11 
C++ A Beginner’s Guide by Herbert Schildt
double lightspeed;
double delay;
double delay_in_min; 
3.
Give distance and lightspeed initial values, as shown here:
distance = 34000000.0; // 34,000,000 miles
lightspeed = 186000.0; // 186,000 per second
4.
To compute the delay, divide distance by lightspeed. This yields the delay in seconds. Assign this 
value to delay and display the results. These steps are shown here:
delay = distance / lightspeed;
cout << "Time delay when talking to Mars: " << delay << " seconds.\n"; 
5.
Divide the number of seconds in delay by 60 to obtain the delay in minutes; display that result using 
these lines of code:
delay_in_min = delay / 60.0;
6.
Here is the entire Mars.cpp program listing:
/*
Project 2-1
Talking to Mars
*/
#include
using namespace std;
int main()
{
double distance;
double lightspeed;
double delay;
double delay_in_min;
distance = 34000000.0; // 34,000,000 miles
lightspeed = 186000.0; // 186,000 per second 
delay = distance / lightspeed;
cout << "Time delay when talking to Mars: " << delay << " seconds.\n"; 
delay_in_min = delay / 60.0;
cout << "This is " << delay_in_min << " minutes.";


12 
C++ A Beginner’s Guide by Herbert Schildt
return 0;
}
7.
Compile and run the program. The following result is displayed:
Time delay when talking to Mars: 182.796 seconds.
This is 3.04659 minutes. 
8.
On your own, display the time delay that would occur in a bidirectional conversation with Mars.

Download 11,33 Mb.

Do'stlaringiz bilan baham:
1   ...   30   31   32   33   34   35   36   37   ...   194




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish