C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
is this codee alright ? Posted by sasha88 on 9 Feb 2007 at 4:48 AM
#include<stdio.h>
#include<conio.h>

void main ()
{
clrscr();
float a=1;int p;float b;
printf("Enter the value as format(power,base):");
scanf("%f,%d",&b,&p);
for (;p!=0;p--)
a*=b;
printf("\nAnswer=%g",a);
getch();
}
Report
Re: is this codee alright ? Posted by AsmGuru62 on 9 Feb 2007 at 5:16 AM
: #include<stdio.h>
: #include<conio.h>
: 
: void main ()
: {
: 	clrscr();
: 	float a=1;int p;float b;
: 	printf("Enter the value as format(power,base):");
: 	scanf("%f,%d",&b,&p);
: 	for (;p!=0;p--)
: 	a*=b;
: 	printf("\nAnswer=%g",a);
: 	getch();
: }
: 

If it runs and gives the correct result - then it is fine.
Report
Re: is this codee alright ? Posted by chenfeng7 on 9 Feb 2007 at 5:30 AM
This message was edited by chenfeng7 at 2007-2-9 6:50:24

This message was edited by chenfeng7 at 2007-2-9 5:58:47

This message was edited by chenfeng7 at 2007-2-9 5:43:17

This message was edited by chenfeng7 at 2007-2-9 5:36:55

This message was edited by chenfeng7 at 2007-2-9 5:36:9

:
: : #include<stdio.h>
: : #include<conio.h>
: : 
: : void main ()
: : {
: : 	clrscr();
: : 	float a=1;int p;float b;
: : 	printf("Enter the value as format(power,base):");
: : 	scanf("%f,%d",&b,&p);
: : 	for (;p!=0;p--)
: : 	a*=b;
: : 	printf("\nAnswer=%g",a);
: : 	getch();
: : }
: : 

: If it runs and gives the correct result - then it is fine.
:

**********************************************************************

I think it will not give the correct result (or am I the one who is mistaken?). Based from my understanding, the programmer wants to solve for base raised to power. The above code, however, computes for power raised to base.

If I am right, the "red" in the ff code snippet must be changed to "green":
/* solution 1 */
scanf("%f,%d",&b,&p);
scanf("%d,%f",&p,&b);

/* or solution 2 */
printf("Enter the value as format(power,base):");
printf("Enter the value as format(base,power):");

/* Note: Either you do solution 1 or solution 2. Do not do them both. */


Correct me if I am wrong (grammatical errors too) but I think there is a logic error in this program. One more thing, variable declarations should come before any other statement. In other words, do this:
float a=1; int p; float b;
clrscr();
instead of this one:
clrscr();
float a=1; int p; float b;
Hope that helps.

#7
Report
Re: is this codee alright ? Posted by Pruyque on 9 Feb 2007 at 4:56 PM
: Correct me if I am wrong (grammatical errors too) but I think there is a logic error in this program. One more thing, variable declarations should come before any other statement. In other words, do this:
:
float a=1; int p; float b;
: clrscr();
instead of this one:
clrscr();
: float a=1; int p; float b;
Hope that helps.
:
: #7
:
Good going, spotting that base/exponent mix-up!

Where to place declarations depends on wether you're writing C or C++. For C they have to come at the beginning of the function, like you said, for C++ they can come in other places as well.
Report
Re: is this codee alright ? Posted by bilderbikkel on 9 Feb 2007 at 5:39 AM
main() is of return type int
See references at http://www.codepedia.com/1/CMain
bilderbikkel

Report
Re: is this codee alright ? Posted by chenfeng7 on 9 Feb 2007 at 5:50 AM
: main() is of return type int
: See references at http://www.codepedia.com/1/CMain
: bilderbikkel
:
:
Yeah. Although I think the Turbo C 2.01 C compiler accepts main to have a void return type. It's not a good programming practice, though, I think, since the OS has no way to know whether the program terminated normally.

#7




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.