473,387 Members | 1,619 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,387 software developers and data experts.

program won't work

My aim is to show that the armonic series diverges, by showing that it's bigger of any given real number b
If you consider b=2 as argument of NSA the while cycle should do four iterations and then return s=4. Well...
it doesn't. What's the problem?
#include<stdio.h>

int NSA(float b){

int i=1;
int s=0;

float sum=0;

while(1){
sum+=(1/i);
s=s+1;
if(sum>=b)
break;
else
i=i+1;
}
return s;
}

int main() {
float a;
int m;
printf("Choose a real number: ");
scanf("%f",&a);
m=NSA(a);
printf("The minimum n for which the armonic series is equal to %f is %d\n ",a,m);
}
Jun 29 '08 #1
11 1139
Bingo Bongo wrote:
My aim is to show that the armonic series diverges, by showing that it's bigger of any given real number b
If you consider b=2 as argument of NSA the while cycle should do four iterations and then return s=4. Well...
it doesn't. What's the problem?
(1 / any_integer_greater_than_one == 0)

(1.0 / i)
>

#include<stdio.h>

int NSA(float b){

int i=1;
int s=0;

float sum=0;

while(1){
sum+=(1/i);
s=s+1;
if(sum>=b)
break;
else
i=i+1;
}
return s;
}

int main() {
float a;
int m;
printf("Choose a real number: ");
scanf("%f",&a);
m=NSA(a);
printf("The minimum n for which the armonic series is equal to %f is %d\n ",a,m);
}


/* BEGIN new.c */

#include<stdio.h>

int NSA(double b)
{
int i = 1;
double sum = 1.0;

while (b sum) {
sum += 1.0 / ++i;
}
return i;
}

int main(void)
{
double a;
int m;

printf("Choose a real number: ");
fflush(stdout);
scanf("%lf", &a);
m = NSA(a);
printf("The minimum n for which "
"the armonic series is equal to %f is %d\n ", a, m);
return 0;
}

/* END new.c */
--
pete
Jun 29 '08 #2

sum += 1.0 / ++i;
Is there an equivalent code for this piece? I don't know what the / means here.
Jun 29 '08 #3
On 29 Haziran, 18:26, Bingo Bongo <bigmesshumphre...@hotmail.com>
wrote:
* * * * *sum += 1.0 / ++i;

Is there an equivalent code for this piece? I don't know what the / means here.
sum += 1.0 / ++i;

sum = sum + 1.0 / ++i;

First of all i is incremented and the result is divider of 1.0 and
the result of that is adding to sum.
Jun 29 '08 #4
Bingo Bongo wrote:
>
> sum += 1.0 / ++i;

Is there an equivalent code for this piece? I don't know what the / means here.
(++i, sum+=((double)1/i));
Here's a clue; you wrote this:

sum+=(1/i);

--
pete
Jun 29 '08 #5
Bingo Bongo wrote:
>
> sum += 1.0 / ++i;

Is there an equivalent code for this piece? I don't know what the / means here.
It means divided-by, just as it always does in C.

sum equals sum plus 1.0 divided by (i plus 1)

pete's point is that in C if both the divsor and dividend are integers,
then integer division is performed and integer 1 divided by anything
positive is zero. Hence you need to force floating-point maths by using
1.0 which is a double.

--
Mark McIntyre

CLC FAQ <http://c-faq.com/>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Jun 29 '08 #6

"Bingo Bongo" <bi***************@hotmail.comwrote in message
news:48***********************@reader2.news.tin.it ...
>
> sum += 1.0 / ++i;

Is there an equivalent code for this piece? I don't know what the / means
here.
sum = sum + 1 / (i+1);
i = i + 1;
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jun 29 '08 #7
Bingo Bongo wrote:
armonic
A lot of times when you write down something
that you heard a Brit say, they're not real words.

--
pete
Jun 29 '08 #8
Malcolm McLean wrote:
>
"Bingo Bongo" <bi***************@hotmail.comwrote in message
news:48***********************@reader2.news.tin.it ...
>>
>> sum += 1.0 / ++i;

Is there an equivalent code for this piece? I don't know what the /
means here.

sum = sum + 1 / (i+1);
i = i + 1;
That contains the original error of having integer division
where floating point division is required.

--
pete
Jun 29 '08 #9
On Jun 29, 5:54*pm, pete <pfil...@mindspring.comwrote:
Bingo Bongo wrote:
armonic

A lot of times when you write down something
that you heard a Brit say, they're not real words.
what makes you think he's a Brit?

--
Nick Keighley
Jun 30 '08 #10
Nick Keighley wrote:
On Jun 29, 5:54 pm, pete <pfil...@mindspring.comwrote:
>Bingo Bongo wrote:
>>armonic
A lot of times when you write down something
that you heard a Brit say, they're not real words.

what makes you think he's a Brit?
I would say
"a harmonic series". A Brit would be more likely to say
"an harmonic series" with a less pronounced 'h'.

--
pete
Jun 30 '08 #11
On Mon, 30 Jun 2008 07:51:31 -0700 (PDT), Nick Keighley
<ni******************@hotmail.comwrote:
>On Jun 29, 5:54*pm, pete <pfil...@mindspring.comwrote:
>Bingo Bongo wrote:
armonic

A lot of times when you write down something
that you heard a Brit say, they're not real words.

what makes you think he's a Brit?
It was a joke prompted by the typo.
Remove del for email
Jul 1 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: gf | last post by:
I believe I am close but I have spent several hours investigating this and can't understand why it won't work. I have tried exec, system, and settled on popen from reading various notes on the...
15
by: Aubrey Hutchison | last post by:
### this will test if your version of IDLE on windows is a MATURE program design ### print "Hello world, If Python IDLE is a mature program design it will output this" Save this simple...
17
by: los | last post by:
Hi, I'm trying to create a program similar to that of Google's desktop that will crawl through the hard drive and index files. I have written the program and as of now I just put the thread to...
4
by: muser | last post by:
Can anyone run this program through their compiler or if they can see a logical error please point it out. I have my tutor working on it at the moment but I would rather a less ambigious response...
2
by: Sally | last post by:
I have written a gui application for windows os using Borland C++ Builder. Does anybody know how I would go about automating the installation process - for instance, I would like my executable to...
17
by: bastiaannaber | last post by:
I am trying to write a program which uses popen but I have a problem. I want to detect if the program I call with popen has ended. For example: #include <stdio.h> #include <sys/types.h>...
6
by: tigrfire | last post by:
I've been working on a program to try and play a game of Craps, based on a version I found elsewhere - I didn't code the original, but I added a few things such as a balance and wager system. I'm...
13
by: John Salerno | last post by:
If I want to write my code in a separate text editor (I like UltraEdit) but then press a single button to have that code run in the IDLE environment, is that possible? I know that you can configure...
43
by: davidkoree | last post by:
I mean not about cookie. Does it have something to do with operating system or browser plugin? I appreciate any help.
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.