Hey One and All,
My teacher gave us a set of practice problems which are ones with addresses and she wants us to do the math out long hand and give her the answer by writing it out. On the test we will not be able to run it to find the answer. This one is a little tricky since it is a "for" loop, this is a practice problem that she said I could compile and run to get the answer for this practice problem. I have worked and worked on it and I can't see how the math adds up to 53 even though it must because when I run the program that is the output it gives me. Can someone show me how to do this math so I will be prepared for similar type problems?

Thanks!

#include <iostream>  
using namespace std;         

	void result (int v, int& x);

	int main ()
	{
		int t, c;
		t = 0;
		for ( c = 3;  c < 9;  c= c + 2)
			t = t + 2 * c;
		result( t - 4, t );
		cout << " t value is " << t << endl;
		
system ("pause");
		
		return 0;
	}//main
	
	void result(int v, int& x)
	{
		x =  ( 3  *  v  +  5  -  x );
		return;
	}//result

This program is quite simple.

I think you are getting confused at this

t = t + 2 * c;

I assume you should read it in the order of processing of the compiler.

t =t + (2 * c);

This is the same when it applies to x in the function result.

multiplication is done first and then addition that is the order of preference.

So now lets go through the function.

void result (int v, int& x);

	int main ()
	{
		int t, c;
		t = 0;//t=0;
		for ( c = 3;  c < 9;  c= c + 2)
			t = t + 2 * c;//here t=0+(2*3)//which is equal to 6 then the same continues until you get c=7 and so on.
//Now t=30; therefore t-4 =26
		result( t - 4, t );//Move to comment in result
		cout << " t value is " << t << endl;
		
system ("pause");
		
		return 0;
	}//main
	
	void result(int v, int& x)//v=26;x=30 
	{
		x =  ( 3  *  v  +  5  -  x );//x=((3*26)+5-30)
	//x=76+5-30;
	//x=81-30=51.
return;
	}//result

That is what happens in the code.

Comments
Nice post, well explained!

Nice post sky, well explained...
But I noticed a very very minor mistake here....

void result(int v, int& x)//v=26;x=30 
	{
		x =  ( 3  *  v  +  5  -  x );//x=((3*26)+5-30)
	//x=76+5-30;
	//x=81-30=51.
return;
	}//result

3*26=78, not 76.
So the final value of x is:
x = 78+5-30
x= 83 - 30 = 53 (as reported by the OP!)

Comments
Gotta develop my math
This article has been dead for over six months. Start a new discussion instead.