Slight Iteration

Been busy lately, so haven’t been able to do a lot. I have been able to make my variables into inputs instead of fixed variables. That’s been about the extent of the modifications of this program. I think next move will have to move to an iterative equation to program so I can test my C++ iteration skills. There are a few more acrobats I may be able to squeeze out of this particular program, but I’m liking the idea of big steps at the moment. If I understand the needed syntax that I’ve read up on and seen so far, then loops aren’t all that different from my learned language of MatLab. If this is truly the case, then it’s just a matter of getting the time to decide which equation I want to work with and write a new program from scratch. Iterative equations are just about a dime a dozen, but I do have a bit of a theme I’m trying to stick to, so I do want to be a little picky moving forward.

Anyway… let’s get back to the changes made to the code from the last post. It didn’t take too many changes to get where I wanted to go. It was simple changes after all. Working in what I guess you could call chronological order (or maybe just logical order), first order of business was to get rid of the initial values given to the variables of m1 and m2. Originally it looked like this:

#include
#include
using namespace std;

int m1(42),m2(1);
double g(9.81), T, numerator, denominator;

Boils down to the fact that I still wanted the variables, but I didn’t want any predetermined values tied to the variables. So that piece then looked like this:

#include
#include
using namespace std;

int m1,m2;
double g(9.81), T, numerator, denominator;

I have declared that I will use variables m1 and m2, but have not given them any initial values. Compiling the source with only this change is possible, will not return errors, but won’t give intelligable results. Executing the compiled program will simply return -NAN or Not A Number. This makes sense to me becuase at this point the variables are still just that – variables. They need numbers! It turns out the syntax is just as simple printing results to the screen. The exact opposite, in fact. The syntax for seeing the current number assigned to a variable has been seen to be ” cout<< ” so it follows that the syntax for assigning a number to a variable in a compiled program would be ” cin>> “. Therefore the source starts to resemble

int main()
{

cin>>m1;
cin>>m2;

There is a problem with this setup, though. That problem would be no interaction with the end user. If anyone other than the source writer (me) was to use this program, they wouldn’t even be able to tell that they need to input something let alone know what they need to input. So clarification is needed for future use. To do that just a little bit of dialogue using the ” cout ” command and some sentences stored as strings using the double quotes ( ” ) . It becomes then:

int main()
{
cout<<“Enter mass 1:”; cin>>m1;
cout<<“Enter mass 2:”; cin>>m2;

Which when ran will output “Enter mass 1:” to the screen and wait for input, then will display “Enter mass 2:” after a carriage return. Subsequently displaying the result. Given all of this, the final form up to this point is this:

// Simple program to calculate tension

#include
#include
using namespace std;

int m1,m2;
double g(9.81), T, numerator, denominator;

int main()
{
cout<<“Enter mass 1:”; cin>>m1;
cout<<“Enter mass 2:”; cin>>m2;
numerator = 2*m1*m2;
denominator = m1+m2;
cout<<“Final tension is:”<<endl;
T = (numerator/denominator)*g;

cout<<T<<endl;

return 0;
}

Which provides conversation resembling:

tylerdurden@codebase:~/Documents/Cprogs/source$ ./tension2
Enter mass 1:234
Enter mass 2:432
Final tension is:
2978

That’s probably going to be it for this program for a little while. As mentioned above, will be switching equatin types and writing a new program using iterative methods. As usual, if you have any feedback of any kind, feel free to leave it. I’m still trying to refine this as a technical blog, a reference point, and a developmental exercise for myself so I welcome all constructive feedback.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s