GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Jan-2009, 13:45
armless armless is offline
New Member
 
Join Date: Jan 2009
Posts: 1
armless is on a distinguished road

Need help shortening my programing


Hey everyone I am trying to write a physics program where the user can enter the x and y coordinates and the x and y velocity and then the program will return the x and y coordinates when t = 1, 2, 3, and 4. So far I have got the program running already, but I am only allowed a maximum of 30 lines of code on each function. Here is part of the code that I think can be simplified.
CPP / C++ / C Code:
  double t = 0;
  double x_acc = 0;
  double x_pos = x_init + (x_vel_init * t) + 0.5 * (x_acc * t * t);
  double y_acc = -9.81;
  double y_pos = y_init + (y_vel_init * t) + 0.5 * (y_acc * t * t);

  cout << "Position after 0 second(s) is (" << x_pos << "," << y_pos << ")" << "\n";

  t = 1;
  x_acc = 0;
  x_pos = x_init + (x_vel_init * t) + 0.5 * (x_acc * t * t);
  y_acc = -9.81;
  y_pos = y_init + (y_vel_init * t) + 0.5 * (y_acc * t * t);

  cout << "Position after 1 second(s) is (" << x_pos << "," << y_pos << ")" << "\n";

  t = 2;
  x_acc = 0;
  x_pos = x_init + (x_vel_init * t) + 0.5 * (x_acc * t * t);
  y_acc = -9.81;
  y_pos = y_init + (y_vel_init * t) + 0.5 * (y_acc * t * t);

  cout << "Position after 2 second(s) is (" << x_pos << "," << y_pos << ")" << "\n";

  t = 3;
  x_acc = 0;
  x_pos = x_init + (x_vel_init * t) + 0.5 * (x_acc * t * t);
  y_acc = -9.81;
  y_pos = y_init + (y_vel_init * t) + 0.5 * (y_acc * t * t);

  cout << "Position after 3 second(s) is (" << x_pos << "," << y_pos << ")" << "\n";

  t = 4;
  x_acc = 0;
  x_pos = x_init + (x_vel_init * t) + 0.5 * (x_acc * t * t);
  y_acc = -9.81;
  y_pos = y_init + (y_vel_init * t) + 0.5 * (y_acc * t * t);

  cout << "Position after 4 second(s) is (" << x_pos << "," << y_pos << ")" << "\n";


Any suggestions would be nice. Thanks!
  #2  
Old 15-Jan-2009, 14:00
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Need help shortening my programing


You should look into using loops.
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Write a program to compute for the average grade of N scholars ae012 C++ Forum 5 12-Oct-2008 22:14
Polymorphic programing harry1617 C++ Forum 3 24-Jun-2008 23:01
How do I load DLL files for windows programing in the Microsoft visual c++? mhasan_mitul C++ Forum 1 01-Nov-2006 15:42

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:10.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.