473,657 Members | 2,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Facing Problem in Loop

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(ba se,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.1 0Lf", res);
return(res);
// return (x * power(x, n-1));
}

Apr 11 '07 #1
18 2089
In article <11************ **********@e65g 2000hsc.googleg roups.com>,
Vijaykumar Dave <vi************ @gmail.comwrote :
>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
Apr 11 '07 #2
In article <11************ **********@e65g 2000hsc.googleg roups.com>,
Vijaykumar Dave <vi************ @gmail.comwrote :
>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
Apr 11 '07 #3
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(ba se,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.1 0Lf", 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: 332525673007965 060202496.00000 0
bc: 332525673007965 087890625
^^^^^^^^

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.
Apr 11 '07 #4
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.hand 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(ba se,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.1 0Lf", res);
return(res);
// return (x * power(x, n-1));
}
Apr 11 '07 #5
On Apr 12, 9:45 am, Kelsey Bjarnason <kbjarna...@nco ldns.comwrote:
printf("\n%60.1 0Lf", 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.

Apr 12 '07 #6
On Thu, 12 Apr 2007 15:32:33 -0700, Old Wolf wrote:
On Apr 12, 9:45 am, Kelsey Bjarnason <kbjarna...@nco ldns.comwrote:
printf("\n%60.1 0Lf", 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.
Apr 12 '07 #7
On Apr 13, 4:19 am, Kelsey Bjarnason <kbjarna...@nco ldns.comwrote:
On Thu, 12 Apr 2007 15:32:33 -0700, Old Wolf wrote:
On Apr 12, 9:45 am, Kelsey Bjarnason <kbjarna...@nco ldns.comwrote:
printf("\n%60.1 0Lf", 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

Apr 13 '07 #8
Vijaykumar Dave wrote:
On Apr 13, 4:19 am, Kelsey Bjarnason <kbjarna...@nco ldns.comwrote:
>>On Thu, 12 Apr 2007 15:32:33 -0700, Old Wolf wrote:
>>>On Apr 12, 9:45 am, Kelsey Bjarnason <kbjarna...@nco ldns.comwrote:

printf("\n%60.1 0Lf", 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.
Apr 13 '07 #9
On Apr 14, 4:04 am, Ian Collins <ian-n...@hotmail.co mwrote:
Vijaykumar Dave wrote:
On Apr 13, 4:19 am, Kelsey Bjarnason <kbjarna...@nco ldns.comwrote:
>On Thu, 12 Apr 2007 15:32:33 -0700, Old Wolf wrote:
>>On Apr 12, 9:45 am, Kelsey Bjarnason <kbjarna...@nco ldns.comwrote:
>>> printf("\n%60.1 0Lf", 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...

Apr 14 '07 #10

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

Similar topics

1
1398
by: Jitendra Shinde | last post by:
I am facing a problem on Session. Firstly i am getting log in the application and woring with my some windows. now keeping that other window open and now from that main window i am logging out. so it throws me to the login page (as i am logged out). I am also clearing and abandoning the session. also with the FromsAuthentication.SignOut().
0
1861
by: John | last post by:
Internet Facing SQL Server, Login Security? How? I would think that our Project is fairly common. We're stuck on a security question. We have a typical SQL Server 2000, running under Windows 2000 Server. Our app is a VB, RDO. The Users are nationwide, in various client companies, all sorts of PC platforms.
2
1466
by: Uma Abhyankar | last post by:
Hello, We are facing precision issues with addition, multiplication or division of "double" Issue1: ##### The output of 0.1 + 0.2 is not 0.3 It is 0.30000000000000004 How do we tackle this??
22
1857
by: Rajshekhar | last post by:
Hi , i am writing a simple prgm to read a .txt file then store the contents into the array... program as follows: -------------------------- #include<stdio.h> int main() { FILE *fp1;
5
1653
by: psbasha | last post by:
Hi, Currently I am using Active Python 2.4.2 IDLE.I am facing the following issue. - When I modify the code in different modules and execute the main module ,and if any error is coming in some module and if I fix that error.Even then the same error is displayed in the "Interactive Window".To get rid of this error I have to close the IDLE and open again the Main module to execute the application,then the error gets disappear. I dont...
3
2247
by: yassinsearch | last post by:
Hi , I am facing problem text direction.(for HEBREW lanaguage) By default the direction of text field is Left to Rignt. Any SRW methods is available to set the reading direction from Right to Left at the field level in the oracle Report ver :10.1.2.0.2. For more clear understand i have attached the screen shot , Note : In FORM we have implemented , problem we are facing in the Reports.
0
1302
by: Niks | last post by:
I am facing a very strange problem in my websevice which is working on socket communication. While i am publishing my webservice to the IIS and invoking a method, a data loss problem in occuring. I seems strange because when i am using the .net framework's inbuild application development server that comes along with 2.0 framework,I am not facing this problem. I dont know why it is not working with IIS while it is working with other...
1
9601
by: manojnkumar | last post by:
#This is to add, sub, mul and div using functions def menu(): print "" print "Welcome to the calculator" print "===== These are some of the options =====" print "1--> for Addition" print "2--> for Subtraction" print "3--> for muliplication"
3
2260
by: coolestavi007 | last post by:
Hi All, I am facing a problem in Ziping any file and then sending it to another server through FTP. What I have to actually do is to retrive the property of files of any directory and then find out the last 24 hours modified files. Then copy it into another directory which named as the current system date. Then Zip that folder in which the last 24 hours modified files are present and then FTP to another server. I have done almost part but...
0
8397
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8310
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8827
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7333
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2731
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.