Click here to Skip to main content
15,881,863 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi. can someone help me with the syntax in my code. im still beginning to learn about C++.
C++
#include<iostream>
#include<math.h>
#define _USE_MATH_DEFINES // for C++
#include <cmath>
using namespace std;

int main(){
	
	int  iTerm;
	float  iSum=0.00,iDenom=1.00,iError;
	float iPi =  M_PI;
	
    cout<<"|Term|Value|Error|"  <<endl;

    for(iTerm=1;iTerm<=100;iTerm++){
	
		if(iTerm%2==1){
			iSum=iSum + (4/iDenom);
			cout<<"|"<<iTerm << "|"<<iSum;
			iError = iSum - iPi;  
			cout<<"|"<< iError <<endl;
			cout<<"\n";
			iDenom = iDenom + 2;
        }
        else{
		    iSum =iSum - (4/iDenom);
		    cout<<"|"<<iTerm<<"|"<<iSum;
		    iError = iSum - iPi; 
		    cout<<"|"<< iError <<endl;
		    cout<<"\n";
		    iDenom = iDenom + 2;
        }
    }
}


can someone help me shorten the the syntax inside if and else. in C there is a way to shorten this.


additional help:
how can I make iError into printing 2 decimal places? in C its %.2f

thank you
Posted
Updated 3-Feb-15 4:29am
v2

If you have a look at your code
then you will see that both branches of your if/else statement
contain mostly the same.
The only difference is "iSum=iSum + (4/iDenom);" vs. "iSum =iSum - (4/iDenom);"

So your code should look like this:
C++
#include<iostream>
#include<math.h>
#define _USE_MATH_DEFINES // for C++
#include <cmath>
using namespace std;

int main(){

	int  iTerm;
	float  iSum = 0.00, iDenom = 1.00, iError;
	float iPi = M_PI;

	cout << "|Term|Value|Error|" << endl;

	for (iTerm = 1; iTerm <= 100; iTerm++){

		if (iTerm % 2 == 1){
			iSum = iSum + (4 / iDenom);
		}
		else{
			iSum = iSum - (4 / iDenom);
		}

		cout << "|" << iTerm << "|" << iSum;
		iError = iSum - iPi;
		cout << "|" << iError << endl;
		cout << "\n";
		iDenom = iDenom + 2;
	}
}

You should always follow the DRY principle:
http://en.wikipedia.org/wiki/Don%27t_repeat_yourself[^]

For string formatting please have a look at:
http://www.tutorialspoint.com/c_standard_library/c_function_sprintf.htm[^]
 
Share this answer
 
v2
Comments
Reuben Cabrera 4-Feb-15 8:21am    
thank you :)
Reuben Cabrera 4-Feb-15 8:27am    
I tried your code but i didn't seem to change the output into 2 decimal places.
Reuben Cabrera 4-Feb-15 8:28am    
if you run the program you will see that the iError is printed with a lot of decimal places.
TheRealSteveJudge 4-Feb-15 8:40am    
You're welcome!
My code does not contain any string formatting,
it just dealt with "making your code shorter".
For string formatting please have a look at the link mentioned in
the last sentence.
C++
for(iTerm=1;iTerm<=100;iTerm++)
{
	iSum += (8*(iTerm % 2)-4)/iDenom;
	cout<<"|"<<iTerm << "|"<<iSum;
	iError = iSum - iPi;  
	cout<<"|"<< iError << endl << endl;
	iDenom = iDenom + 2;
 }
 
Share this answer
 
Comments
Reuben Cabrera 4-Feb-15 8:22am    
thank you :)
CPallini 4-Feb-15 12:16pm    
You are welcome.
TheRealSteveJudge 4-Feb-15 8:41am    
This is even shorter than my suggestion. :-(
5*
CPallini 4-Feb-15 12:18pm    
Thank you (your asnwer had my 5 as well). :-)
TheRealSteveJudge 5-Feb-15 2:48am    
Thank you, too!
Try

C++
for (iTerm = 1; iTerm <= 100; iTerm++){
 
	if (iTerm % 2)
		iSum += (4 / iDenom);
	
	else
		iSum -= (4 / iDenom);
	
	cout << "|" << iTerm << "|" << iSum;
	iError = iSum - iPi;
	cout << "|" << iError << endl;
	cout << "\n";
	iDenom = iDenom + 2;
}
 
Share this answer
 
Comments
Reuben Cabrera 4-Feb-15 8:22am    
thank you :)
Member 10641779 4-Feb-15 22:52pm    
you are welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900