473,385 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

syntax/logic errors

What are the syntax/logic errors in this code? I am confused between the two types.

[code]

int i;
double pi;

pi = 1.0;

for (i=2; i<n, i=i+1)
{
if (i%2 = 1)
pi = pi - (1.0 / ((2.0*i)+1));
else
pi = pi + (1.0 / ((2.0*i)+1));
}

pi = pi * 4.0;


[code]/*



also how can I make the output more accurate?
Mar 21 '10 #1
3 4328
whodgson
542 512MB
Expand|Select|Wrap|Line Numbers
  1. int i;
  2. double pi;
  3. pi = 1.0;
  4. for (i=2; i<n, i=i+1)//; missing  n? not declared
  5. {
  6. if (i%2 = 1)                              // == equality operator required
  7.    pi = pi - (1.0 / ((2.0*i)+1));  
  8.   else
  9.    pi = pi + (1.0 / ((2.0*i)+1));
  10. }
  11. pi = pi * 4.0;
  12.  
Mar 21 '10 #2
donbock
2,426 Expert 2GB
A syntax error is a violation of the C language standard; it triggers a compiler error. A logic error is a violation of what you want the program to do.

The most significant logic error is that you don't have any comments describing what this code snippet is supposed to do. Without a clear statement of your intentions it is difficult to say whether the code accomplishes them.

It looks like you are using a partial geometric series to approximate the value of pi. If so, the second term is missing and all terms except the first have the wrong sign.
Mar 21 '10 #3
donbock
2,426 Expert 2GB
I think your intention is to approximate the value of pi by evaluating the series
pi/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 - ...
(I was mistaken in my earlier post to characterize this as a geometric series.)

There are other techniques that are more efficient (ie, converge faster in fewer iterations). Take a look at Numerical approximations of pi in Wikipedia or do a google search.
Mar 21 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
0
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that sound strange. "Exception are divided into logic errors and...
2
by: Daniel | last post by:
how to make sure a xsl document has valid xsl syntax? i tried loading it into an xml document but that doesnt show syntax errors inside attributes such as "foo/bar" vs "bar\foo"
8
by: BobTheDatabaseBoy | last post by:
the following syntax is accepted: alter table DB2ADMIN.APPTS add constraint appts_status check (lob_appt_status in ( case when state_code = 'VA' then 'P' -- , 'I' else 'X'
3
by: Wayne Wengert | last post by:
I am working through some ASP.NET tutorials and trying to adapt some of them to my data. I built an aspx page that pulls data from an SQL server with a query and use a repeater control to display...
8
by: shawnk | last post by:
Given several nullable boolean flags; bool? l_flg_01 = true; bool? l_flg_02 = false; bool? l_flg_03 = true; bool? l_result_flg = null; I would have liked...
1
by: cindy | last post by:
this is the call private void Page_Load(object sender, System.EventArgs e) { OdbcConnection connection = new OdbcConnection ("DSN=PFW52"); CreateDataAdapter(connection); } this is the code,...
1
by: Hari Sekhon | last post by:
I've written an except hook into a script as shown below which works well for the most part and catches exceptions. import sys def myexcepthook(type,value,tb): do something ...
0
by: fiona | last post by:
FOR IMMEDIATE RELEASE Catalyst release low cost logic processing tool 87% of defects in software are errors in logic Yucca Valley, CA, September 2006 - Catalyst Development Corporation,...
16
by: Chuck | last post by:
Please help me correct the statements in sub "BoundData" The following sub is in a module. Everything runs with no error messages, however,data is not reaching the text box. Data is being...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.