0% found this document useful (0 votes)
2 views5 pages

Include

This C++ program simulates projectile motion using Euler's method. It calculates the position and velocity of an object under the influence of gravity over a total time of 10 seconds, updating at intervals of 0.01 seconds. The results are printed to the console at each time step, showing time, position, and velocity.

Uploaded by

samisohail891
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Include

This C++ program simulates projectile motion using Euler's method. It calculates the position and velocity of an object under the influence of gravity over a total time of 10 seconds, updating at intervals of 0.01 seconds. The results are printed to the console at each time step, showing time, position, and velocity.

Uploaded by

samisohail891
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <iostream> #include <cmath> int main() { // Define constants const double g = 9.

81; //
acceleration due to gravity (m/s^2) // Define variables double t = 0.0; // time (s) double x = 0.0; //
initial position (m) double vx = 10.0; // initial velocity (m/s) double y = 0.0; // initial height (m)
double vy = 20.0; // initial vertical velocity (m/s) // Define time step and total time double dt =
0.01; // time step (s) double t_total = 10.0; // total time (s) // Simulate projectile motion using
Euler's method for (int i = 0; i < t_total / dt; i++) { // Update position and velocity x += vx * dt; y
+= vy * dt; vy -= g * dt; // Print results std::cout << "Time: " << t << " s" << std::endl; std::cout
<< "Position: (" << x << ", " << y << ")" << std::endl; std::cout << "Velocity: (" << vx << ", "
<< vy << ")" << std::endl; // Increment time t += dt; } return 0; } es saare code ki har aik line ka
har aik word explain karen
ChatGPT said:

Bilkul! Aaiye is C++ program ke har ek line aur har ek lafz ko urdu mein asaani se samajhte
hain, takay aapko har cheez clear ho jaye:

cpp
CopyEdit
#include <iostream>

• #include: Yeh ek preprocessor directive hai, jo batata hai ke program mein kisi aur file
ko include karo.
• <iostream>: Is file mein input/output (jaise cin, cout) ke functions hote hain. Yani hum
screen par data print kar sakte hain ya user se input le sakte hain.

cpp
CopyEdit
#include <cmath>

• <cmath>: Yeh C++ ka math library hai. Isme mathematical functions hote hain, jaise
sqrt(), sin(), cos() waghera. Is program mein use nahi hua, lekin future mein use ho
sakta hai.

cpp
CopyEdit
int main() {

• int: Function ka return type hai, matlab yeh function ek integer return karega.
• main(): Yeh C++ ka starting point hota hai. Jab program run hota hai, sabse pehle yahi
function execute hota hai.
• {: Yeh curly brace function ke shuru hone ka sign hai.

cpp
CopyEdit
const double g = 9.81; // acceleration due to gravity (m/s^2)
• const: Iska matlab hai ke yeh variable constant hai — iski value program ke dauraan
change nahi hogi.
• double: Yeh ek data type hai jo decimal values (floating-point numbers) store karta hai.
• g: Yeh variable ka naam hai — yahaan yeh gravity ka symbol hai.
• = 9.81: Is line se g ki value set ki ja rahi hai — 9.81 meters per second squared, jo ke
zameen par gravity ki value hoti hai.
• // ...: Double slash ke baad likha hua comment hota hai — sirf programmer ke liye,
compiler isse ignore karta hai.

cpp
CopyEdit
double t = 0.0; // time (s)

• double: Ek floating-point number.


• t: Time variable ka naam hai.
• = 0.0: Initial time 0 seconds se start ho raha hai.

cpp
CopyEdit
double x = 0.0; // initial position (m)

• x: Horizontal direction mein object ki position.


• = 0.0: Start mein object ground level par hai.

cpp
CopyEdit
double vx = 10.0; // initial velocity (m/s)

• vx: Horizontal direction mein initial velocity — yani 10 meters per second se left/right
move kar raha hai.

cpp
CopyEdit
double y = 0.0; // initial height (m)

• y: Vertical position (height).


• = 0.0: Start mein object ground se shuru ho raha hai (0 meters height).

cpp
CopyEdit
double vy = 20.0; // initial vertical velocity (m/s)

• vy: Vertical (upar) direction mein object ki initial velocity — 20 m/s se object upar ki
taraf ja raha hai.
cpp
CopyEdit
double dt = 0.01; // time step (s)

• dt: Time ka chhota interval ya "step" — har iteration mein kitna time guzra samjha jaye.
• = 0.01: Har step 0.01 seconds ka hai.

cpp
CopyEdit
double t_total = 10.0; // total time (s)

• t_total: Total simulation ka waqt — 10 seconds tak ka motion simulate karna hai.

cpp
CopyEdit
for (int i = 0; i < t_total / dt; i++) {

• for: Yeh loop hota hai, jo bar-bar chalta hai.


• int i = 0: Loop start hone par i ko 0 set karo.
• i < t_total / dt: Jab tak i is value se chhota hai, loop chalta rahega. t_total / dt ka
matlab hai 10 / 0.01 = 1000 steps.
• i++: Har dafa i mein 1 ka izafa hoga.

cpp
CopyEdit
x += vx * dt;

• x +=: Yeh x = x + vx * dt ka short form hai.


• vx * dt: Distance = speed × time. Har second mein kitna faasla tay hua, woh x mein add
karo.

cpp
CopyEdit
y += vy * dt;

• Vertical direction mein bhi position update ho rahi hai.

cpp
CopyEdit
vy -= g * dt;

• vy -=: vy = vy - g * dt
• Gravity ke effect se vertical velocity har second kam ho rahi hai (object neeche gir raha
hai).

cpp
CopyEdit
std::cout << "Time: " << t << " s" << std::endl;

• std::cout: Output screen par likhne ke liye.


• "Time: ": String hai jo print hogi.
• << t: Us waqt ka time print hoga.
• std::endl: New line — agla output nayi line mein aayega.

cpp
CopyEdit
std::cout << "Position: (" << x << ", " << y << ")" << std::endl;

• x aur y dono print ho rahe hain, taake har waqt object ki position maloom ho.

cpp
CopyEdit
std::cout << "Velocity: (" << vx << ", " << vy << ")" << std::endl;

• vx (horizontal) aur vy (vertical) velocity print kar raha hai.

cpp
CopyEdit
t += dt;

• Time update ho raha hai — har step mein t mein dt ka izafa ho raha hai.

cpp
CopyEdit
}

• For loop khatam ho raha hai.

cpp
CopyEdit
return 0;
}

• return 0: Program successfully execute ho gaya, compiler ko return kar diya gaya ke koi
error nahi hai.
• }: main() function ka end.
• string ka matlab:
• C++ mein string ek data type hai jo text ya lafzon ka group store karta hai — jaise:
• cpp
• CopyEdit
• string name = "Sami";
• Yahan "Sami" ek string literal hai, aur name ek variable hai jisme woh text store ho raha
hai.

You might also like