User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 397,850 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,345 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 7101 | Replies: 5
Reply
Join Date: Jul 2004
Posts: 6
Reputation: ellisrn is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ellisrn ellisrn is offline Offline
Newbie Poster

factorial using a for loop

  #1  
Jul 19th, 2004
I need to write a program which will execute a factorial, but with an upper and lower bound. Here's what I've written, but it's not working. I keep getting an error message on the for command with expression syntax. Please, I've got to have this and much much more by wednesday.

Thanks!


#include <iostream.h>


#define q = 3

int main()


{
int n;
if (n < 0) return 0;
int f;

for (n=1; n <= q; n++)
{
double f = 1;
{f *= n;}
}

cout<< f;
char w;
cin>>w;
return 0;
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 3,462
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: factorial using a for loop

  #2  
Jul 19th, 2004
The following header is deprected, instead try to use <iostream>
#include <iostream.h>
You shouldn't have the equals sign here.
#define q = 3
I have no idea what this is intended to do.
int n;
if (n < 0) return 0;
Again, your code it a little unclear here. It says to declare a variable f, set it equal to 1 and then multiply by n. This f and the int f previously declared are different.
for (n=1; n <= q; n++)
{
double f = 1;
{f *= n;}
}
When the loop finishes, the double f should go out of scope. So the value that is being printed at the end is the uninitialized value of the int f.

(If it were to compile.)

It couldn't hurt to use more descriptive names, too. Maybe something like this.
#include <iostream> // let's try to use a compiler from this millenium
 
int main()
{
int n, result = 1, value = 3;
for ( n = 1; n <= value; n++ )
{
	 result *= n;
}
std::cout << value << "! = " << result << std::endl;
std::cin.get(); // pause ???
return 0;
}
 
/* my output
3! = 6
*/
Reply With Quote  
Join Date: Jul 2004
Posts: 6
Reputation: ellisrn is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ellisrn ellisrn is offline Offline
Newbie Poster

Re: factorial using a for loop

  #3  
Jul 19th, 2004
Thanks, I'll try and integrate this into my monster program.




Originally Posted by Dave Sinkula
The following header is deprected, instead try to use <iostream>
#include <iostream.h>
You shouldn't have the equals sign here.
#define q = 3
I have no idea what this is intended to do.
int n;
if (n < 0) return 0;
Again, your code it a little unclear here. It says to declare a variable f, set it equal to 1 and then multiply by n. This f and the int f previously declared are different.
for (n=1; n <= q; n++)
{
double f = 1;
{f *= n;}
}
When the loop finishes, the double f should go out of scope. So the value that is being printed at the end is the uninitialized value of the int f.

(If it were to compile.)

It couldn't hurt to use more descriptive names, too. Maybe something like this.
#include <iostream> // let's try to use a compiler from this millenium
 
int main()
{
int n, result = 1, value = 3;
for ( n = 1; n <= value; n++ )
{
	 result *= n;
}
std::cout << value << "! = " << result << std::endl;
std::cin.get(); // pause ???
return 0;
}
 
/* my output
3! = 6
*/
Reply With Quote  
Join Date: May 2004
Location: Egypt - Cairo
Posts: 129
Reputation: meabed is on a distinguished road 
Rep Power: 5
Solved Threads: 2
Colleague
meabed's Avatar
meabed meabed is offline Offline
Junior Poster

Re: factorial using a for loop

  #4  
Jul 20th, 2004
see this example dude
int main()
{
int num;
do{
cout << "Enter a number between 1 and 10";
cin>>num;
}while( num < 1 || num > 10 ); /* ask some more if we dont get something between 1 and 10 */
double factorial( num );
for( int n(num-1) ; n > 0 ; factorial *= n, --n); /* demonstration of how the for loop can be extremely useful */
cout << "The factorial of "<< num << " is "<< factorial << endl;
}
Real Eyes Realize Real Lies
Reply With Quote  
Join Date: Jul 2004
Posts: 1
Reputation: ahsn is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ahsn ahsn is offline Offline
Newbie Poster

Help Re: factorial using a for loop

  #5  
Jul 20th, 2004
Hi Guys
actually it is the first time to write to you
i have only 3 quistions and if any one of you has the answer for it please give it to me cause i need it very very urgent:

the first quistion is for factorial also but in PASCAL :
i want to have a program written in PASCAL that calculate the factorial ..but this program must contain stacks..i gave up looking fo the answer cause i didnt found it so pleaaase heeeelp meee.

the second quistion is in PASCAL also :
i want to write a program in PASCAL using arrays that can recieve marks of 10 students in 5 subjects then calculate :
1- the average of every student
2 - the average of every subject
3 - the general average of all subjects an all students
4 - the highest mark & the lowest mark.

the third one is in C++ .. i need a programe for three hotdog shops
that recieve how many hotdog and how many bread in each shop then recordin every sale process by losing 1 bread & 1 hot dog every time
and i can ask at any place in programm about the amount of hotdog and bread in each shop
also it must contain a summary at the end of the day about how many bread & hot dog at the end of the day.


well .. i know it is too defficult but please i want it urgent as soon as posiple
please send it to my email at
sa7ari_alshooq@yahoo.com
Reply With Quote  
Join Date: May 2004
Location: Egypt - Cairo
Posts: 129
Reputation: meabed is on a distinguished road 
Rep Power: 5
Solved Threads: 2
Colleague
meabed's Avatar
meabed meabed is offline Offline
Junior Poster

Re: factorial using a for loop

  #6  
Jul 20th, 2004
i can help u with the first .. cuz really i'm not expert with pascal but i know little about it . .. see this code this what i learned in pascal
{---------------------------------------------------------------------
 PROGRAM: FACTORIAL2.PAS
 Version:				   Turbo Pascal 6.0
 -------------------------------------------------------------------}
PROGRAM Factrl2 (Input, Output);
USES
  Crt;
CONST
  MaxM  =  7;
VAR
  Choice,
  M			:  Integer;
  UserQuits	:  BOOLEAN;
{------------------------------------}
{			PROCEDURES			  }
{------------------------------------}
{-----------------------------}
{		  INIT.PROC		  }
{-----------------------------}
PROCEDURE Init;
BEGIN
  TextBackground(Blue);				   { Sets colors					 }
  TextColor(White);
  Window(0,0,80,25);
  ClrScr;
END;
 
{-----------------------------}
{		 .PROC		   }
{-----------------------------}
PROCEDURE Null;
BEGIN
END;
{-----------------------------}
{	  FACTFORDO .PROC		}
{-----------------------------}
PROCEDURE FactForDo;
VAR
  k,								   { index }
  FAC,
  Factorial		 :  INTEGER;		{ partial factorial }
BEGIN
  ClrScr;
  WriteLn;
  Write(' ':20,' Enter an integer - 0 to ',MaxM,': ');
  ReadLn(M);
  IF M <= MaxM THEN
	BEGIN
	  FAC := 1;
	  FOR k := 2 to M DO
		FAC := FAC * K;
	  Factorial := FAC;
	  WriteLn;
	  WriteLn(' ':20,'		  M was entered as: ', M );
	  WriteLn;
	  WriteLn(' ':20,'	   M!, or M Factorial = ', Factorial );
	END
	ELSE
	  BEGIN
		WriteLn;
		WriteLn('The number you entered is greater than ',MaxM,'.  Re-enter. ');
	  END;  { ELSE }
  WriteLn;
  WriteLn(' Press <ENTER> to return to menu.');
  ReadLn;
END;	  {FactForDo}
 
{-----------------------------}
{	  FACTORIAL.PROC		 }
{-----------------------------}
FUNCTION Factorial ( M : Integer ) : Integer;
BEGIN
  IF M = 0 THEN
	Factorial := 1
  ELSE
	Factorial := M * Factorial( M-1 );
END;	  { Factorial }

{-----------------------------}
{		FACTREC.PROC		 }
{-----------------------------}
PROCEDURE FactRec;
BEGIN
  ClrScr;
  WriteLn;
  Write(' ':20,'Enter an integer - 0 to ',MaxM,': ');
  ReadLn(M);
  WriteLn;
  IF M > MaxM THEN
	WriteLn('The number you entered is greater than ',MaxM,'.  Re-enter. ')
	ELSE
	  BEGIN
		WriteLn;
		WriteLn(' ':20,'		  M was entered as: ', M );
		M := Factorial( M );
	  END;  { ELSE }
  WriteLn;
  WriteLn(' ':20,'	   M!, or M Factorial = ', M );
  WriteLn;
  WriteLn(' Press <ENTER> to return to menu.');
  ReadLn;
END;	  {FactRec}
 
{------------------------------------}
{		   MAIN PROGRAM			 }
{------------------------------------}
BEGIN
  Init;
  UserQuits := False;
  REPEAT
	ClrScr;
	WriteLn;
	WriteLn(' ':25, ' Menu For Factorial Methods ' );
	WriteLn;
	WriteLn(' ':20, ' 1.  Factorial Using a FOR DO Loop' );
	WriteLn(' ':20, ' 2.  Factorial Using Recursion'	 );
	WriteLn(' ':20, ' 3.  Quit '						 );
	ReadLn ( Choice );
	CASE Choice OF
	   1  :  FactForDo;
	   2  :  FactRec;
	   3  :  UserQuits := True;
	END;
  UNTIL UserQuits;
END.
Real Eyes Realize Real Lies
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 8:02 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC