Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Facing Problem in Loop

Reply
Thread Tools

Facing Problem in Loop

 
 
Vijaykumar Dave
Guest
Posts: n/a
 
      04-11-2007
I have a program for base X power N as under. The problem is that when
the range specified in loop is given it works well, but when any
character is pressed, it goes to infinite loop.

Second problem is it works fine for smaller value of X or N but not
for higher say base 15 power 20. How can I get true value displayed
with such higher base or power.?
Can any one help me to correct this program putting proper loop and
for high value power.

#include<stdio.h>
#include<conio.h>

long double power(double x, double n);

double base, pwr;
long double result;
//int pwr;

void main()
{
clrscr();
gotoxy(22,11);
printf("Enter Base Number : ");
scanf("%lf", &base);
gotoxy(22, 13);
printf("Enter Power Number : ");
scanf("%lf", &pwr);
printf("%lf %lf", base,pwr);

result=power(base,pwr);
gotoxy(22, 15);
printf("Result of Base %f having power %d is\n %60.10Lf.", base,
pwr, result);
getch();
}

long double power(double x, double n)
{
long double res;
if (n < 0)
return 1/power(x, -n);
else if (n == 0)
return 1.0;
else
res=(x*power(x,n-1));
printf("\n%60.10Lf", res);
return(res);
// return (x * power(x, n-1));
}

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      04-11-2007
In article <(E-Mail Removed) .com>,
Vijaykumar Dave <(E-Mail Removed)> wrote:
>I have a program for base X power N as under. The problem is that when
>the range specified in loop is given it works well, but when any
>character is pressed, it goes to infinite loop.


>#include<stdio.h>
>#include<conio.h>


Sorry, there is no conio.h in standard C, and there is no
gotoxy() or getch() functions. As those appear to have something to
do with your I/O and you are having problems with your I/O
("it goes to infinite loop") you will need to consult a newsgroup
that deals with whatever operating system you are using. Or, better
yet, rewrite the program without using those functions.
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      04-11-2007
In article <(E-Mail Removed) .com>,
Vijaykumar Dave <(E-Mail Removed)> wrote:
>I have a program for base X power N as under.


>double base, pwr;


> printf("Result of Base %f having power %d is\n %60.10Lf.", base,
>pwr, result);


You have pwr as a double, but you are attempting to print it with
a %d format, which is only valid for int . It would be surprising
if any of your outputs were correct, other than when pwr was 0.
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
 
Reply With Quote
 
Kelsey Bjarnason
Guest
Posts: n/a
 
      04-11-2007
On Wed, 11 Apr 2007 14:03:42 -0700, Vijaykumar Dave wrote:

> I have a program for base X power N as under. The problem is that when
> the range specified in loop is given it works well, but when any
> character is pressed, it goes to infinite loop.
>
> Second problem is it works fine for smaller value of X or N but not
> for higher say base 15 power 20. How can I get true value displayed
> with such higher base or power.?
> Can any one help me to correct this program putting proper loop and
> for high value power.
>
> #include<stdio.h>
> #include<conio.h>


conio.h isn't standard.

> long double power(double x, double n);
>
> double base, pwr;
> long double result;
> //int pwr;
>
> void main()


main returns int. Now and forever. Always has.

> {
> clrscr();


No such function as clrscr() in C.

> gotoxy(22,11);


No such function as gotoxy in C.

> printf("Enter Base Number : ");
> scanf("%lf", &base);
> gotoxy(22, 13);
> printf("Enter Power Number : ");
> scanf("%lf", &pwr);
> printf("%lf %lf", base,pwr);
>
> result=power(base,pwr);
> gotoxy(22, 15);
> printf("Result of Base %f having power %d is\n %60.10Lf.", base,
> pwr, result);
> getch();


No such function as getch() in C.
> }




> long double power(double x, double n)
> {
> long double res;
> if (n < 0)
> return 1/power(x, -n);
> else if (n == 0)


Note that n is a double. Somehow I suspect at least one of these tests is
going to fail as the n you think should be, say, 0 is actually
0.0000000014376 or some such.

> return 1.0;
> else
> res=(x*power(x,n-1));
> printf("\n%60.10Lf", res);
> return(res);
> // return (x * power(x, n-1));
> }


If I were you, I'd try changing the second parameter of power() to an
integer of some flavour. Also, %lf? I believe that should simply be %f.

According to my testing, the code - after changing n to an int - doesn't
work right. Results:

Your code: 332525673007965060202496.000000
bc: 332525673007965087890625
^^^^^^^^

I'm assuming this is the usual problem when using floating point values.
Indeed, testing it with long doubles bought another four digits of correct
answer.


 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      04-11-2007
Vijaykumar Dave wrote:
> I have a program for base X power N as under. The problem is that when
> the range specified in loop is given it works well, but when any
> character is pressed, it goes to infinite loop.


If you remove the non-standard implementation-specific conio junk,
fix the illegal return type on main,
fix the incorrect "%d" specifier,
provide a '\n' to terminate the last line of output,
and fflush(stdout) after your prompts, you may find your problems disappear.
Of course, you still have the problem of detecting non-integral values
for n, since you have no terminating condition for 0 < n < 1. This will
obviously mean power never ends.

(and don't use tabs or // comments in posted programs, unless you want
them to be unreadable)

> Second problem is it works fine for smaller value of X or N but not
> for higher say base 15 power 20. How can I get true value displayed
> with such higher base or power.?


The equivalent
return (n < 0) ? 1/power(x,-n) : (n == 0) ? 1 : x*power(x,n-1);
has been tested and found to work fine for power(15,20).

You might try a little easier to read output.
#include <float.h> and instead of
> printf("Result of Base %f having power %d is\n %60.10Lf.", base,
> pwr, result);

try
printf("power(%g,%g) = %.*Lg\n", base, pwr, LDBL_DIG, result);

How do you know the result was wrong?

By the way, your variables result and res are typing practice and not
needed.

> Can any one help me to correct this program putting proper loop and
> for high value power.
>
> #include<stdio.h>
> #include<conio.h>
>
> long double power(double x, double n);
>
> double base, pwr;
> long double result;
> //int pwr;
>
> void main()
> {
> clrscr();
> gotoxy(22,11);
> printf("Enter Base Number : ");
> scanf("%lf", &base);
> gotoxy(22, 13);
> printf("Enter Power Number : ");
> scanf("%lf", &pwr);
> printf("%lf %lf", base,pwr);
>
> result=power(base,pwr);
> gotoxy(22, 15);
> printf("Result of Base %f having power %d is\n %60.10Lf.", base,
> pwr, result);
> getch();
> }
>
> long double power(double x, double n)
> {
> long double res;
> if (n < 0)
> return 1/power(x, -n);
> else if (n == 0)
> return 1.0;
> else
> res=(x*power(x,n-1));
> printf("\n%60.10Lf", res);
> return(res);
> // return (x * power(x, n-1));
> }
>

 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      04-12-2007
On Apr 12, 9:45 am, Kelsey Bjarnason <(E-Mail Removed)> wrote:
> > printf("\n%60.10Lf", res);

>
> If I were you, I'd try changing the second parameter of power() to an
> integer of some flavour. Also, %lf? I believe that should simply be %f.


In C99, %Lf is used for printing long doubles and either of
%f and %lf can be used for printing doubles.

 
Reply With Quote
 
Kelsey Bjarnason
Guest
Posts: n/a
 
      04-12-2007
On Thu, 12 Apr 2007 15:32:33 -0700, Old Wolf wrote:

> On Apr 12, 9:45 am, Kelsey Bjarnason <(E-Mail Removed)> wrote:
>> > printf("\n%60.10Lf", res);

>>
>> If I were you, I'd try changing the second parameter of power() to an
>> integer of some flavour. Also, %lf? I believe that should simply be %f.

>
> In C99, %Lf is used for printing long doubles and either of
> %f and %lf can be used for printing doubles.


My bad; somehow I completely overlooked the use of long double for result.

Meanwhile the values passed around are doubles, rendering the use of long
double here kinda pointless.


 
Reply With Quote
 
Vijaykumar Dave
Guest
Posts: n/a
 
      04-13-2007
On Apr 13, 4:19 am, Kelsey Bjarnason <(E-Mail Removed)> wrote:
> On Thu, 12 Apr 2007 15:32:33 -0700, Old Wolf wrote:
> > On Apr 12, 9:45 am, Kelsey Bjarnason <(E-Mail Removed)> wrote:
> >> > printf("\n%60.10Lf", res);

>
> >> If I were you, I'd try changing the second parameter of power() to an
> >> integer of some flavour. Also, %lf? I believe that should simply be %f.

>
> > In C99, %Lf is used for printing long doubles and either of
> > %f and %lf can be used for printing doubles.

>
> My bad; somehow I completely overlooked the use of long double for result.
>
> Meanwhile the values passed around are doubles, rendering the use of long
> double here kinda pointless.


Dear All,

My problem is regarding loop...

It does not work when any key other than the option 1-8 is pressed
keeping program in infinite loop

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      04-13-2007
Vijaykumar Dave wrote:
> On Apr 13, 4:19 am, Kelsey Bjarnason <(E-Mail Removed)> wrote:
>
>>On Thu, 12 Apr 2007 15:32:33 -0700, Old Wolf wrote:
>>
>>>On Apr 12, 9:45 am, Kelsey Bjarnason <(E-Mail Removed)> wrote:
>>>
>>>>> printf("\n%60.10Lf", res);

>>
>>>>If I were you, I'd try changing the second parameter of power() to an
>>>>integer of some flavour. Also, %lf? I believe that should simply be %f.

>>
>>>In C99, %Lf is used for printing long doubles and either of
>>>%f and %lf can be used for printing doubles.

>>
>>My bad; somehow I completely overlooked the use of long double for result.
>>
>>Meanwhile the values passed around are doubles, rendering the use of long
>>double here kinda pointless.

>
>
> Dear All,
>
> My problem is regarding loop...
>
> It does not work when any key other than the option 1-8 is pressed
> keeping program in infinite loop
>

Have you stepped through in your debugger to see why?

--
Ian Collins.
 
Reply With Quote
 
Vijaykumar Dave
Guest
Posts: n/a
 
      04-14-2007
On Apr 14, 4:04 am, Ian Collins <(E-Mail Removed)> wrote:
> Vijaykumar Dave wrote:
> > On Apr 13, 4:19 am, Kelsey Bjarnason <(E-Mail Removed)> wrote:

>
> >>On Thu, 12 Apr 2007 15:32:33 -0700, Old Wolf wrote:

>
> >>>On Apr 12, 9:45 am, Kelsey Bjarnason <(E-Mail Removed)> wrote:

>
> >>>>> printf("\n%60.10Lf", res);

>
> >>>>If I were you, I'd try changing the second parameter of power() to an
> >>>>integer of some flavour. Also, %lf? I believe that should simply be %f.

>
> >>>In C99, %Lf is used for printing long doubles and either of
> >>>%f and %lf can be used for printing doubles.

>
> >>My bad; somehow I completely overlooked the use of long double for result.

>
> >>Meanwhile the values passed around are doubles, rendering the use of long
> >>double here kinda pointless.

>
> > Dear All,

>
> > Myproblemis regardingloop...

>
> > It does not work when any key other than the option 1-8 is pressed
> > keeping program in infiniteloop

>
> Have you stepped through in your debugger to see why?
>
> --
> Ian Collins.- Hide quoted text -
>
> - Show quoted text -


ya... but it is not stopping at scanf if any alphabate is pressed...

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Facing problem while using Wizards with Project Templates !!!!! Anubhav Jain ASP .Net 0 03-20-2006 09:16 AM
Facing problem in loading data from CArchive in VCDotNet. Konark Khera ASP .Net 0 03-22-2005 11:24 AM
Urgent Pls: Facing problem in reading Log information from Log file, created by IIS Amratash ASP .Net 0 04-13-2004 09:08 AM
facing a problem hussein_hariri@hotmail.com ASP .Net 2 12-27-2003 02:06 PM



Advertisments