4 Replies - 628 Views - Last Post: 01 September 2012 - 08:44 AM Rate Topic: -----

#1 Weisguy216  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-September 12

order of operations in C

Posted 01 September 2012 - 08:01 AM

Hi all this was a lab I had to do for school. The lab is complete and the code works as it should but I'm assuming there is a proper way to do the math that I am missing.
I could not figure out the order of operations so created 2 more variables to do the math for (2b+c) and (d-e).

So is there a "better" way to write the math part of this or is how I did it correct?
This project has already been turned in, I just want to know for future problems as I'm sure the math will get more intense.

// MRW Lab_1b.cpp 
// Solve the equation a(2b+c)+(d-e) from user input
#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
	float vara, varb, varc, vard, vare, bcsum, desum, ans;
	//****heading****
	printf("Please enter the values for variables to be used while solving a(2b+c)+(d-e)\n\n");

	//****User input****
	printf("a=");scanf(" %f",&vara); 
	printf("b=");scanf(" %f",&varb); 
	printf("c=");scanf(" %f",&varc); 
	printf("d=");scanf(" %f",&vard); 
	printf("e=");scanf(" %f",&vare); 

	//****math****
	bcsum = 2*varb+varc;
	desum = vard-vare;
	ans = vara *bcsum+desum;

	//****print final answer****
	printf("The result for a(2b+c)+(d-e) is %f\n", ans );



	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: order of operations in C

#2 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,258
  • Joined: 08-January 12

Re: order of operations in C

Posted 01 September 2012 - 08:08 AM

Yes, there was another way. By using brackets. Something like
ans = vara * (2*varb+varc) + (vard-vare);


Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2517
  • View blog
  • Posts: 4,001
  • Joined: 21-June 11

Re: order of operations in C

Posted 01 September 2012 - 08:09 AM

I'm not entirely sure what you mean when you say that you couldn't figure out the order of operations.

There's nothing wrong with creating additional variables (in fact it is often preferable for readability if the variables have meaningful names), but doing it on one line as in your formula would have worked fine, too:

ans = vara * (2 * varb + varc) + (vard - vare);

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon

Reputation: 5344
  • View blog
  • Posts: 16,679
  • Joined: 25-December 09

Re: order of operations in C

Posted 01 September 2012 - 08:13 AM

Use parentheses. But, sometimes using multiple variables make troubleshooting easier.

I usually make sure of my precedence by separating the addition/subtraction and division/multiplication.

ans = (vara * ((2 * varb) + varc))) + (vard - vare);

Jim

This post has been edited by jimblumberg: 01 September 2012 - 08:16 AM

Was This Post Helpful? 0
  • +
  • -

#5 Weisguy216  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-September 12

Re: order of operations in C

Posted 01 September 2012 - 08:44 AM

Thank you very much for the help. I probably made some kind of typo when I first did this because I could not get the correct answer when using the brackets. I went back and tried what was said and it works fine.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1